更新现有条目

时间:2017-09-12 09:57:31

标签: php html mysql

我正在尝试创建一个检索所有者姓名的页面,并让我选择输入我对其操作的评论。使用适当选定人员的时间戳发送评论。

我可以以查看格式检索“所有者”名称,但在提交我的评论时遇到问题,因为它更新了“评论”的所有列,而不是仅将其添加到所选条目(可能是'id' 'issue)字段和时间戳无法正确捕获,它会添加一个带有空白字段但具有时间戳值的新行。

这是我到目前为止所做的:

<?php
function renderForm($id, $owner, $attendance, $comments, $log, $error)
{
?>
<!DOCTYPE html>
<html>
<head>
<title>Log</title>
</head>
<body>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<strong>OWNER: </strong><?php echo $owner; ?><br/><br/>

<strong>ATTENDANCE: </strong><select name="attendance" id="attendance">
            <option value=""><?php echo $status; ?></option>
            <option value = "Present">Ongoing</option>   
            <option value = "Absent">Closed</option>   
    </select><br/><br/>

<div style="position: absolute; top: 200px; left: 300px; width: 210px; height: 125px;">
<strong>COMMENTS: </strong><br/>
<?php 
// connect to the database
$conn = mysqli_connect("localhost", "root", "");
$result = mysqli_query($conn, "SELECT * FROM arc.log WHERE attendance IN ('Present', 'Absent')", MYSQLI_USE_RESULT)
    or die(mysql_error());
echo "<table border='1' cellpadding='10'>";
echo "<font color=\"white\"><tr><th>TIMESTAMP</th><th>COMMENTS</th></tr>";
while($row = mysqli_fetch_array( $result )) {   
echo "<tr>";
echo '<td>' . $row['timestamp'] . '</td>';
echo '<td>' . $row['comments'] . '</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?></div>

<div style="position: absolute; top: 8px; left: 700px; width: 210px; height: 125px;">
<strong>COMMENTS: </strong> <br/><textarea id="comments" name="comments" rows="10" cols="50"></textarea></div>
</div>
<br/>
<br/>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

<?php
}
$conn = mysqli_connect("localhost", "root", "");
if (isset($_POST['submit']))
{
if (is_numeric($_POST['id']))
{
$comments = mysqli_real_escape_string($conn, $_POST['comments']);
$timestamp_history = date("Y-m-d h:i:s");
if ($comments == '')
{
$error = 'ERROR: Please fill in all required fields!';
renderForm($remarks, $error);
}
else
{
mysqli_query($conn, "UPDATE arc.log SET comments='$comments'")
or die(mysql_error());
$query = "INSERT INTO `ticket`(`timestamp`) VALUES ('$timestamp')";
$result = mysqli_query($conn, $query);
}
}
else
{
echo 'Error1';
}
}
else
{
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
$id = $_GET['id'];
$result = mysqli_query($conn, "SELECT * FROM arc.log WHERE id=$id")
or die(mysql_error());
$row = mysqli_fetch_array($result);
if($row)
{
$owner = $row['owner'];
$attendance = $row['attendance'];
$comments = $row['comments'];
renderForm($id, $owner, $attendance, $comments, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
{
echo 'Error2';
}
}
?>

MySQL的:

enter image description here

2 个答案:

答案 0 :(得分:0)

UPDATE arc.log SET comments='$comments' WHERE id = $_POST['id']

请注意,您应该转义POST参数,而不是盲目地复制它。

了解时间戳问题。如果您不想插入新记录,而是更新现有记录,请使用UPDATE而不是INSERT

答案 1 :(得分:0)

- 它会更新“评论”的所有列 - 必须通过替换

进行更正
 mysqli_query($conn, "UPDATE arc.log SET comments='$comments'"); by 
 $id = $_POST['id']; 
 mysqli_query($conn, "UPDATE arc.log SET comments='".$comments."',log.timestamp = '".$timestamp_history."'  where id=$id ");  

如果colums时间戳的类型是时间戳,则必须将datetime转换为时间戳,如下所示: 你必须在这一行后添加

$timestamp_history = date("Y-m-d h:i:s"); 
$timestamp_history =  strtotime($timestamp_history);