if ($response_array[0] == 1) {
$table = payments_received;
function mysql_insert_array($table, $response_array) {
foreach ($response_array as $field=>$value) {
$fields[] = '`' . $field . '`';
$values[] = "'" . mysql_real_escape_string($value) . "'";
}
$field_list = implode(',', $fields);
$value_list = implode(', ', $values);
$query = "INSERT INTO `" . $table . "` (" . $field_list . ") VALUES (" . $value_list . ")";
if (!$query) {
$message = mysql_error();
die($message);
}
}
include('receipt.php');
}
Any ideas why this doesn’t work? I know the condition at the top is satisfied because the script goes on to include receipt.php (bottom of code). I know that $response_array has data because of this as well (plus I use the data from it in the receipt). I get no error output at all despite the condition under $query (by the way, my MySQL connection info is specified at the top of the script by including config.inc.php which is in perfect working order). I hope I’m not missing something glaringly obvious.
UPDATE #1:
As several of you pointed out I didn’t actually call mysql_query(as I feared I was missing some glaringly obvious stuff that goes to show that my brain is not functioning on a high level). I took Gus’ edits and tried them, fixing the unbalanced braces and adding mysql_query() (as Frank mentioned). Here’s what I have:
function mysql_insert_array($table, $response_array) { foreach ($response_array as $field=>$value) { $fields[] = '' . $field . ''; $values[] = "'" . mysql_real_escape_string($value) . "'"; } $field_list = implode(',', $fields); $value_list = implode(', ', $values);$field_list = rtrim($fieldlist,","); $value_list = rtrim($value_list,","); $query = mysql_query("INSERT INTO `" . $table . "` (" . $field_list . ") VALUES (" . $value_list . ")"); if (!$query) { $message = mysql_error(); die($message); }}
if ($response_array[0] == 1) {
$table = "payments_received";
mysql_insert_array($table, $response_array);
include('receipt.php');
} else {
include('declined.php');
}
I get further and at least get an error code, which is...
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 '0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,' at line 1
...I know that the problem is the trailing comma but I keep running into this problem today and have yet to figure out how to get rid of that damn extra comma in a situation like this. Ideas?
Thank you all for your help so far.
UPDATE #2
Answered my own question. I used rtrim($field_list,",") and rtrim($value_list,",") with a successful result. See updates to the code in UPDATE #1.... Or not! I just realized when I was pasting the code above that I had typoed and forgot the underscore in field_list in one place... I added it back and tested again and it didn't work. Same error as the one above. However when the typo remains in place the code works and does exactly what I want. WTF!?
FINAL UPDATE
I did what I wanted with the code below. I added ` around $field on line 3 of the code below (I don't know what that character is called).
function mysql_insert_array($table, $response_array) { foreach ($response_array as $field=>$value) { $fields[] = "$field"; $values[] = "'" . mysql_real_escape_string($value) . "'"; } $field_list = implode(',', $fields); $value_list = implode(', ', $values);$field_list = rtrim($field_list,","); echo "$field_list <br />"; $value_list = rtrim($value_list,","); echo "$value_list <br />"; $query = mysql_query("INSERT INTO `" . $table . "` ($field_list) VALUES (" . $value_list . ")"); if (!$query) { $message = mysql_error(); die($message); }}
if ($response_array[0] == 1) {
$table = "payments_received";
mysql_insert_array($table, $response_array);
include('receipt.php');
} else {
include('declined.php');
}
You haven’t actually executed $query, like with mysql_query($query);
🙂
Ryan’s probably right too, but this is specifically why your query isn’t working – as far as PHP is concerned, you’ve built the query and then not bothered to do anything productive with it.
Edit 1: Frank?! Man, way to show the love. 🙂
But I jest. You can do a couple of things; either use substr() to clip the trailing comma off the end and then append a ; in its place with normal string concatenation, or alternatively switch your do while loop for a for () loop and add an if-clause that selectively drops a “,” or a “;” in based on whether your counter is equal to the size of the source array minus one.
I’d do the substr one, personally.