Jump to content

Nested Explode mysql insert


mac25

Recommended Posts

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

$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))...

 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.