我發現了一個addrecord.php
我已經編輯添加基於現有id
值,這addrecord.php
是用來值添加到它已經存在的記錄的另一列,添加值基於現有ID
樣品,從下面的表中,Column1和Column2已經有一個現有的值,我想通過向Column3添加一個值爲New3的值來編輯表,但它似乎沒有更新表。
Column1 | Column2 | Column3 | Column4
Exist1 | Exist2 | New3 | New4
我曾嘗試使用編輯選項,而不是增加基於現有id
的記錄,但問題還是一樣,不增加新的記錄。
這裏是我的HTML,
<tr>
<td>New3:<strong><span style="color: #ff0000;">*</span></strong></td>
<td colspan="2"><input type="text" name="new3" style="width: 220px;" value=""/></td>
</tr>
<tr>
<td>new4:<span style="color: #ff0000;"><strong>*</strong></span></td>
<td colspan="2"><input type="text" name="new4" style="width: 220px;" value="<?php echo $_SESSION['name']; ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>new5:<span style="color: #ff0000;"><strong>*</strong></span></td>
<td colspan="2"><select name="new5" style="width: 224px;">
<option value="" selected="selected">Please select...</option>
<option value="New5Option">New5Option</option>
<option value="New5Option2">New5Option2</option>
<option value="New5Option3">New5Option3</option>
<option value="New5Option4">New5Option4</option>
</select></td>
</tr>
這是我的PHP,
if (isset($_POST['submit']))
{
// get the form data
$id = $_POST['id'];
$new3 = $_POST['new3'];
$new4 = htmlentities($_POST['new4'], ENT_QUOTES);
$new5 = htmlentities($_POST['new5'], ENT_QUOTES);
// check that firstname and lastname are both not empty
if ($new3 == '' || $new4 == '' || $new5 == '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($new3, $new4, new5, $id);
}
else
{
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT testtable (new3, new4, new5) VALUES (?, ?, ?) WHERE id=?"))
{
$stmt->bind_param("sssi", $new3, $new4, $new5, $id);
$stmt->execute();
$stmt->close();
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare SQL statement.";
}
// redirect the user
header("Location: new_statuslist.php");
}
}
// if the form hasn't been submitted yet, show the form
else
{
renderForm();
}
// close the mysqli connection
$mysqli->close();
?>
「WHERE」子句作爲SQL的一部分無效INSERT ... VALUES '聲明。 SQL'INSERT'語句將一行(或多行)添加到表中。使用SQL UPDATE語句修改已存在的行。 (MySQL還提供了一個特殊的'INSERT ... ON DUPLICATE KEY UPDATE'語句,它可以方便用於某些用例。) – spencer7593