simple update

Discussion in 'PHP' started by ataloss, May 5, 2014.

  1. #1
    Anyone, how to make this work. no results evident.
    <?php
    error_reporting(E_ALL ^ E_NOTICE);
    // error_reporting(0);

    if(isset($_POST['update']))
    {
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'cookie';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);

    if(! $conn ) { die('Could not connect: ' . mysql_error()); }

    $id = $_POST['id'];
    $taxrate = $_POST['taxrate'];
    $sql = "UPDATE numbers ".
    "SET taxrate = $taxrate ".
    "WHERE id = $id" ;
    mysql_select_db('homedb');
    $retval = mysql_query( $sql, $conn );

    if(! $retval ) { die('Could not update data: ' . mysql_error()); }

    echo "Updated data successfully\n";
    mysql_close($conn);
    }
    // else
    // {
    ?>
     
    Solved! View solution.
    ataloss, May 5, 2014 IP
  2. #2
    Use This

    
    <?php
    if(isset($_POST['update']))
    {
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'cookie';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn ) { die('Could not connect: ' . mysql_error()); }
    else {mysql_select_db('homedb');}
    $id = $_POST['id'];
    $taxrate = $_POST['taxrate'];
    $sql = "UPDATE numbers SET taxrate = '$taxrate' WHERE id = '$id' ";
    $retval = mysql_query( $sql, $conn );
    if(! $retval ) {
    die('Could not update data: ' . mysql_error());
    }
    else {
    echo "Updated data successfully\n";
    mysql_close($conn);
    }
    ?>
    
    Code (markup):
     
    xXxpert, May 5, 2014 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    [​IMG]

    1) would help if you used the CODE bbCode...

    2) Not that it matters since you have no formatting (at least none that doing a quote showed) -- no sensible formatting == gibberish code and mistakes galore.

    3) This is 2014, not 2006, you shouldn't be using the mysql_xxx functions

    4) and here's why, you are wide open to sql injections as you are blindly dumping $_POST into your query...

    5) your biggest problem is you don't actually connect to a database. IF you are going to use the buggy outdated outmoded insecure train-wreck known as mysql_ functions, you need to call mysql_select_db so it has some clue what database you are trying to use... (something built into the $DSN part of if you were using PDO like a good little doobie)

    To drag that kicking and screaming into THIS century:
    <?php
    error_reporting(E_ALL);
    
    if (isset($_POST['update'])) {
    	try {
    		$db = new PDO(
    			'mysql:host=localhost;dbname=database',
    			'root',
    			'cookie'
    		);
    		$statement = $db->prepare('
    			UPDATE numbers
    			SET taxrate = :taxrate
    			WHERE id = :id
    		');
    		$statement->execute([
    			':taxrate' => $_POST['taxrate'],
    			':id' => $_POST['id']
    		]);
    		if ($statement->rowCount()) {
    			echo 'Updated Data Successfully';
    		} else die('Could Not Update Data');
    	} catch (PDOException $e) {
    		die('Connection failed ' . $e->getMessage());
    	}
    }
    
    ?>
    Code (markup):
     
    deathshadow, May 5, 2014 IP
    sarahk likes this.
  4. ataloss

    ataloss Active Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #4
    Neither of your approaches work
     
    ataloss, May 7, 2014 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #5
    How much debugging have you done? If you echo out the sql query and run it through phpMyAdmin what error messages do you get?
     
    sarahk, May 7, 2014 IP