du8828 2018-05-20 15:29
浏览 51
已采纳

PHP中的mysqli_query问题 - 查询在phpmyadmin中运行,而不在PHP中运行

I am trying to run an insert query using PHP. As mentioned in the title it works in phpmyadmin but does not work in php code.

Below is the code:

if ($result) {

  session_start();
  $_SESSION['fname'] = $firstname;
  $firstname_sess = $_SESSION['fname'];
  $sql_uid = " SELECT id FROM users WHERE firstname = '" . $firstname_sess . "' ";
  var_dump($sql_uid);
  $sql_uid_result = mysqli_query($db, $sql_uid);
  $sql_uid_array = mysqli_fetch_array($sql_uid_result, MYSQLI_ASSOC);
  $sql_uid_final = $sql_uid_array['id'];
  var_dump($sql_uid_final);
  mysqli_query('SET foreign_key_checks = 0');
  $uo_uid = "INSERT INTO `user_order` (user_id) VALUES ('$sql_uid_final')";
  var_dump($uo_uid);
  $uo_uid_final = mysqli_query($db, $uo_uid);
  var_dump($uo_uid_final);
  if ($uo_uid_final) {

    echo "All good";
  } else {
    echo "Something went wrong 1";
  }
  //header('Location: productsform.php');

} else {
  echo "Somethingwentwrong2";
}

Code Explanation:

  1. There are two tables users and user_order
  2. Trying to fetch existing user's user id from users table to user_order table.
  3. user_order table has user_id as a foreign key referencing id(PK) field in users table.
  4. Facing issue while inserting the value in user_order table.

Although the code works through PHP myadmin or terminal but is not working via PHP code.

I have gone through the similar links given below: 1. mysql DELETE works on phpmyadmin but not on php script 2. SQL query working in phpmyadmin but not in php 3. mysqli_query works in phpmyadmin but not in php 4. Query works in phpMyAdmin but not in php

And many more but could not find anything. Any help would really be appreciated.

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • dsq2015 2018-05-20 15:46
    关注

    One problem I see in your code is this line:

    mysqli_query('SET foreign_key_checks = 0');
    

    You haven't provided $db as the first argument of mysqli_query.

    If that's not the issue, could you provide the mylsqli_error() for the query that fails?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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