我正在尝试从textarea提交的列表中更新mysql数据库。 textarea中的每一行代表一个值,该值将与数据库中的现有条目进行比较。如果匹配,则必须更新该行中的状态。使用此代码只有textarea的最后一行有效。我做错了什么?
<?php
if(isset($_POST['regnr']))
{
$db_host = "localhost";
$db_user = "root";
$db_pass = "root";
$db_name = "sonstwas";
$port = 3306;
// Create connection
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$new_status = 2;
$lines = preg_split("/\r\n/", $_POST["regnr"]);
foreach ($lines as $key => $value){
$sql = "UPDATE kunden ". "SET Status = $new_status ". "WHERE RegNr = '$value'";
//echo 'rows '.$key.' ist: "'.$value.'"<br>';
}
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<textarea name="regnr" rows="30" id="regnr"></textarea>
<input name="update" type="submit" id="update" value="Update">
</form>
<?php
}
?>
非常感谢
答案 0 :(得分:0)
你正在循环和准备sql语句,但是你没有做任何事情(当然除了最后准备好的语句)。 所以在循环中移动sql语句的执行。
foreach ($lines as $key => $value){
$sql = "UPDATE kunden ". "SET Status = $new_status ". "WHERE RegNr = '$value'";
//echo 'rows '.$key.' ist: "'.$value.'"<br>';
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
}