如何在php上一次更新一个字段中的行?

时间:2014-10-04 13:41:51

标签: php mysql phpmyadmin

我在更新tblitemlist上的状态字段时遇到问题。我想显示所有status ='PENDING',当它完成时,我想要做的是只需单击一个按钮就可以将其状态更新为已完成。我一个接一个地想要它而不是单击按钮并同时更新所有内容。请帮帮我

这就是我所做的:

$query_update = mysql_query("UPDATE tblitemlist SET status = 'FINISHED'") or die(mysql_error());

我试过这个。没有错误但是当我检查我的数据库时,状态没有改变。

$id=$_GET['id'];

    $query_update = mysql_query("UPDATE tblitemlist SET status = 'FINISHED' WHERE id='$id'") or die(mysql_error());

要显示我的代码..

inner.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php

include("../connection/connection.php");
//
if(!isset($_SESSION['u']))
{ 
header("location../resto/index.php");

$result = mysql_query("SELECT tblcustomer.fname, tblitemlist.item, tblitemlist.category, tblitemlist.date_ordered FROM tblitemlist INNER JOIN tblcustomer ON tblcustomer.cust_id=tblitemlist.cust_id WHERE tblitemlist.status='PENDING' ORDER BY tblcustomer.cust_id" ) or die(mysql_error());
echo "<table width='900' border='1' align='center'>";
echo "<tr>";
//echo "<td>Code</td>";
echo "<td align='center' style='font-size:20px;'>Name</td>";
echo "<td align='center' style='font-size:20px;'>Item</td>";
echo "<td align='center' style='font-size:20px;'>Category</td>";
echo "<td align='center' style='font-size:20px;'>Date</td>";
echo "<td align='center' style='font-size:20px;'>Status</td>";

echo "<tr>";

while ($row=mysql_fetch_array($result))
{
    echo "<tr>";
    //echo "<td>" .$row['code'];
    echo "<td>" .$row['fname'];
    echo "<td>" .$row['item'];
    echo "<td>" .$row['category'];
    echo "<td>" .$row['date_ordered'];



?>

<td><a href="update.php?>$id=<?php echo $row['id'];?>">DONE</a></td>
<?php
echo "<tr>";

}

echo "</table>";
}
?>
</p>
</body>
</html>

update.php

<?php error_reporting(0); ?>  

<?php 

include ("../connection/connection.php");  
$id=$_GET['id'];

        $query_update = mysql_query("UPDATE tblitemlist SET status='FINISHED' WHERE id='$id'") or die(mysql_error());

        if($query_update){
            echo '<script type="text/javascript">alert("COOKED");
            window.location="index.php";
            </script>';
        }
        else{
            echo '<script type="text/javascript">alert("UNSUCCESSFULL");
            window.location="index.php";
            </script>';
        }





?>

1 个答案:

答案 0 :(得分:1)

您必须添加WHERE语句以指定必须更新的表项。首先,您必须使用ID提供查询。例如,每个按钮都会发布&#39; id_to_update&#39;通过表格。

$query_update = mysql_query("UPDATE tblitemlist SET status = 'FINISHED' WHERE id = ".$_POST['id_to_update']) or die(mysql_error());

这就是你想要的吗?