dongruoqiong9017 2011-04-13 04:14
浏览 27
已采纳

php - 无法将数组插入MySQL

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

  • 写回答

4条回答 默认 最新

  • dounei5721 2011-04-13 04:17
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染