gpt4 book ai didi

php - 使用表单、PHP 和 MYSQL 提交、更新和删除

转载 作者:行者123 更新时间:2023-11-30 22:00:13 25 4
gpt4 key购买 nike

我是 PHP 和 MySQL 的新手。尝试构建一个具有插入、更新、删除功能的简单表单。

在数据库中添加和检索信息工作正常。但是当我想更新现有行时,它不起作用。

我做错了什么?

我有一个 index.php 和一个 resultaat.php。表单转到 resultaat.php 并直接返回到 index.php。

索引.php:

<?php

$servername = "localhost";
$username = "root";
$password = "localdb";
$database = "test";

$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {
die("Connectie met SQL mislukt: " . $conn->connect_error);
}


$sql2 = "SELECT ID, naam, achternaam, email FROM nieuw";
$result = $conn->query($sql2);


$formulier = "";
$formulier .= "<form method='post' action='resultaat.php'>\n";
$formulier .= "<fieldset>\n";
$formulier .= "<label>Naam</label>\n";
$formulier .= "<input type='text' name='naam'/>\n <br />";
$formulier .= "<label>Achternaam</label>\n";
$formulier .= "<input type='text' name='achternaam'/><br /> \n";
$formulier .= "<label>Email</label>\n";
$formulier .= "<input type='text' name='email'/><br /> \n";
$formulier .= "<input type='submit' name='submit' value='bevestigen'/>";
$formulier .= "</fieldset>\n";
$formulier .= "</form>";

echo $formulier;

print <<< HERE
<form method='post' action='resultaat.php'>
<table border="1">

<tr>
<th hidden></th>
<th>Naam</th>
<th>Achternaam</th>
<th>Email</th>
<th></th>

</tr>

HERE;

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {

echo "<tr>\n";
echo "<td hidden name='record' value='$row[ID]'>$row[ID]</td>\n";
echo "<td><input type='text' name='naam' value='$row[naam]'</td>\n";
echo "<td><input type='text' name='achternaam' value='$row[achternaam]'</td>\n";
echo "<td><input type='text' name='email' value='$row[email]'</td>\n";
echo "<td><input type='submit' name='update' value='Add' onclick='return confirm(\'Weet je zeker dat je dit item wilt bijwerken?\')/>";
echo "</tr>\n";
}
} else {
echo "Niets te tonen";
}

echo "<tr>";
echo "<td><input type='text'></td>";
echo "<td><input type='text'></td>";
echo "<td><input type='text'></td>";
echo "<td><input type='submit' value='add'></td>";
echo "</tr>";
echo "</table>";
echo "</form>"



?>

结果.php

<?php

$servername = "localhost";
$username = "root";
$password = "localdb";
$database = "test";

$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {
die("Connectie met SQL mislukt: " . $conn->connect_error);
}

$naam = mysqli_real_escape_string($conn, $_REQUEST['naam']);
$achternaam = mysqli_real_escape_string($conn, $_REQUEST['achternaam']);
$email = mysqli_real_escape_string($conn, $_REQUEST['email']);


if(isset($_POST['submit'])) {

$naam = mysqli_real_escape_string($conn, $_REQUEST['naam']);
$achternaam = mysqli_real_escape_string($conn, $_REQUEST['achternaam']);
$email = mysqli_real_escape_string($conn, $_REQUEST['email']);

$sql = "INSERT INTO nieuw (naam, achternaam, email) VALUES ('$naam', '$achternaam', '$email')";
if(mysqli_query($conn, $sql)){

echo "Gegevens zijn verwerkt.";
} else{
echo "ERROR: Aanvraag kon niet verwerkt worden $sql. " . mysqli_error($link);
}

mysqli_close($link);

header("Location: index.php"); // redirect back to your contact form
exit;
}

if(isset($_POST['update'])) {
$id = $_POST['record'];
$naam = mysqli_real_escape_string($conn, $_REQUEST['naam']);
$achternaam = mysqli_real_escape_string($conn, $_REQUEST['achternaam']);
$email = mysqli_real_escape_string($conn, $_REQUEST['email']);


$UpdateQuery = "UPDATE nieuw SET naam = '$naam', achternaam = '$achternaam', email = '$email' WHERE ID='$id'";
if(mysqli_query($conn, $UpdateQuery)){

echo "Gegevens zijn verwerkt.";
} else{
echo "ERROR: Aanvraag kon niet verwerkt worden $UpdateQuery. " . mysqli_error($link);
}


exit;
}

?>

最佳答案

您的 ID 不在字段中。所以 $_POST['record']; 是空的。用于您的 id 字段

    echo "<td><Input type='hidden' name='record' value='$row[ID]'>$row[ID]</td>\n";

你还需要每行一个表单来更新,否则id将被覆盖

关于php - 使用表单、PHP 和 MYSQL 提交、更新和删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43627800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com