繁体   English   中英

为什么我的 PHP foreach 循环在 mysql 上插入了错误的值?

[英]why does my PHP foreach loop is inserting wrong value on mysql?

我在 PHP 中的 foreach 循环插入了错误的值。 我有一个表格,它使用带有 jquery 的 html 表单收集有关一本书的数据。 在我插入数组值的表中,这是我的表单代码。

表单.php

include_once 'config/database.php';
include_once 'config/bookclass.php';

$database = new Database();
$db = $database->getConnection();

$book = new Book($db);

if($_POST){

      $book->book_title=$_POST['book_title'];
      $book->number_of_ page=$_POST['number_of_ page'];
      $book->genre=$_POST['genre'];
      $book->author=$_POST['author'];
      $book->type=$_POST['type'];
      $book->publication=$_POST['publication'];

      if($book->book()){
        echo "saved";
      }
      else{
      echo "not saved";
 }
 }

这是我用来插入数据的 bookclass.php 代码

public function book(){

foreach($this-> book_title AS $this->key => $this->value) {

                  $query = "INSERT INTO

                    table_a 

                    SET
                    book_title=:book_title,
                    number_of_ page = :number_of_page,
                    genre = :genre,
                    author = :author,
                    type = :type,
                    publication = :publication";

                    $stmt = $this->conn->prepare($query);



                    $this->book_title=$this->value;
                    $this->number_of_ page= $this->number_of_ page[$this->key];
                    $this->genre = $this->genre[$this->key];
                    $this-> author= $this-> author[$this->key];
                    $this-> type= $this-> type[$this->key];
                    $this-> publication = $this->publication[$this->key];

                    $stmt->bindParam(':book_title', $this->book_title);
                    $stmt->bindParam(':number_of_ page', $this->number_of_ page);
                    $stmt->bindParam(':genre ', $this->genre );
                    $stmt->bindParam(':author', $this->author);
                    $stmt->bindParam(': type', $this-> type);
                    $stmt->bindParam(':publication', $this->publication);

                   $stmt->execute();
                  }

这是我在表中插入的数据。

 +-----------+---------------+-------+--------------+------------+-----------+
 | book_title|number_of_ page| genre | author       | type       |publication|
 +-----------+---------------+-------+--------------+------------+-----------+
 | carrie    |     199       | horror| stephen king | softcover  |   1974    |
 +-----------+---------------+-------+--------------+------------+-----------+
 | dune      |     412       | scifi | frank herbert| hardcover  |   1965    |

但这是后端的结果。

 +-----------+---------------+-------+--------------+------------+-----------+
 | book_title|number_of_ page| genre | author       | type       |publication|
 +-----------+---------------+-------+--------------+------------+-----------+
 | carrie    |     199       | horror| H            | softcover  |   1974    |
 +-----------+---------------+-------+--------------+------------+-----------+
 | dune      |     9         | o     | I            | o          |   9       |

我不确定是什么问题。 也许有人可以分享他们对此事的专家意见,我将不胜感激。 非常感谢您提前。

编辑:我能够在第一行正确插入数据,这是现在的样子。 第一行的作者是正确的,但在第二行的“I”上,它现在变成了“t”。

 +-----------+---------------+-------+--------------+------------+-----------+
 | book_title|number_of_ page| genre | author       | type       |publication|
 +-----------+---------------+-------+--------------+------------+-----------+
 | carrie    |     199       | horror| stephen king | softcover  |   1974    |
 +-----------+---------------+-------+--------------+------------+-----------+
 | dune      |     9         | o     | t            | o          |   9       |

附加编辑:

所以我在 form.php 中添加这些代码

include_once 'config/database.php';
include_once 'config/bookclass.php';

$database = new Database();
$db = $database->getConnection();

$book = new Book($db);

if($_POST){

  $book->book_title=$_POST['book_title'];
  $book->number_of_ page=$_POST['number_of_ page'];
  $book->genre=$_POST['genre'];
  $book->author=$_POST['author'];
  $book->type=$_POST['type'];
  $book->publication=$_POST['publication'];

  if($book->book()){
        var_dump ($value);
        var_dump($book->number_of_ page[$key]);
        var_dump($book->genre[$key]);
        var_dump($book->author[$key]);
        var_dump($book->type[$key]);
        var_dump($book->publication[$key]);
  }
  else{
  echo "not saved";
}
}

这是我得到的:

Notice: Undefined variable: value in /var/www/html/library/form.php on line 106
NULL 
Notice: Undefined variable: key in /var/www/html/library/form.php on line 107

Notice: String offset cast occurred in /var/www/html/library/form.php on line 107
string(1) "9" 
Notice: Undefined variable: key in /var/www/html/library/form.php on line 108

Notice: String offset cast occurred in /var/www/html/library/form.php on line 108
string(1) "t" 
Notice: Undefined variable: key in /var/www/html/library/form.php on line 109

Notice: String offset cast occurred in /var/www/html/library/form.php on line 109
string(1) "o" 
Notice: Undefined variable: key in /var/www/html/library/form.php on line 110

Notice: String offset cast occurred in /var/www/html/library/form.php on line 110
string(1) "o" 
Notice: Undefined variable: key in /var/www/html/library/form.php on line 111

Notice: String offset cast occurred in /var/www/html/library/form.php on line 111
string(1) "9" 

为什么在foreach循环中使用类成员? 你可以这样做:

foreach ($this->book_title as $key => $value)

你也有很多围绕$this->调用的空格。 您的代码也是 XSS 可注入的,因为您没有使用htmlspecialcharshtmlentities清理您的输入。 但所有这些都放在一边。 调试 PHP 代码的最佳方式是使用var_dump 首先检查$this->author通过执行var_dump($this->author) 不幸的是,我不能再告诉你了,因为你提供的代码不完整,为了做出有效的判断并为你提供所需的帮助,你需要提供分配给author类成员的部分。

您确定要插入数据库的值是真正的数组而不是字符串吗? 从您的输出来看,这些值看起来像字符串,否则您将不会只得到字符。

好的,我发布了一个答案,因为我终于破解了它,

我只是改变了这个:

public function book(){

foreach($this-> book_title AS $this->key => $this->value) {

              $query = "INSERT INTO

                table_a 

                SET
                book_title=:book_title,
                number_of_ page = :number_of_page,
                genre = :genre,
                author = :author,
                type = :type,
                publication = :publication";

                $stmt = $this->conn->prepare($query);

                $this->book_title=$this->value;
                $this->number_of_ page= $this->number_of_ page[$this->key];
                $this->genre = $this->genre[$this->key];
                $this-> author= $this-> author[$this->key];
                $this-> type= $this-> type[$this->key];
                $this-> publication = $this->publication[$this->key];

                $stmt->bindParam(':book_title', $this->book_title);
                $stmt->bindParam(':number_of_ page', $this->number_of_ page);
                $stmt->bindParam(':genre ', $this->genre );
                $stmt->bindParam(':author', $this->author);
                $stmt->bindParam(': type', $this-> type);
                $stmt->bindParam(':publication', $this->publication);

               $stmt->execute();
              }

对此:

              public function book(){

    foreach($this-> book_title AS $key => $value) {

              $query = "INSERT INTO

                table_a 

                SET
                book_title=:book_title,
                number_of_ page = :number_of_page,
                genre = :genre,
                author = :author,
                type = :type,
                publication = :publication";

                $stmt = $this->conn->prepare($query);

                $this->book_title=$value;
                $this->number_of_ page= $this->number_of_ page;
                $this->genre = $this->genre;
                $this-> author= $this-> author;
                $this-> type= $this-> type;
                $this-> publication = $this->publication;

                $stmt->bindParam(':book_title', $value);
                $stmt->bindParam(':number_of_ page', $this->number_of_ page[$key]);
                $stmt->bindParam(':genre ', $this->genre[$key] );
                $stmt->bindParam(':author', $this->author[$key]);
                $stmt->bindParam(': type', $this-> type[$key]);
                $stmt->bindParam(':publication', $this->publication[$key]);

               $stmt->execute();
              }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM