Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Update: I forgot to mention that echo $matstring outputs '65.70', 'Coles','34 days','14' - which would appear to be the right syntax?

I'm a php/mysql newbie, and I think this is fairly basic, but having read all of the other stackoverflow questions on this topic and fiddling with different versions of my code for several hours I can't understand what I'm doing wrong. Would very much appreciate any help/suggestions. Aim: pass data from my php array ($matrix) into a mysql table

$matrix[1]=
( [0] => 65.70 [1] => Coles [2] => 34 days [3] => 14 )

$matrix[2]=
( [0] => 62.70 [1] => Coles [2] => 13 days [3] => 14 )

$matrix[3]=
( [0] => 12.70 [1] => Safeway [2] => 43 days [3] => 14 )

Code:

$matstring=implode("','",$matrix[1]);
$matstring="'".$matstring."'";
mysql_query('INSERT INTO Australia (Price, Company, Days, Weight) VALUES ('$matstring')');
share|improve this question
you can use serialize($data) before inserting and deserialize when pulling the data. or use json_encode and json_decode.. either way really. – skrilled Feb 24 at 20:54
That isn't the answer. Try changing mysql_query to echo, then checking the syntax. Most likely the extra ' around $matstring are meaning you are having '' around the values. – Rich Bradshaw Feb 24 at 20:56
oh nevermind i just read the title and assumed it was exactly what i was to expect.. and also using native queries seems a bad idea vs using PDO and easily making it something like $dbh->prepare("INSERT INTO Australia (Price, Company, Days, Weight) VALUES (?, ?, ?, ?)"); $stmt->execute($matrix[1]); – skrilled Feb 24 at 20:59

4 Answers

you're building the query wrong. it'll look like this:

INSERT INTO ... VALUES (''65.70,Coles,34 days,14'');

note how all 4 values are inside a SINGLE string.. but the string's also wrong ('').

you need to quote each individual value of the array:

('65.70', 'Coles', '34 days', '14')

THEN you implode it, giving you

INSERT INTO ... VALUES ('65.70', 'Coles', etc...)
share|improve this answer
I'm still a bit confused. When I echo $matstring with the code I posted, it outputs '65.70', 'Coles','34 days','14'. So isn't that exactly the syntax I require? Why can't I pass it into VALUES('$matstring')? – user2037290 Feb 24 at 21:08
Your $matstring is correct; however, when you use VALUES ('$matstring') in your SQL string, you are adding additional quotations around it, creating VALUES(''65.70','Coles','34 days',14'') – user18477575 Feb 25 at 0:42
mysql_query("INSERT INTO Australia (`Price`, `Company`, `Days`, `Weight`) VALUES ($matstring)");
share|improve this answer
I tried this correction but it still didn't work (script breaks) – user2037290 Feb 24 at 21:09
edited, try.... – QQQ Feb 24 at 21:17
and post error... – QQQ Feb 24 at 21:26

when i run this code :

$matrix = array();
$matrix[1] = array( 0 => 65.70, 1 => 'Coles', 2 => '34 days', 3 => 14 );
$matstring=implode("','",$matrix[1]);
$matstring="'".$matstring."'";
print "INSERT INTO Australia (`Price`, `Company`, `Days`, `Weight`) VALUES ($matstring)";

become result:

INSERT INTO Australia (`Price`, `Company`, `Days`, `Weight`) VALUES ('65.7','Coles','34 days','14')
share|improve this answer
up vote 0 down vote accepted

Corrected code:

$matstring=implode("','",$matrix[1]);

mysql_query("INSERT INTO Australia (Price, Company, Days, Weight) VALUES ('$matstring')");

*(i.e. delete the second line from original code and put double quotes around the argument of mysql_query)*

Appreciate user1847757's help - as she/he pointed out, $matstring itself was correct, but the single quotes inside of VALUES(' ') were being joined to the single quotes added to $matstring in the 2nd line of my original code, resulting in VALUES(''65.70','Coles','34 days','14'')

Thanks all for your help & suggestions

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.