dongxi7704 2017-07-10 19:45
浏览 59

sql UPDATE取决于行id

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');
?>
  • 写回答

1条回答 默认 最新

  • doulan1073 2017-07-10 19:50
    关注

    You need to add

      echo "<input type='hidden' name='id' value='".$donnees['id']."'>";
    

    into your first file (probably before the last echo). Then change $id = $_GET['id']; to $id = $_POST['id']; in the second file (as you are using method='post', not method='get'). And also in the second file change your query to:

    $sql = "UPDATE minichat SET color='".$_POST['color']."' WHERE id=".(int)$id;
    

    Please note that this code is susceptible to SQL injection attack. Using prepared queries may one way to mitigate.

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料