Saeed Dofajs Saeed Dofajs - 6 months ago 11
MySQL Question

I get this php mysql error about mysqli?

I want to make php mysql CRUD, but I m struggling with update page.
This code is my update page. I also have a view page with a Update link per row so when I click on update this page appears and you can see your info and change it.
I have a database in phpmyadmin. My Id on my database is: KLANTNR

I get this:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\Users*****************
****\update_klant.php on line 17**
What I am doing wrong?

<?php

$servername = "localhost";
$user_name = "root";
$passwordd = "usbw";
$database = "reizen";
$connection = mysqli_connect($servername,$user_name,$passwordd,$database);
if(!$connection){
die("Database connection failed");
}
$query = "SELECT * FROM klanten";



$id = null;
if(!empty($_GET['KLANTNR'])){
$id = $_REQUEST['KLANTNR'];

}
$sql = "SELECT * FROM klanten WHERE klantnr = $id";
$result = mysqli_query($connection,$sql);
$row = mysqli_fetch_array($result);



?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h2>Klant Wijzigen</h2>
<form method="POST" action="update_klant.php">
<table>
<tr>
<tr>
<td>Voornaam</td>
<td><input type="text" value="<?php echo $row['VOORNAAM'];?>" name="txtvnaam"/> </td>


<td>Achternaam</td>
<td><input type="text" value="<?php echo $row['ACHTERNAAM'];?>" name="txtanaam"/> </td>


<td>Adres</td>
<td><input type="text" value="<?php echo $row['ADRES'];?>" name="txtadres"/> </td>

</tr>
<td>Postcode</td>
<td><input type="text" value="<?php echo $row['POSTCODE'];?>" name="txtpcode"/> </td>


<td>Woonplaats</td>
<td><input type="text" value="<?php echo $row['WOONPLAATS'];?>" name="txtwoonplaats"/> </td>


<td>Provincie</td>
<td><input type="text" value="<?php echo $row['PROVINCIE'];?>" name="txtprov"/> </td>


<td>Telefoon</td>
<td><input type="text" value="<?php echo $row['TELEFOON'];?>" name="txttel"/> </td>


<td></td>
<td><input type="submit" name="submit" value= "Toevoegen"/> </td>
</tr>

</table>

</form>




</body>
</html>

Answer

SIMPLE ANSWER: Your mysqli_query function is failing.

Elaborating the answer:

So given your error message:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\Users*********************\update_klant.php on line 17**

We learn that parameter 1 of your mysqli_fetch_array() is a boolean. And inyour code, the parameter is $result.

You obtained $result from the following code:

$result =  mysqli_query($connection,$sql);

The documentation on the return results of mysqli_query says the following:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

So now we know that on failure, you get a boolean, and in your error message, it says $result is a boolean.

Hence, Your mysqli_query function is failing.

The specific reason it is failing could be various. It seems that you are connecting to the database because otherwise your code would have ended way earlier. So the problem seems to be with the execution of your sql statement.

At first glance, the structure of your sql statement seems fine.

MY RECOMMENDATION:

do an echo $sql and comment out the code from $result = ... and under just to view what your finished sql statement looks like. There is a chance that $id isn't a valid value.

Also, it could be that you typed the table name badly. Basically you'll need to debug the problem since you're the only one here with access to your database and we can't see the structure of your tables.