Jump to content

insert data from a string into MySQL table as rows


c500mg

Recommended Posts

I was wondering if anyone could help me finding the error or why my script won't insert data into the table.

$str = '123456,123457,123458,123459,234567,234568,234569';
$str_arr = explode (",", $str);
echo '<br> $str_arr = ';
print_r($str_arr);
echo "<br><br><br>";

foreach ($str_arr as $value)
{
	$sql = 'INSERT INTO parts (part_serial) VALUES ("'.$value.'")';
        if(mysqli_query($connect, $sql)){
                echo "record inserted successfully.<br>";
        } else {
                echo "<br>ERROR: $sql.";
                print_r($connect->error);
        }
}

and i don't any error, if anyone could help me debug the code  this is the output:

$str_arr = Array ( [0] => 123456 [1] => 123457 [2] => 123458 [3] => 123459 [4] => 234567 [5] => 234568 [6] => 234569 )

ERROR: INSERT INTO parts (part_serial) VALUES ("123456").
ERROR: INSERT INTO parts (part_serial) VALUES ("123457").
ERROR: INSERT INTO parts (part_serial) VALUES ("123458").
ERROR: INSERT INTO parts (part_serial) VALUES ("123459").
ERROR: INSERT INTO parts (part_serial) VALUES ("234567").
ERROR: INSERT INTO parts (part_serial) VALUES ("234568").
ERROR: INSERT INTO parts (part_serial) VALUES ("234569").

and also recommend a software to write php for MacOS 

Edited by c500mg
Link to comment
Share on other sites

your connection probably doesn't exist and you would be getting a bunch of php errors.

you need to ALWAYS have php's error_reporting set to E_ALL and when learning, developing, and debugging code/queries, set display_errors to ON. these settings should be in the php.ini on your system.

next, you need to ALWAYS have error handling for all statements that can fail. for database statements - connection, query, prepare, and execute, the easiest way of adding error handling is to use exceptions and in most cases let php catch the exception where it will use its error related settings to control what happens with the actual error information (database errors will automatically get displayed or logged the same as php errors.)

Link to comment
Share on other sites

I wanted to thank you guys for your help, I had 3 days trying to figure it out and finally found the error, there is no issues in the code it was only that i hace the connection line deleted for some reason!!!

I just want to apologize for my terrible mistake... I am sorry

but I did turned ON display_errors for php... thank you for that too!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.