我尝试使用表单来更新sql表,首先获取其数据(editrower.php)并将其设置为表单中的值,然后使用表单更新表(update_contactrequest.php)但它返回说划船者已更新但表格没有更新。
editrower.php
<?php
require('login.php');
?>
<?php
include 'php/mysql_connect.php';
if(isset($_GET['id'])){
$q = $db->prepare('SELECT * FROM rowercontacts WHERE id=:id LIMIT 1');
$q->execute(array(':id'=>$_GET['id']));
$row = $q->fetch(PDO::FETCH_ASSOC);
if($row){
echo '
<form method="post" action="php/update_contactrequest.php"><div class="col-xs-9 col-md-6 col-lg-6">
<div class="form-group">
<input type="hidden" name="id" id="id" value="'.$_GET['id'].'">
<label for="firstname">First Name</label>
<input type"text" class="form-control" name="firstname" placeholder="First Name" value="'.$row['firstname'].'" />
</div>
<div class="form-group">
<label for="lastname">Last Name</label>
<input type="text" class="form-control" name="lastname" placeholder="Last Name" value="'.$row['lastname'].'" />
</div>
<br><br>
<br><br>
<input type="submit" class="btn btn-default" value="Update" />
</div></form>
';
}
else{
echo 'No rower found';
}
}
else{
echo 'No rower found';
}
?>
update_contactrequest.php:
<?php
session_start();
if($_SESSION['loggedIn'] == true){
$rower_id= $_POST['id'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
// connection to the database
try {
$bdd = new PDO('mysql:host=localhost;dbname=home','username','password');
} catch(Exception $e) {
exit('Unable to connect to database.');
}
// insert the records
$sql = "UPDATE rowercontacts SET firstname=:firstname, lastname=:lastname WHERE id=:rower_id";
$q = $bdd->prepare($sql);
if($q->execute(array(':firstname'=>$firstname, ':lastname'=>$lastname, ':rower_id'=>$id))){
echo '<script type="text/javascript">alert("Rower Updated.");location.href="../rowerlist.php";</script>';
}
else{
echo '<script type="text/javascript">alert("Something went wrong...");location.href="../rowerlist.php";</script>';
}
}
?>
答案 0 :(得分:1)
$q->rowCount()
。准备好的语句将返回受影响的行数。
如果查询本身没有错误且执行正常,则需要受影响的行。
$q = $bdd->prepare($sql);
if($q->execute(array(':firstname'=>$firstname, '...'))){
$updRows = $q->rowCount();
if($updRows==0){
echo '<script type="text/javascript">alert("Affected Rows = 0 !!!");location.href="../rowerlist.php";</script>';
}
else{
echo '<script type="text/javascript">alert("Rows affected : '.$updRows.'");location.href="../rowerlist.php";</script>';
}
}
else{
echo '<script type="text/javascript">alert("Something went wrong...");location.href="../rowerlist.php";</script>';
}
WHERE
不正确,其余部分来自尝试将record
替换为已存在的exactly the same values
。WHERE id = null
通常不是人们所期望的。对于您的问题我确定您可以在以下3行中找到错误的部分。 : - )
$rower_id= $_POST['id'];
....
$sql = "UPDATE rowercontacts ... WHERE id=:rower_id";
if($q->execute(array(':firstname'=>$firstname,...,':rower_id'=>$id)))