doudou1438 2018-03-05 01:34
浏览 61

INSERT INTO关系数据库不能使用php代码

The code I have for the function trying to insert the data into the table is

function registerDiet(){
    global $connect, $meat, $seafood, $salad, $name, $username, $age, $email, $password, $hash;

    $statement = mysqli_prepare($connect, "SELECT user_id FROM User WHERE username = ?");
    mysqli_stmt_bind_param($statement, "s", $username);
    mysqli_stmt_execute($statement);
    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $colUserID);

    $statement = mysqli_prepare($connect, "INSERT INTO Diet (user_id, meat, seafood, salad) VALUES (?, ?, ?, ?)");
    mysqli_stmt_bind_param($statement, "iiii", $colUserID, $meat, $seafood, $salad);
    mysqli_stmt_execute($statement);
    mysqli_stmt_close($statement);
}

The function is called towards the end of the code.

user_id is the primary key in the User table and a foreign key in the Diet table, the relationship has been configured and it works fine when using phpMyAdmin. Meat, seafood and salad field types are all boolean (tinyint) in the database table.

For example when I use

INSERT INTO `Diet` (`user_id`, `meat`, `seafood`, `salad`) VALUES ('46', '0', '0', '0');  

in phpMyAdmin it works, anyone able to advise?

  • 写回答

1条回答 默认 最新

  • drb56625 2018-03-05 04:28
    关注

    You have to close the first statement before opening another one.

    function registerDiet(){
    global $connect, $meat, $seafood, $salad, $name, $username, $age, $email, $password, $hash;
    
    $statement = mysqli_prepare($connect, "SELECT user_id FROM User WHERE username = ?");
    mysqli_stmt_bind_param($statement, "s", $username);
    mysqli_stmt_execute($statement);
    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $colUserID);
    
    mysqli_stmt_close($statement);
    
    $statement = mysqli_prepare($connect, "INSERT INTO Diet (user_id, meat, seafood, salad) VALUES (?, ?, ?, ?)");
    mysqli_stmt_bind_param($statement, "iiii", $colUserID, $meat, $seafood, $salad);
    mysqli_stmt_execute($statement);
    mysqli_stmt_close($statement);
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记