Search code examples
phpmysqliupdating

mysqli_error: updating tables (Procedural style)


I am using PHP to try and update information I have in a mysqli table. I have decided to try and use mysqli rather than mysql. Unfortunately I cant seem to find my answer anywhere because im also trying to complete it Procedural style, as I have no knowledge of OOP and all tutorials (that i have found) are in OOP.

Below is the script I have created. I have added comments to say what I think each command is doing.

<?php    
DEFINE('DB_USER', 'root');
DEFINE('DB_PASS', 'password');
DEFINE('DB_NAME', 'test');
DEFINE('DB_HOST', 'localhost');

//connect to db
$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die(mysqli_connect_error($dbc));
mysqli_set_charset($dbc, 'utf8');

//form not submitted
if(!isset($_POST['submit'])){

    $q = "SELECT * FROM people WHERE people_id = $_GET[id]";//compares id in database with id in address bar
    $r = mysqli_query($dbc, $q);//query the database
    $person = mysqli_fetch_array($r, MYSQLI_ASSOC);//returns results from the databse in the form of an array

}else{//form submitted

    $q = "SELECT * FROM people WHERE people_id = $_POST[id]";//compares id in database with id in form
    $r2 = mysqli_query($dbc, $q);//query the database
    $person = mysqli_fetch_array($r2, MYSQLI_ASSOC);//returns results from the database in an array

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $age = $_POST['age'];
    $hobby = $_POST['hobby'];
    $id = $_POST['id'];

    //mysqli code to update the database
    $update = "UPDATE people
               SET    people_fname = $fname,
                      people_lname = $lname,
                      people_age = $age,
                      people_hobby = $hobby
               WHERE  people_id = $id";

    //the query that updates the database
    $r = @mysqli_query($dbc, $update) or die(mysqli_error($r));

    //1 row changed then echo the home page link
    if(mysqli_affected_rows($dbc) == 1){    
        echo "<a href=\"index.php\">home page</a>";
    }
}
?>

The update form

<form action="update.php" method="post">
    <p>First name<input type="text" name="fname" value="<?php echo "$person[people_fname]" ?>" /></p>
    <p>Last name<input type="text" name="lname" value="<?php echo "$person[people_lname]" ?>" /></p>
    <p>Your age<input type="text" name="age" value="<?php echo "$person[people_age]" ?>" /></p>
    <p>Your hobby<input type="text" name="hobby" value="<?php echo "$person[people_hobby]" ?>" /></p>
    <input type="hidden" name="id" value="<?php echo $_GET['id'] ?>" />        
    <input type="submit" name="submit" value="MODIFY" />
</form>`

When I submit the form I get the following error message

Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\sandbox\update.php on line 39

I realize this is telling me the issue is with

$r = @mysqli_query($dbc, $update) or die(mysqli_error($r));

So I have tried to put the sqli code in as the second parameter (i realize this is the same as putting the variable in, but it was a last resort), but it didn't seem right and still didn't work. I have also looked a php.net but couldn't work out the answer from the example they have given

Please advise, I thought this was meant to be simple?


Solution

  • Don't pass $r to mysqli_error. It accepts an optional mysql link, but not a query result anyway.

    In your case, the query is executed. That evaluates to false, which is assigned to $r. The assignment evaluates to false, causing you to call die(mysqli_error($r)) with $r being false.

    I think you meant to pass $dbc to mysqli_error.