我正在上传表单并将其数据插入MySQL中的表中。主键是唯一必须唯一的字段。在下面的代码中,我检查错误号是否为1062(重复条目)并显示该案例的自定义消息。但是当我在表格中输入唯一数据时,我有时会收到错误。奇怪的是,它只是时不时出现,但我确信价值观不一样......
$success = mysqli_stmt_execute( $stmt );
$errorCode = mysqli_errno( $con );
if ($success) {
move_uploaded_file( $_FILES['photo']['tmp_name'], $uniquePath);
?>
<div id="response">
<p>Success! The user has been added to the database</p>
</div>
<?
} else if ($errorCode == 1062) {
?>
<div id="response">
<p>
The user could not be added to the database.
The ID already exists!
</p>
</div>
<?
} else {
?>
<div id="response">
<p>
The user could not be added to the database.
MySQL error code: <? echo $errorCode ?>
</p>
</div>
<?
}
?>
</body>
</html>
与SQL相关的代码如下所示:
$sql = "INSERT INTO users ( id, photo_path) VALUES (?, ?)";
$stmt = mysqli_prepare( $con, $sql );
mysqli_stmt_bind_param( $stmt, 'is', $_POST['id'], $uniquePath);
有什么简单的我在这里俯瞰吗?