【发布时间】:2013-12-09 13:33:19
【问题描述】:
大家好,我正在尝试更新数据库中的复选框字段,并且必须在使用 SQL PDO 时捕获和绑定值。
我收到一个关于数组到字符串转换的错误,
我的复选框看起来像这样 -
<div class="checkboxFour">
<p class="admin_label" id="checklabel2">In Seaon</p>
<input type="checkbox" name="inSeason[]" value="1" id="checkboxFourInput" checked="checked"<?php if( isset( $_GET['inSeason'] ) ){ ?> checked="checked" <?php } ?>/>
<label for="checkboxFourInput"></label>
</div>
<div class="checkboxFour">
<p class= "admin_label"id="checklabel2">Out Season</p>
<input type="checkbox" name="inSeason[]" value="0" id="checkboxFour2Input" <?php if( isset( $_GET['inSeason'] ) ){ ?> checked="checked" <?php } ?> />
<label for="checkboxFour2Input"></label>
</div>
在我的 PHP 中,我构建了以下内容 -
if (isset($_POST['updateProductSubmit'])) {
// open the database connection
include 'includes/connection.php';
$inSeasonValue = "0";
if( array_key_exists( 'inSeason', $_GET ) && $_GET['inSeason'] == '0' ){
$saleItemValue = "0";
echo $saleItemValue['inSeason'];
}
$inSeasonValue = "1";
if( array_key_exists( 'inSeason', $_GET ) && $_GET['inSeason'] == '1' ){
$saleItemValue = "1";
echo $saleItemValue['inSeason'];
}
try {
//create our sql update statement
$sql = "UPDATE Products SET productNbr = :productNbr, productName= :productName, inSeason = '$inSeasonValue', description = :description, photo =
:photo WHERE productNbr=:productNbr;";
// prepare the statement
$statement = $pdo->prepare($sql);
// bind the values
$statement->bindValue(':productNbr', $_POST['productNbr']);
$statement->bindValue(':productName', $_POST['productName']);
$statement->bindValue(':inSeason', $_POST['inSeason']);
$statement->bindValue(':description', $_POST['description']);
$statement->bindValue(':photo', $_POST['photo']);
// execute the statement
$success = $statement->execute();
} // end try
catch (PDOException $e) {
echo 'Error updating item: ' . $e->getMessage();
exit();
} // end catch
// test the result and display appropriate message
if ($success) {
echo '<script type="text/javascript"> alert("Successfully updated Item.")</script>';
}
else {
echo '<script type="text/javascript">alert("Unable to execute the Item update.");</script>';
}
//free connection to the server
$statement->closeCursor();
} // end if statement
?>
错误是指出绑定值$statement->bindValue(':inSeason', $_POST['inSeason']);
希望大家帮忙!!
非常感谢!
:-)
【问题讨论】:
-
使用方括号名称后缀表示法,即
name="inSeason[]"导致 PHP 将 POST 数据值解释为数组,因此数组到字符串的转换