My foreach loop in PHP is inserting the wrong value. I have a table that collects data regarding a book using html form with jquery. In a table I was inserting array value, here is my code from the form.
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()){
echo "saved";
}
else{
echo "not saved";
}
}
and here is my code from bookclass.php that I used to insert the data
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".
+-----------+---------------+-------+--------------+------------+-----------+
| 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
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"
$this-> book_titlecontain?