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 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗