我正在尝试更新单个记录中的两个字段。但是,当我点击“更新”按钮并提交更改时,不会向assigned_to=$_POST['assigned']
,completed='_POST['completedTime']
或id='$_POST['ticket']
发布任何内容。我想当我点击“更新”时,它会查看名为“已分配”/“已完成时间”/和“票证”的每个字段。如何确保只选择一条记录?
<?php require_once('../con.php'); ?>
<?php
//query DB
$query = "SELECT * FROM table";
$results = mysql_query($query, $con) or die (mysql_error() . mysql_error($con));
$row = mysql_fetch_assoc($results);
$totalRows = mysql_num_rows($results);
//Update db
if (isset($_POST['update']))
{
$update = "UPDATE table SET assigned_to=$_POST['assigned'], completed='$_POST['completedTime'] WHERE id='$_POST['ticket']'";
mysql_query($update, $con);
if (!mysql_query($update, $con))
{
die('Error: ' . mysql_error($con));
}
}
?>
<!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></title>
<style type="text/css">
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
h1,h2,h3,h4,h5,h6 {
font-family: Arial, Helvetica, sans-serif;
}
h1 {
font-size: 18px;
}
h2 {
font-size: 14px;
}
</style>
</head>
<body>
<h1>Report</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" accept-charset="iso-8859-1">
<hr size="1" noshade="noshade" />
<table border="1" cellpadding="3" cellspacing="0" class="small">
<tr>
<td><strong>TicketNum</strong></td>
<td><strong>User</strong></td>
<td><strong>Category</strong></td>
<td><strong>Description</strong></td>
<th>Comments</th>
<td><strong>Date</strong></td>
<td><strong>AssignedTo</strong></td>
<td><strong>CompletedDate</strong></td>
</tr>
<?php do { ?>
<tr bgcolor="<?php
$colorarray=array('#FFFFFF','#D9ECFF');
$colorarraysize=count($colorarray);
if ($BRB_rowcounter++%1 == 0){
if ($colorarrayindex<($colorarraysize-1)){
$colorarrayindex++;
}else{
$colorarrayindex=0;
}
}
echo "$colorarray[$colorarrayindex]"; ?>">
<td valign="top" nowrap="nowrap"><?php echo $row['id']; ?></td>
<td valign="top" nowrap="nowrap"><?php echo $row['username']; ?></td>
<td valign="top" nowrap="nowrap"><?php echo $row['category']; ?></td>
<td valign="top" nowrap="nowrap"><?php echo $row['title']; ?></td>
<td valign="top"><?php echo $row['issue_details']; ?></td>
<td valign="top" nowrap="nowrap"><?php echo $row['timestamp']; ?></td>
<td valign="top" nowrap="nowrap"><input type="text" name="assigned" value ='<?php echo $row['assigned_to']; ?>'></td>
<td valign="top" nowrap="nowrap"><input type="text" name="completedTime" value ='<?php echo $row['completed']; ?>'></td>
<td><input type="hidden" name="ticket" value ='<?php echo $row['id']; ?>'></td>
<td><input type="submit" name="update" value="Update"></td>
</tr>
<?php } while ($row = mysql_fetch_assoc($results)); ?>
</table>
</form>
</body>
</html>
<?php
mysql_free_result($update);
mysql_free_result($results);
?>
答案 0 :(得分:1)
试一试:
<?php
require_once('../con.php');
//Update db
if (isset($_POST['update']))
{
$update = "UPDATE table SET assigned_to='{$_POST['assigned']}', completed='{$_POST['completedTime']}' WHERE id='{$_POST['ticket']}'";
$result = mysql_query($update, $con) or die ('Error' . mysql_error($con));
}
//query DB
$query = "SELECT * FROM table";
$results = mysql_query($query, $con) or die (mysql_error() . mysql_error($con));
$totalRows = mysql_num_rows($results);
?>
<!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></title>
<style type="text/css">
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
h1,h2,h3,h4,h5,h6 {
font-family: Arial, Helvetica, sans-serif;
}
h1 {
font-size: 18px;
}
h2 {
font-size: 14px;
}
</style>
</head>
<body>
<h1>Report</h1>
<hr size="1" noshade="noshade" />
<table border="1" cellpadding="3" cellspacing="0" class="small">
<tr>
<td><strong>TicketNum</strong></td>
<td><strong>User</strong></td>
<td><strong>Category</strong></td>
<td><strong>Description</strong></td>
<th>Comments</th>
<td><strong>Date</strong></td>
<td><strong>AssignedTo</strong></td>
<td><strong>CompletedDate</strong></td>
</tr>
<?php
while ($row = mysql_fetch_assoc($results)) {
$colorarray = array("#FFFFFF","#D9ECFF");
$colorarraysize = count($colorarray);
if ($BRB_rowcounter++%1 == 0){
if ($colorarrayindex<($colorarraysize-1)){
$colorarrayindex++;
}else{
$colorarrayindex=0;
}
}
$content .= <<< END
<form action="" method="POST" accept-charset="iso-8859-1">
<tr bgcolor="{$colorarray[$colorarrayindex]}">
<td valign="top" nowrap="nowrap">{$row['id']}</td>
<td valign="top" nowrap="nowrap">{$row['username']}</td>
<td valign="top" nowrap="nowrap">{$row['category']}</td>
<td valign="top" nowrap="nowrap">{$row['title']}</td>
<td valign="top">{$row['issue_details']}</td>
<td valign="top" nowrap="nowrap">{$row['timestamp']}</td>
<td valign="top" nowrap="nowrap"><input type="text" name="assigned" value="{$row['assigned_to']}"></td>
<td valign="top" nowrap="nowrap"><input type="text" name="completedTime" value="{$row['completed']}"></td>
<td><input type="submit" name="update" value="Update"></td>
</tr>
<input type="hidden" name="ticket" value="{$row['id']}">
</form>
END;
}
echo $content;
mysql_free_result($results);
?>
</table>
答案 1 :(得分:0)
您的字符串插值语法不正确。它应该是:
$update = "UPDATE table SET assigned_to='{$_POST['assigned']}', completed='{$_POST['completedTime']}' WHERE id='{$_POST['ticket']}'";
如果索引包含引号,则需要将{ }
放在数组引用周围。您也可以省略引号:
$update = "UPDATE table SET assigned_to='$_POST[assigned]', completed='$_POST[completedTime] WHERE id='$_POST[ticket]'";
你也有一些缺失的引号和美元符号,我已在此处修复过。
有关字符串语法的详细信息,请参阅PHP documentation。
然而,这是非常糟糕的风格,因为它受SQL注入。您最好使用支持预准备查询的API(PDO,mysqli);如果没有,请将$_POST
参数分配给变量,并调用mysql_real_escape_string()
来清理它们。