This question already has an answer here:
这个问题在这里已有答案:
- My PDO Statement doesn't work 1 answer
- Invalid parameter number, PDO on duplicate update fails 2 answers
我的PDO声明不起作用1回答
参数号无效,重复更新时PDO失败2个答案
<?php
if(isset($_POST['submit'])){
$_POST = array_map( 'stripslashes', $_POST );
extract($_POST);
if($eventName ==''){
$error[] = 'Please enter the Event Name.';
}
if(!isset($error)){
try {
$stmt = $db->prepare('UPDATE event SET eventID = :eventID, eventName = :eventName, eventTime = :eventTime, eventLocation = :eventLocation, postDate = :postDate WHERE eventID = :eventID') ;
$stmt->execute(array(
':eventID' => $eventID,
':eventName' => $eventName,
':eventTime' => $eventTime,
':eventLocation' => $eventLocation,
':postDate' => date('Y-m-d H:i:s')
));
//redirect to index page
header('Location: view-event.php?action=updated');
exit;
} catch(PDOException $e) {
echo $e->getMessage();
}
}
}
?>
And
<?php
//check for any errors
if(isset($error)){
foreach($error as $error){
echo $error.'<br />';
}
}
try {
$stmt = $db->prepare('SELECT eventID,eventName, eventTime, eventLocation, postDate FROM event WHERE eventID = :eventID') ;
$stmt->execute(array(':eventID' => $_GET['eventID']));
$row = $stmt->fetch();
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
HTML Form
<form role="form" method="post" action=''>
<input type="hidden" class="form-control" placeholder="Enter Event Name" name="eventID" value='<?php echo $row['eventID'];?>'>
<input class="form-control" placeholder="Enter Event Name" name="eventName" value='<?php echo $row['eventName'];?>'>
<input class="form-control" placeholder="Enter Event Time" name="eventTime" value='<?php echo $row['eventTime'];?>'>
<input class="form-control" placeholder="Enter Event Location" name="eventLocation" value='<?php echo $row['eventLocation'];?>'>
<input type="submit" class="btn btn-primary" name="submit" value="Edit Event">
</form>
Hi, Here is the code. I want to update data to mysql database. When I hit the button it just loaded to "view-event.php?action=updated" this page but nothing changes in database. Please help me.
嗨,这是代码。我想将数据更新到mysql数据库。当我点击按钮时,它只是加载到“view-event.php?action = updated”这个页面,但数据库中没有任何变化。请帮帮我。
1 个解决方案
#1
2
Ok, I finished my tests without the create table santax. If it still doesn't work, then it has to do with the table definition.
好的,我在没有create table santax的情况下完成了我的测试。如果它仍然不起作用,那么它与表定义有关。
Unless you want to do something else with the caught exceptions, other than displaying an error message to the user, you should remove the try-catch blocks. It will allow you to show a user-friendly error message/page in the future, by means of defining an error and an exception handler. While in the meantime PHP will just show you the error message and die: exactly what you need during the development. Take a look into this and this tutorial chapters in order to see how to apply the error and exception handling and reporting.
除非您想对捕获的异常执行其他操作,否则除了向用户显示错误消息之外,您应该删除try-catch块。它将允许您通过定义错误和异常处理程序在将来显示用户友好的错误消息/页面。与此同时,PHP只会向您显示错误消息并消亡:正是您在开发过程中所需要的。请查看本教程和本教程章节,以了解如何应用错误和异常处理和报告。
In the update statement you are using the :eventID
marker twice, but assigning the value of $eventID
once. In order to be able to do this you need to activate emulation, as part of the connection options. If you don't do this you are receiving the exception
在update语句中,您使用了:eventID标记两次,但是将$ eventID的值赋值一次。为了能够执行此操作,您需要激活仿真,作为连接选项的一部分。如果您不这样做,您将收到例外
SQLSTATE[HY093]: Invalid parameter number
So, put this in your connection options array:
所以,把它放在你的连接选项数组中:
PDO::ATTR_EMULATE_PREPARES => TRUE
Good luck.
#1
2
Ok, I finished my tests without the create table santax. If it still doesn't work, then it has to do with the table definition.
好的,我在没有create table santax的情况下完成了我的测试。如果它仍然不起作用,那么它与表定义有关。
Unless you want to do something else with the caught exceptions, other than displaying an error message to the user, you should remove the try-catch blocks. It will allow you to show a user-friendly error message/page in the future, by means of defining an error and an exception handler. While in the meantime PHP will just show you the error message and die: exactly what you need during the development. Take a look into this and this tutorial chapters in order to see how to apply the error and exception handling and reporting.
除非您想对捕获的异常执行其他操作,否则除了向用户显示错误消息之外,您应该删除try-catch块。它将允许您通过定义错误和异常处理程序在将来显示用户友好的错误消息/页面。与此同时,PHP只会向您显示错误消息并消亡:正是您在开发过程中所需要的。请查看本教程和本教程章节,以了解如何应用错误和异常处理和报告。
In the update statement you are using the :eventID
marker twice, but assigning the value of $eventID
once. In order to be able to do this you need to activate emulation, as part of the connection options. If you don't do this you are receiving the exception
在update语句中,您使用了:eventID标记两次,但是将$ eventID的值赋值一次。为了能够执行此操作,您需要激活仿真,作为连接选项的一部分。如果您不这样做,您将收到例外
SQLSTATE[HY093]: Invalid parameter number
So, put this in your connection options array:
所以,把它放在你的连接选项数组中:
PDO::ATTR_EMULATE_PREPARES => TRUE
Good luck.