更新在数据库中不起作用

时间:2013-07-24 17:52:14

标签: php mysql mysqli

不介意安全问题,这只是本地测试,但是当我点击更新按钮时,没有任何更改通过页面或查询,我得到没有错误。

<?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>";
}

&GT;

3 个答案:

答案 0 :(得分: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') {

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

答案 1 :(得分: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>";
}

答案 2 :(得分:0)

POST键应该在引号中。试试这个:

$ updateQuery =“UPDATE test1 SET f_name =”。$ _ POST ['f_name']。“,l_name =。”$ _ POST ['l_name']。“,email =”。$ _ POST ['email']。“WHERE id =” $ _ POST [ 'ID']。