dongwuchu0314 2017-02-23 10:28
浏览 50
已采纳

bind_param与数组$ i php数据库

I'm trying to insert data from an array into my database. I found few people that had similar problems so it could be duplicate, but the answers didn't help me at all, because they didn't have the $i variable in the code. So could you please help me insert these values to my database?

 for ($i=0; $i < $_SESSION["count"]; $i++) {

   // Create connection
   $conn = new mysqli($servername, $username, $password, $dbname);
   // Check connection
   if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
   }
     $stmt = ("INSERT INTO dite (name, lname, adress, bday, inf1, inf2, inf3, id_o, id_m, id_p) VALUES (?,?,?,?,?,?,?,?,?,?);");
     $stmt->bind_param("sssssssiii",$_SESSION["name$i"], $_SESSION["lname$i"],
                      $_SESSION["adress$i"], $_SESSION["bday$i"],
                      $_SESSION["inf1$i"], $_SESSION["inf1$i"],
                      $id_o, $id_m, $id_p);
     $stmt->execute();
     printf("%d Row inserted.
", $stmt->affected_rows);
     $stmt->close();

     $conn->close();
 }

Every help would be greatly appreciated!

EDIT:

If I did echo loop on every session I would get this:

test_name0 
test_lname0
test_address0
test_bday0
test_inf10
test_inf20
        //(blank)


test_name1 
test_lname1
test_address1
test_bday1
test_inf11
                 //(blank)
asdfg1

and $id_o = 0; $id_m = 0; $id_p = 0;

And error looks like this:

Call to a member function bind_param() on string in C:\wamp64\www\login\file.php on line 210.

Line 210 is:

$stmt->bind_param("sssssssiii",$_SESSION["name$i"], $_SESSION["lname$i"],
  • 写回答

1条回答 默认 最新

  • dsfhe34889789708 2017-02-23 11:01
    关注

    prepare and bind.

    in your query you are just binding, without preparing.

    <?php
    
        for ($i=0; $i < $_SESSION["count"]; $i++) {
    
           // Create connection
           $conn = new mysqli($servername, $username, $password, $dbname);
           // Check connection
           if ($conn->connect_error) {
              die("Connection failed: " . $conn->connect_error);
           }
             $stmt = $conn->prepare("INSERT INTO dite (name, lname, adress, bday, inf1, inf2, inf3, id_o, id_m, id_p) VALUES (?,?,?,?,?,?,?,?,?,?);");
             $stmt->bind_param("sssssssiii",$_SESSION["name$i"], $_SESSION["lname$i"],
                              $_SESSION["adress$i"], $_SESSION["bday$i"],
                              $_SESSION["inf1$i"], $_SESSION["inf1$i"],
                              $id_o, $id_m, $id_p);
             $stmt->execute();
             printf("%d Row inserted.
    ", $stmt->affected_rows);
             $stmt->close();
    
             $conn->close();
         }
         ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名