0

不要介意安全问题,这只是本地测试,但是当我单击更新按钮时,页面或查询上的任何更改都不会发生,我也不会出错。

<?php 

$link = mysqli_connect("localhost", "root", "", "test") or die("could not connect");

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

$updateQuery = (" UPDATE `test1` SET f_name = '$_POST[f_name]', l_name='$_POST[l_name]', email='$_POST[email]' WHERE id='$_POST[id]'");
mysqli_query($link, $updateQuery);


};


$query = ("SELECT * FROM `test1`");
$result = mysqli_query($link, $query);

echo "<table border=1
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th> 
</tr>";

while($row = mysqli_fetch_array($result)) {
echo "<form method=post action=update.php>";
echo "<tr>";
echo "<td>" . "<input type=text name=f_name value=" . $row['f_name'] . " </td>";
echo "<td>" . "<input type=text name=l_name value=" . $row['l_name'] . " </td>";
echo "<td>" . "<input type=text name=email value=" . $row['email'] . " </td>";
echo "<td>" . "<input type=hidden name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=submit value=update" .  " </td>";
echo "</tr>";
}

?>

4

3 回答 3

0

将您的表格更改为

while($row = mysqli_fetch_array($result)) {
    echo "<form method=post action=update.php>";
    echo "<input type=hidden name=update>";
    echo "<tr>";
    echo "<td>" . "<input type=text name=f_name value=" . $row['f_name'] . " </td>";
    echo "<td>" . "<input type=text name=l_name value=" . $row['l_name'] . " </td>";
    echo "<td>" . "<input type=text name=email value=" . $row['email'] . " </td>";
    echo "<td>" . "<input type=hidden name=id value=" . $row['id'] . " </td>";
    echo "<td>" . "<input type=submit name=submit value=update" .  " </td>";
    echo "</tr>";
}
于 2013-07-24T17:57:55.007 回答
0

POST 键应该用引号引起来。试试这个:

$updateQuery = "UPDATE test1 SET f_name = ".$_POST['f_name'].", l_name=."$_POST['l_name'].", email=".$_POST['email']." WHERE id =".$_POST['id'];
于 2013-07-24T18:00:44.090 回答
0

尝试这个:

$updateQuery = ("UPDATE `test1` SET f_name = '{$_POST['f_name']}', l_name='{$_POST['l_name']}', email='{$_POST['email']}' WHERE id='{$_POST['id']}'");

您也可以尝试在您的内部回显某些内容if (isset($_POST['update'])) {,以确保它正在测试真实。

这是你的问题:

if (isset($_POST['submit']) && $_POST['submit'] == 'update') {

提交按钮的名称是提交不更新,值是更新。

于 2013-07-24T17:55:51.077 回答