【问题标题】:Array to string conversion PHP is showing error数组到字符串转换 PHP 显示错误
【发布时间】: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-&gt;bindValue(':inSeason', $_POST['inSeason']);

希望大家帮忙!!

非常感谢!

:-)

【问题讨论】:

  • 使用方括号名称后缀表示法,即name="inSeason[]" 导致 PHP 将 POST 数据值解释为数组,因此数组到字符串的转换

标签: sql database pdo insert


【解决方案1】:

您已在此处使用数组结构命名您的 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' ){

您正在针对一个字符串进行测试,因此会出现 array-to-sring 错误消息。

只需像这样在 HTML 中重命名复选框:

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

【讨论】:

  • 非常感谢 mike 摆脱了我的错误 ok :-) 你能看出我在 sql 语句中插入值的方式有什么问题吗?或绑定值,都在更新 bar inSeason col.
猜你喜欢
  • 2013-08-24
  • 2014-07-31
  • 2016-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
相关资源
最近更新 更多