使用表单,PHP和MYSQL提交,更新和删除

时间:2017-04-26 07:35:37

标签: php mysql forms

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

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

我做错了什么?

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

的index.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>"



?>

resultaat.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;
}

?>

1 个答案:

答案 0 :(得分:0)

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

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

此外,如果不覆盖id,则每行需要一个form进行更新