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:
- There are two tables users and user_order
- Trying to fetch existing user's user id from users table to user_order table.
- user_order table has user_id as a foreign key referencing id(PK) field in users table.
- 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.