dongmoyu0336 2016-07-15 10:37
浏览 70

php mysql编写语句循环数组

my problem is that I want to loop through an array and insert every entry of that array into another column of an mySQL table. To be honest, I'm not sure if that's the best way to design my database, but that's one way I could imagine, it works. If someone has a better idea of how to do it or a link for best practice or something, that would be awesome.

So what I want to do: I have a form where someone can register to offer a food delivery service. He can enter name etc. and up to 10 offers (limitation of the database table). These information should be insert into the table 'anbieter' into the fields 'angebot_0' , 'angebot_1' ... So what I did is:

if (isset($_POST['register_offer']) and isset($_POST['anbieter-email'])){

$name = $loc = $cat = $email = $password ="";
$angebot = array();
// fill all variables
$name = test_sql($_POST['anbieter-name']);
$email = test_sql($_POST['anbieter-email']);
$password = test_sql($_POST['anbieter-password']);
$loc = test_sql($_POST['anbieter-loc']);
$cat = test_sql($_POST['anbieter-cat']);
// fill $angebot with all given angebot[] entries
foreach($_POST['angebot'] as $ang) {
    $angebot[] = test_sql($ang);
}

if(!empty($name) and !empty($loc) and !empty($email) ){
    /* decrypt password */
    $password = password_hash($password, PASSWORD_BCRYPT, ["cost" => 12]);
    // insert name, email, password, location and category into database
    /* Prepared statement, stage 1: prepare */
  if (!($stmt = $conn->prepare("INSERT INTO anbieter (anbieter_name, anbieter_email, anbieter_password, anbieter_loc, anbieter_cat) VALUES (?, ?, ?, ?, ?)"))) {
     echo "Prepare failed: (" . $stmt->errno . ") " . $stmt->error;
    }
/* Prepared statement, stage 2: bind and execute */
  if (!$stmt->bind_param('sssss', $name, $email, $password, $loc, $cat)) {
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
    }
  if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
    }

$userid = $stmt->insert_id;
// safe all angebot[] entries to database - angebot[0]-> angebot_0
for($x=0; $x < count($angebot) ; $x++) {
    $upd = $conn->prepare("UPDATE anbieter SET angebot_".$x." = ? WHERE abieter_ID = ? ");
    $upd->bind_param('si', $angebot[$x], $userid);
    $upd->execute();
}

So when I do this, I get the error:

Fatal error: Call to a member function bind_param() on boolean in ...

It's a super bad way to do that by using $x to name different fields of the table, but that's the only way I could think of it works :/

I hope someone can help me here! :) Thanks a lot!

  • 写回答

2条回答 默认 最新

  • doubeizhong5178 2016-07-15 10:59
    关注

    My suggestion instead on single record update query multiple times you can do it in a single query,

    Eg:

    $query = "UPDATE anbieter SET";
    for ($x = 0; $x < count($angebot); $x++) {
        $query .= " angebot_" . $x . " = '" . $angebot[$x] . "', ";
    }
    echo $query .= " WHERE abieter_ID = " . $userid;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗