?Briella 2017-01-04 14:32 采纳率: 0%
浏览 415

MySQL插入空行

I have an array of object that I want to insert into database.
So I'm looping all objects with for loop.

I'm sending $offer and $gameid with ajax
$offer is array with 10 items and $gameid is just integer

All variables have data because it echo them.

I'm working on xampp with localhost

include "../../inc/connect.php";

$offer = @$_POST['offer'];
$gameid = @$_POST['gameid'];

$query = $db->prepare("INSERT INTO items VALUES (id, gameid, assetid, name, icon, price, owner)");

for($i = 0; $i < count($offer) - 1; $i++) {
    $assetid = $offer[$i]['assetid'];
    $name = $offer[$i]['name'];
    $icon = $offer[$i]['icon'];
    $price = $offer[$i]['price'];

    $query_array = array('id' => '', 'gameid' => $gameid, 'assetid' => $assetid, 'name' => $name, 'icon' => $icon, 'price' => $price, 'owner' => $steamid);

    $query->execute($query_array);

echo "
<div class='item'>
    <div class='left'>
        <img src='$avatar' />
        <div class='names'>
            <p>$username</p>
            <p>$name</p>
        </div>
    </div>
    <div class='right'>
        <div class='price'>
            $" . $price . "
        </div>
        <div class='item-icon'>
            <img src='http://cdn.steamcommunity.com/economy/image/$icon' />
        </div>
    </div>
</div>  
";
}

Connect

$user = "root";
$pass = "";

$db = new PDO('mysql:host=localhost;dbname=website', $user, $pass);

Table

enter image description here

What I get when $query->execute($query_array)

enter image description here

No errors in mysql_error.log or in ajax data

  • 写回答

2条回答 默认 最新

  • weixin_33717117 2017-01-04 14:54
    关注

    You need colons on your placeholders, or else it will treat the values as column names:

    $query = $db->prepare("INSERT INTO items VALUES (:id, :gameid, :assetid, :name, :icon, :price, :owner)");
    
    $query_array = array(':id' => '', ':gameid' => $gameid, ':assetid' => $assetid, ':name' => $name, ':icon' => $icon, ':price' => $price, ':owner' => $steamid);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试