Hi,
I'm trying to make a php script as dynamic as possible that will updates my mysql table from a html form.
I want the script to get the actual column names from the mysql table and use these along with the values from the html form
to update the table. Below is what I got so far.
<?php
$link = mysqli_connect('localhost','user','pass','table');
$query = "SELECT * FROM utlaan WHERE id=".$_GET["id"]."";
$result = mysqli_query($link, $query);
$fetch = mysqli_fetch_assoc($result);
if(empty($_POST)) {
print "<table border='0'><form action='" . $_SERVER["PHP_SELF"] . "' method='post'";
foreach($fetch as $key => $value) {
print "<tr><td>" . $key . "</td><td><input type='text' name='" . $value . "' value='" . $value . "'></td></tr>";
}
print "<tr><td colspan='2'><input type='submit' value='Reserver'></td></tr></form></table>";
}
else {
mysqli_query("UPDATE `pdb`.`utlaan` SET `$key` = '$value' WHERE `utlaan`.`id` = $_GET[id];");
print mysqli_affected_rows($link);
}
mysqli_free_result($result);
mysqli_close($link);
?>
The first part (if { }) where the script gets the column names (key) and values from the table and fills it into the form works fine.
The second part (else { }) is where it fails.
I get 3 warnings :
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /var/www/utlaan/endre.php on line 7
Warning: mysqli_query() expects at least 2 parameters, 1 given in /var/www/utlaan/endre.php on line 21
-1
Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in /var/www/utlaan/endre.php on line 26
I wonder how I can get my script to work and update the table. Any help is greatly apreciated.