【发布时间】:2016-09-01 15:23:19
【问题描述】:
我在 SQL Server 数据库中有一个表。我创建了一个“edit.php”,我们可以在其中更新数据库。使用 PDO 建立数据库连接。
我在表中有很多 Null 值。当我单击“编辑”选项时,表单会弹出。如果我完全填写记录,数据库就会更新。否则我会收到此错误消息。
SQLSTATE[42000]:[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]将数据类型 nvarchar 转换为数字时出错。
我的edit.php的代码如下:
require_once('database.php');
if (isset($_POST['btn_submit'])) {
$id = $_POST['txt_id'];
$site = $_POST['txt_site_code'];
$location = $_POST['txt_location_name'];
try {
$stmt = $conn->prepare("UPDATE MATRIX SET Site_Code=:site, Location_name=:location
WHERE OBJECTID =:id");
$stmt->execute(array(':site' => $site, ':location' => $location,':id' => $id));
if ($stmt) {
header('Location:index.php');
}
} catch (PDOException $e) {
echo $e->getMessage();
}
}
$object_id = '';
$site = '';
$location = '';
if (isset($_GET['id'])) {
$id = $_GET['id'];
$stmt = $conn->prepare("SELECT * FROM MATRIX WHERE OBJECTID=:id");
$stmt->execute(array(':id' => $id));
$row = $stmt->fetch();
$object_id = $row['OBJECTID'];
$site = $row['Site_Code'];
$location = $row['Location_name'];
}
?>
<h2>Edit the records</h2>
<form action="" method="post">
<table border="3px" cellpadding="5px">
<tr>
<td>Site Code</td>
<td><input type="text" name="txt_site_code" value="<?= $site; ?>"></td>
</tr>
<tr>
<td>Location Name</td>
<td><input type="text" name="txt_location_name" value="<?= $location; ?>"></td>
</tr>
<tr>
<td><input type="hidden" name="txt_id" value="<?= $object_id; ?>"></td>
<td><input type="submit" name="btn_submit" value="Submit"></td>
</tr>
</table>
</form>
感谢您的帮助。
【问题讨论】:
-
如果任何答案帮助您将其标记为正确。
标签: php sql-server edit