doumu2172
2017-04-26 07:35
浏览 74
已采纳

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

I am new to PHP and MySQL. Trying to build a simple form with insert, update, delete functions.

Adding and retrieving information to and from the database is working fine. But when i want to update an existing row, it does not work.

What am i doing wrong?

I have an index.php and an resultaat.php. The form goes to resultaat.php and returns to index.php directly.

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'>
";
$formulier .= "<fieldset>
";
$formulier .= "<label>Naam</label>
";
$formulier .= "<input type='text' name='naam'/>
 <br />";
$formulier .= "<label>Achternaam</label>
";
$formulier .= "<input type='text' name='achternaam'/><br /> 
";
$formulier .= "<label>Email</label>
";
$formulier .= "<input type='text' name='email'/><br /> 
";
$formulier .= "<input type='submit' name='submit' value='bevestigen'/>";
$formulier .= "</fieldset>
";
$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>
";
        echo "<td hidden name='record' value='$row[ID]'>$row[ID]</td>
";
        echo "<td><input type='text' name='naam' value='$row[naam]'</td>
";
        echo "<td><input type='text' name='achternaam' value='$row[achternaam]'</td>
";
        echo "<td><input type='text' name='email' value='$row[email]'</td>
";
        echo "<td><input type='submit' name='update' value='Add' onclick='return confirm(\'Weet je zeker dat je dit item wilt bijwerken?\')/>";
        echo "</tr>
";
    }
} 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条回答 默认 最新

  • dongyu6276 2017-04-26 07:38
    已采纳

    Your id is not in a field. So $_POST['record']; is empty. Use for your id field

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

    Also you Need one form per row for update if not the id will be overridden

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题