简体   繁体   English

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

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

My foreach loop in PHP is inserting the wrong value.我在 PHP 中的 foreach 循环插入了错误的值。 I have a table that collects data regarding a book using html form with jquery.我有一个表格,它使用带有 jquery 的 html 表单收集有关一本书的数据。 In a table I was inserting array value, here is my code from the form.在我插入数组值的表中,这是我的表单代码。

form.php表单.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";
 }
 }

and here is my code from bookclass.php that I used to insert the data这是我用来插入数据的 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();
                  }

here is data that I am inserting in the table.这是我在表中插入的数据。

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

but here is what the result on the backend.但这是后端的结果。

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

I am not sure what is the problem.我不确定是什么问题。 Maybe someone can share their expert opinion on this matter I would gladly appreciate it.也许有人可以分享他们对此事的专家意见,我将不胜感激。 Thank you very much in advance.非常感谢您提前。

EDIT : I am able to insert the data on the first row correctly, here is what it looks like now.编辑:我能够在第一行正确插入数据,这是现在的样子。 The author from the first row was correct, but on the second one from "I", it now became "t".第一行的作者是正确的,但在第二行的“I”上,它现在变成了“t”。

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

Additional Edit:附加编辑:

So I add these code in form.php所以我在 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";
}
}

And here is what I got:这是我得到的:

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" 

Why are you using class members for your foreach loop?为什么在foreach循环中使用类成员? You can just do:你可以这样做:

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

You also have a lot of whitespaces surrounding $this-> calls.你也有很多围绕$this->调用的空格。 Your code is also XSS injectable, because you do not sanitize your input with htmlspecialchars or htmlentities .您的代码也是 XSS 可注入的,因为您没有使用htmlspecialcharshtmlentities清理您的输入。 But all of that aside.但所有这些都放在一边。 The best way of debugging PHP code is to use var_dump .调试 PHP 代码的最佳方式是使用var_dump Check first and foremost the $this->author by doing var_dump($this->author) .首先检查$this->author通过执行var_dump($this->author) I can unfortunately not tell you anymore because the code you've provided is not complete and to make a valid judgement and give you the help you need, you would need to provide the parts where you assign the author class member.不幸的是,我不能再告诉你了,因为你提供的代码不完整,为了做出有效的判断并为你提供所需的帮助,你需要提供分配给author类成员的部分。

Are you sure that the values that you're inserting to the database are truly arrays and not strings?您确定要插入数据库的值是真正的数组而不是字符串吗? Judging from your output, the values seem like strings, otherwise you wouldn't have gotten just characters.从您的输出来看,这些值看起来像字符串,否则您将不会只得到字符。

Ok Im posting an answer because I finally cracked it,好的,我发布了一个答案,因为我终于破解了它,

I just changed this :我只是改变了这个:

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();
              }

to this:对此:

              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