PDO没有更新MYSQL

时间:2016-08-10 21:06:34

标签: php mysql pdo

绞尽脑汁,请帮忙。我想更新表格中14个字段中的7个。没有任何错误,但也没有发送数据到表。我是PDO的新手,过去两天一直在研究这个问题。我想要更新的字段使用带有POST的bindparm,其他字段值不会更改。谢谢你的帮助。

<html>
<form role="form" method="POST" action="update.php">
<div>
<label class="control-label">Contractor ID</label>
<input type="text" name="contractor_id"  />
</div>
<div>
<label class="control-label">Firstname</label>
<input type="text" name="firstname"  />
</div>
<div>
<label class="control-label">Lastname</label>
<input type="text" value="Lastname" name="lastname"  />
</div>
<div>
<label class="control-label">Address</label>
<input type="text" value="Address" name="address"  />
</div>
<div>
<label class="control-label">City</label>
<input type="text" value="City" name="city" />
</div>
<div>
<label class="control-label">State</label>
<input type="text" value="State" name="state" />
</div>
<div>
<label class="control-label">Zipcode</label>
<input type="text" value="Zipcode" name="zip" />
</div>
<div>
<label class="control-label">Email</label>
<input type="text" value="Email" name="email"  />
</div>
<div class="margiv-top-10">
<input type="submit" name="submit" class="email" value="Update" >
</div>
</form>
</html>

PHP     

try {
$pdo = new PDO($dsn, $user, $password);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected to database';
echo '<br>';

$sql = "UPDATE contractor_keys SET  
    contractor_id = :contractor_id,
    firstname = :firstname,
    lastname = :lastname,
    address = :address,
    city = :city,
    state = :state, 
    zip = :zip,
    email = :email,
    areacode = :areacode,
    phonenumber = :phonenumber,
    ssnumber = :ssnumber,
    rate = :rate,
    mykey = :mykey,
    passwd = :passwd

WHERE contractor_id = :contractor_id";

$stmt = $pdo->prepare($sql);  

$stmt->bindParam(':contractor_id', $contractor_id, PDO::PARAM_STR);                               
$stmt->bindParam(':firstname', $_POST['firstname'], PDO::PARAM_STR);       
$stmt->bindParam(':lastname', $_POST['lastname'], PDO::PARAM_STR);    
$stmt->bindParam(':address', $_POST['address'], PDO::PARAM_STR);
$stmt->bindParam(':city', $_POST['city'], PDO::PARAM_STR); 
$stmt->bindParam(':state', $_POST['state'], PDO::PARAM_STR);   
$stmt->bindParam(':zip', $_POST['zip'], PDO::PARAM_STR);  
$stmt->bindParam(':email', $_POST['email'], PDO::PARAM_STR); 
$stmt->bindParam(':areacode', $arecode, PDO::PARAM_INT); 
$stmt->bindParam(':phonenumber', $phonenumber, PDO::PARAM_STR); 
$stmt->bindParam(':ssnumber', $ssnumber, PDO::PARAM_STR); 
$stmt->bindParam(':rate', $rate, PDO::PARAM_INT); 
$stmt->bindParam(':mykey', $mykey, PDO::PARAM_STR); 
$stmt->bindParam(':passwd', $passwd, PDO::PARAM_STR); 
$stmt->execute(); 
echo 'Record Updated';

}

catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>

1 个答案:

答案 0 :(得分:0)

您应该$contractor_id替换$_POST["contractor_id"]。请参阅以下代码:

<强>之前

$stmt->bindParam(':contractor_id', $contractor_id, PDO::PARAM_STR); 

<强>后

$stmt->bindParam(':contractor_id', $_POST["contractor_id"], PDO::PARAM_STR); 

还有一件事。如果你在where条件下使用它,你不需要更新contractor_id字段。请参阅以下代码:

<强>之前:

$sql = "UPDATE contractor_keys SET  
    contractor_id = :contractor_id,
    firstname = :firstname,
    lastname = :lastname,
    address = :address,
    city = :city,
    state = :state, 
    zip = :zip,
    email = :email,
    areacode = :areacode,
    phonenumber = :phonenumber,
    ssnumber = :ssnumber,
    rate = :rate,
    mykey = :mykey,
    passwd = :passwd

WHERE contractor_id = :contractor_id";

<强>后:

$sql = "UPDATE contractor_keys SET  
    firstname = :firstname,
    lastname = :lastname,
    address = :address,
    city = :city,
    state = :state, 
    zip = :zip,
    email = :email,
    areacode = :areacode,
    phonenumber = :phonenumber,
    ssnumber = :ssnumber,
    rate = :rate,
    mykey = :mykey,
    passwd = :passwd

WHERE contractor_id = :contractor_id";