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();
         }
         ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?