Note that there are some explanatory texts on larger screens.

plurals
  1. POmysqli_error: updating tables (Procedural style)
    primarykey
    data
    text
    <p>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.</p> <p>Below is the script I have created. I have added comments to say what I think each command is doing.</p> <pre><code>&lt;?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 "&lt;a href=\"index.php\"&gt;home page&lt;/a&gt;"; } } ?&gt; </code></pre> <p>The update form</p> <pre><code>&lt;form action="update.php" method="post"&gt; &lt;p&gt;First name&lt;input type="text" name="fname" value="&lt;?php echo "$person[people_fname]" ?&gt;" /&gt;&lt;/p&gt; &lt;p&gt;Last name&lt;input type="text" name="lname" value="&lt;?php echo "$person[people_lname]" ?&gt;" /&gt;&lt;/p&gt; &lt;p&gt;Your age&lt;input type="text" name="age" value="&lt;?php echo "$person[people_age]" ?&gt;" /&gt;&lt;/p&gt; &lt;p&gt;Your hobby&lt;input type="text" name="hobby" value="&lt;?php echo "$person[people_hobby]" ?&gt;" /&gt;&lt;/p&gt; &lt;input type="hidden" name="id" value="&lt;?php echo $_GET['id'] ?&gt;" /&gt; &lt;input type="submit" name="submit" value="MODIFY" /&gt; &lt;/form&gt;` </code></pre> <p>When I submit the form I get the following error message</p> <pre><code>Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\sandbox\update.php on line 39 </code></pre> <p>I realize this is telling me the issue is with</p> <p><code>$r = @mysqli_query($dbc, $update) or die(mysqli_error($r));</code></p> <p>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</p> <p>Please advise, I thought this was meant to be simple?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload