I'm trying to update the color value in the database through a form but is working only when I set the id, e.g.: WHERE ID=40"; but I need this to work with all the rows.
<?php
try
{
$bdd = new PDO('mysql:host=localhost;dbname=test;charset=utf8','root', '');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
$reponse = $bdd->query('SELECT id,pseudo, message, color FROM
minichat ORDER BY ID DESC LIMIT 0, 10');
while ($donnees = $reponse->fetch())
{
echo '<form action="minichat_post2.php" method="post">
<table style="border:1px solid black">';
echo '<tr>';
echo '<td style="border:1px solid black">' .$donnees['id']. '</td>';
echo '<td style="border:1px solid black">' .$donnees['pseudo'].
'</td>';
echo '<td style="border:1px solid black">' .$donnees['message'].
'</td>';
echo '<td style="border:1px solid black">' .$donnees['color'].
'</td>';
echo '<td style="border:1px solid black">
<label for="color">Color</label> : <input type="color" name="color"
id="color" /><br />
<input type="submit" value="Envoyer" />
</td></tr></table>
</form>';
}
?>
$reponse->closeCursor();
?>
and this is the update page minichat_post2.php but is working uniquely when I set the id. So I need something to change here: WHERE id=?"; Please Help!!
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$id=$_GET['id'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE minichat SET color='".$_POST['color']."' WHERE id=?";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
header('Location: minichat.php');
?>