编辑:我几乎找到了一个解决办法,但我还不太清楚。我准备好了 $statement->bindValue(':ID', $row['ID']);
但现在我正在更新数据库中的所有行,而不是所选的一行。我知道这与foreach循环有关,但是现在我只需要更新一个选中的行。
目前,我在pdo中有一个mysql update查询,当我指定一个$id时,它运行得非常好。但是,我想修改update查询,以便$id可以是表中的任何一行。目前,当我设置$id='132612'时,下面的查询工作:
foreach ($result as $row)
{
$eventprofileid = ''. $row['ID'] .'';
}
if (isset($_POST['verify'])) {
$host = '';
$user = '';
$pass = '';
$database = '';
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false
);
$pdo = new PDO("mysql:host=$host;dbname=$database", $user, $pass, $options);
$sql = "UPDATE `EVENT_DATA` SET `finishReason` = :finishreason WHERE ID = :ID";
$statement = $pdo->prepare($sql);
$id = '132612'; // -----this is what I want to change to a variable, which is whatever $eventprofileid is equal to.
$finishreason2 = '1';
$statement->bindValue(':ID', $row['ID']); ---edited code, problem is now this updates all rows instead of one row.
$statement->bindValue(':finishreason', $finishreason2);
$update = $statement->execute();
}
<form action='' method='POST'>
<input type='submit' name='verify' value='Verify' />
现在,如何使用相同的查询,而不是指定$id,而是将其设置为变量?我尝试了下面的代码,但没有成功。我肯定知道我在那里做错了什么,部分原因是我对pdo越来越熟悉:
$statement->bindValue(':ID', $row['ID']); --Problem is now this updates all rows instead of one.
1条答案
按热度按时间xytpbqjk1#
这可能不太可能,但我认为您每次都试图用不同的id多次执行一个准备好的语句。
试着替换你的
execute
用foreach循环。