mac25 Posted January 23, 2010 Share Posted January 23, 2010 Hello, I'm trying to insert a posted string with miltiple delimeters into mysql. a string is posted in from a web page in the format aa1;bb1;cc1;dd1@|*aa2;bb2;cc2;dd2@|*.....etc the @|* delimits the rows, and ; delimts the fields in the rows. <?php $system_data = $_POST["v1"]; // clean up data $remove = array("] ", "player=", ";;", "- empty -"); $insert = array ("];","", ";Null;", "- empty -;- empty -"); $system_data_clean= str_replace($remove, $insert,"$system_data"); // $system_data_clean can be written to file ok // Connect to database $con = mysql_connect($databasehost,$databaseusername,$databasepassword); if (!$con) { die('Could not connect: ' . mysql_error()); } foreach (explode('@|*',$system_data_clean) as $system_line) { $line_array = explode (';', $system_line); //generate query $sql = mysql_query("INSERT INTO 'ae_system' VALUES ('".mysql_real_escape_string($line_array)."')") or trigger_error (mysql_error()); mysql_query($sql); } mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/189530-nested-explode-mysql-insert/ Share on other sites More sharing options...
jacksonmj Posted January 23, 2010 Share Posted January 23, 2010 $sql = mysql_query("INSERT INTO 'ae_system' VALUES ('".mysql_real_escape_string($line_array)."')") or trigger_error (mysql_error()); You are implicitly converting an array into a string here. This will always give the result "Array". (If a SQL query is failing, try printing the query and having a look to see if anything obvious is wrong with it). Perhaps you need an implode()? ...mysql_real_escape_string(implode("','",$line_array))... Link to comment https://forums.phpfreaks.com/topic/189530-nested-explode-mysql-insert/#findComment-1000409 Share on other sites More sharing options...
mac25 Posted January 23, 2010 Author Share Posted January 23, 2010 Thanks for the response. I've tried the implode which I think I need to do, but it's not working. Any other suggestion gratefully received. Link to comment https://forums.phpfreaks.com/topic/189530-nested-explode-mysql-insert/#findComment-1000456 Share on other sites More sharing options...
mattal999 Posted January 23, 2010 Share Posted January 23, 2010 What does the mysql_error() output? Link to comment https://forums.phpfreaks.com/topic/189530-nested-explode-mysql-insert/#findComment-1000462 Share on other sites More sharing options...
mac25 Posted January 23, 2010 Author Share Posted January 23, 2010 Nothing in the log. This did prompt me to look in the PHP log though and this is reported: PHP Notice: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ae_system' VALUES ('')' So I guess the SQL statement is incorrect As you might have guessed i'm very new to this. Link to comment https://forums.phpfreaks.com/topic/189530-nested-explode-mysql-insert/#findComment-1000465 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.