Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4272010
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:31:12+00:00 2026-05-21T07:31:12+00:00

if ($response_array[0] == 1) { $table = payments_received; function mysql_insert_array($table, $response_array) { foreach ($response_array

  • 0
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');
}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:31:13+00:00Added an answer on May 21, 2026 at 7:31 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the oledb provider. Query is something like this: SELECT appid FROM table WHERE
I am storing the response to various rpc calls in a mysql table with
From a DB2 table I've got blob which I'm converting to a byte array
I've a table in which addition rows can be generatred as per the user
I have a table that I cannot get to size correctly. The table populates
My html of form is like this <form id=form method=post action=func.php> <table> <!--- DATA
I have this response.write on my page function roundthecon() { document.write(Math.round(exchRate*Math.pow(10,2))/Math.pow(10,2)); } But it
My MySQL DB table holds a BLOB image with image_id as key. I am
How to handle a array response from .getjson. Following code handles ajax request. function
I have a table exercise_results. People put in their results at the beginning and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.