dq1230123 2014-02-11 23:50
浏览 39
已采纳

MySQL问题使用一个插入语句插入多行

Ok, I realize there are similar topics on this, many in fact, but none of them are helping me figure out my issue and it just seems to lead me farther and farther from my goal. I have tried implode, for each, loops, etc., but either I receive "Query was empty" because I am passing empty array or else I get a syntax error.

I am using mysqli prepared statements and here is what I have tried working with with no luck. Very stumped. Basically, I am inserting potentially many ingredient rows passed from an API I have built:

API URL string

menu_item.php?ingredient_name[]=bacon&ingredient_price[]=1.00&ingredient_default[]=0&ingredient_name[]=cheese&ingredient_price[]=0&ingredient_default[]=1

PHP

// set arrays, item_id, foreign key, already set from previous query
    $ingredient_name = $_GET['ingredient_name'];
    $ingredient_price = $_GET['ingredient_price'];
    $ingredient_default = $_GET['ingredient_default'];
    // define arrays
    $ingredients = array(
        'ingredient_name' => $ingredient_name,
        'ingredient_price' => $ingredient_price,
        'ingredient_default' => $ingredient_default
    );
    $insertQuery = array();
    $insertData = array();
    // set array length
    $len = count($ingredients);
    /**
    prepare the array values for mysql
    **/
    // prepare values to insert recursively
    $ingQuery = "INSERT INTO TABLE (column1,column2,column3,column4) VALUES ";
    // set placeholders
    foreach ($ingredients as $row) {
        $insertQuery[] = '(?,?,?,?)';
    }
    // iterate through all available data
    for( $i=0;$i<$len;$i++ ) {
        $insertData[] = $ingredients['ingredient_name'][$i];
        $insertData[] = $ingredients['ingredient_price'][$i];
        $insertData[] = $ingredients['ingredient_default'][$i];
    }
    // set ingredient value placeholders
    $ingQuery .= implode(', ', $insertQuery);
    // prepare statement ingredients
    $ingStmt = $mysqli->prepare($sql);
    // run the query
    if( $ingStmt ) {
        $ingStmt->execute($insertData);
    } else {
        // handle error return
        echo json_encode(array('error' => $mysqli->error.__LINE__));
        echo json_encode($insertData);
    }

For now ignore all that maybe I need to start over. Any suggestions? Currently I am receiving Query Was Empty error... I need to do this a better, more efficient way, but I am scratching my head on this!

EDIT (still in progress)

// the query
    $ingQuery = "INSERT INTO table (column1,column2,column3,column4) VALUES (?,?,?,?)";
    // prepare statement
    $ingStmt = $mysqli->prepare($ingQuery);
    if( $ingStmt ) {
        // iterate through all available data
        for( $i=0;$i<count($_GET['ingredient_name']);$i++ ) {
            $ingStmt->execute(array($item_id,$_GET['ingredient_name'][$i],$_GET['ingredient_price'][$i],$_GET['ingredient_default'][$i]));
        }
    } else {
        echo json_encode(array('error' => $mysqli->error.__LINE__));
    }
    $ingStmt->close();
  • 写回答

2条回答 默认 最新

  • duanlaiquan8174 2014-02-12 00:23
    关注

    First, you have 4 columns in your query - (column1,column2,column3,column4) VALUES (?,?,?,?), but you are only inserting 3 values $_GET['ingredient_name'], $_GET['ingredient_price'], $_GET['ingredient_default']

    Second, unless you need to reuse your arrays/variables, why not simplify it down to the prepare and execute in a loop -

    // the query
    $ingQuery = "INSERT INTO TABLE (column1,column2,column3) VALUES (?,?,?)";
    // prepare statement
    $ingStmt = $mysqli->prepare($ingQuery);
    // iterate through all available data
    for( $i=0;$i<count($_GET['ingredient_name']);$i++ ) {
        $ingStmt->execute(array($_GET['ingredient_name'][$i], $_GET['ingredient_price'][$i], $_GET['ingredient_default'][$i]));
    }
    

    update
    You need to use bind_param() when using mysqli -

    // the query
    $ingQuery = "INSERT INTO TABLE (column1,column2,column3,column4) VALUES (?,?,?,?)";
    // prepare statement
    $ingStmt = $mysqli->prepare($ingQuery);
    $ingStmt->bind_param("isss", $item_id, $val1, $val2, $val3);
    // iterate through all available data
    for( $i=0;$i<count($_GET['ingredient_name']);$i++ ) {
        $val1 = $_GET['ingredient_name'][$i];
        $val2 = $_GET['ingredient_price'][$i];
        $val3 = $_GET['ingredient_default'][$i];
        $ingStmt->execute();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程