All the php code is below.
The script works fine up until the insert in to cinema database table part. I've echo'd the contents of the variables each run through the loop to ensure that they have all the correct data inside them. But for some reason some of the data is not being inserted in to the database even though it is contained in the variables just before the insert statement. There are 18 loops all together, but only 12 rows of data are being inserted in to the database. I thought maybe its too much data for one insert statement so tried limiting it to ten, then I planned on selecting the rest using the from id desc query, but of the first 10 only 7 inserted telling me that it must be the insert statement, not the database table. I have searched for a reason and tried lots of different things but still no joy. This is literally a last resort for me, as I am very much a DIY person and don't like asking for help. Thanks in advance for any help I get!
<?php
require('../php/connect.php');
$table = "CREATE TABLE IF NOT EXISTS cinema (
id int(20) NOT NULL AUTO_INCREMENT,
tags VARCHAR(200) NOT NULL,
title VARCHAR(200) NOT NULL,
description VARCHAR(300) NOT NULL,
image VARCHAR(100) NOT NULL,
link VARCHAR (200) NOT NULL,
PRIMARY KEY(id))";
$result = mysqli_query ($dbc, $table);
$select = "SELECT * FROM search";
$r = mysqli_query ($dbc, $select);
while ( $row = mysqli_fetch_array( $r , MYSQLI_ASSOC ))
{
$tags = $row['tags'];
$title = $row['title'];
$description = $row['description'];
$image = $row['image'];
$link = $row['link'];
echo $tags;
echo $title;
echo $description;
echo $image;
echo "$link <br />";
echo "one <br /><br />";
$insert = "INSERT INTO cinema (tags, title, description, image, link)
VALUES ('$tags', '$title', '$description', '$image', '$link')";
$run = mysqli_query ($dbc, $insert);
}
if($result) {
echo 'success';
}
else {
echo 'failed';
}
Regarding your code not inserting all data, it might be possible that the data which you are inserting might contain duplicate ID
which is a primary key so that particular insert statement is failing.
Suppose:- You are executing loop 10 times(10 rows), then it might be possible that out of 10 rows 3 rows contain duplicate id
(a primary key) so 3 insert statements are not getting executed.
Try this:
$r = mysqli_query ($dbc, $select);
while ( $row = mysqli_fetch_array( $r , MYSQLI_ASSOC ))
{
$tags = $row['tags'];
$title = $row['title'];
$description = $row['description'];
$image = $row['image'];
$link = $row['link'];
echo $tags;
echo $title;
echo $description;
echo $image;
echo "$link <br />";
echo "one <br /><br />";
$insert = "INSERT INTO cinema (tags, title, description, image, link)
VALUES ('$tags', '$title', '$description',
'$image', '$link')";
$run = mysqli_query ($dbc, $insert);
if($run) {
echo 'success';
}
else {
echo 'failed';
}
}
Use the LOW PRIORITY keyword in your insert statement to avoid redundancy when the data is large.
INSERT LOW PRIORITY INTO cinema('a','b','c') VALUES('x','y','c')