嘿傢伙我想更新我的數據庫中的複選框feild,並且必須捕獲和綁定使用SQL PDO中的值。數組到字符串轉換PHP顯示錯誤
但是我得到一個錯誤約一個數組串轉換,
我的複選框看起來像這樣 -
<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']);
我希望你們能幫助!
非常感謝!
:-)
使用方括號名後綴符號,即'名稱=「inSeason []」'導致PHP解釋POST數據值作爲一個數組,因此數組轉換爲字符串 – Phil