c500mg Posted February 17, 2020 Share Posted February 17, 2020 (edited) 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 February 17, 2020 by c500mg Quote Link to comment https://forums.phpfreaks.com/topic/310061-insert-data-from-a-string-into-mysql-table-as-rows/ Share on other sites More sharing options...
requinix Posted February 18, 2020 Share Posted February 18, 2020 Have you considered trying to use mysqli_error() to find out what went wrong? Quote Link to comment https://forums.phpfreaks.com/topic/310061-insert-data-from-a-string-into-mysql-table-as-rows/#findComment-1574453 Share on other sites More sharing options...
c500mg Posted February 18, 2020 Author Share Posted February 18, 2020 Yes I also tried echo "<br>ERROR: $sql.".mysqli_error($connect); Quote Link to comment https://forums.phpfreaks.com/topic/310061-insert-data-from-a-string-into-mysql-table-as-rows/#findComment-1574454 Share on other sites More sharing options...
mac_gyver Posted February 18, 2020 Share Posted February 18, 2020 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.) Quote Link to comment https://forums.phpfreaks.com/topic/310061-insert-data-from-a-string-into-mysql-table-as-rows/#findComment-1574456 Share on other sites More sharing options...
c500mg Posted February 18, 2020 Author Share Posted February 18, 2020 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!! Quote Link to comment https://forums.phpfreaks.com/topic/310061-insert-data-from-a-string-into-mysql-table-as-rows/#findComment-1574457 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.