数组到字符串转换PHP显示错误

时间:2013-11-24 23:52:06

标签: sql database pdo insert

嘿伙计们,我正在尝试更新数据库中的复选框,并且必须使用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']);

我希望你们能帮忙!!

非常感谢!

: - )

1 个答案:

答案 0 :(得分:0)

您已在此处使用数组构造命名inSeason checkox

<input type="checkbox" name="inSeason[]" value="0" id="checkboxFour2Input" <?php if( isset( $_GET['inSeason'] ) ){ ?> checked="checked" <?php } ?> />

当PHP解码$ _GET数据时,这将显示为一个数组,但这里

if( array_key_exists( 'inSeason', $_GET ) && $_GET['inSeason'] == '0' ){

您正在针对字符串进行测试,因此会出现数组到sring错误消息。

只需在您的HTML中重命名复选框,如下所示:

<input type="checkbox" name="inSeason" value="0" id="checkboxFour2Input" <?php if( isset( $_GET['inSeason'] ) ){ ?> checked="checked" <?php } ?> />
                                    ^^^ remove square brackets here.