For my ordering system, when an admin inserts an order i need to check if the customer he inserts for the order exists in my customer table.
Im trying something like this, but with no luck..
$sql = "IF EXISTS(SELECT * FROM customer WHERE customer_id = '$customer')
THEN insert into `order` (customer_id, product, quantity, creation_time, order_note, order_employee)
values ('$customer', '$product', '$quantity', 'now()', '$note', '$employee')";
Error: syntax error in first line.
What is wrong here?
And is this the right approach? Is there a better way?
you might want to do it "backwards" - insert if exists:
$sql = "insert into `order` (customer_id, product, quantity, creation_time, order_note, order_employee)
select '$customer', '$product', '$quantity', now(), '$note', '$employee'
from dual
where EXISTS(SELECT * FROM customer WHERE customer_id = '$customer')";
Here's a neat trick you can do:
$sql = "INSERT INTO `order`
(`customer_id`, `product`, `quantity`,
`creation_time`, `order_note`, `order_employee`)
SELECT `customer_id`, '$product', '$quantity', now(), '$note', '$employee'
FROM `customer` WHERE `customer_id`='$customer'";
Note that I'm trusting you have already properly sanitised your variables!
But this will only insert the order if the customer with that ID exists.
I think you should think your problem in another way.
Proceed with steps :
1 - I need to know if customer my customer exist?
$sql = "SELECT ID FROM customer WHERE customer_id = '$customer'";
2- I try my customer, does he exist?
If(!empty($result_cust)){
$sql = insert into `order` (customer_id, product, quantity, creation_time, order_note, order_employee)
values ('$customer', '$product', '$quantity', 'now()', '$note', '$employee')";
}
With this way your code will be more easily to understand.
Divide and Rule
They said.
Related
This question already has answers here:
Retrieve id of each INSERT statement in multi query
(5 answers)
Closed 3 years ago.
I've been working with this project, now am stuck where i have to insert the id of the first query of which it identifies the relations of that table to others, i want the id of the first query to be saved as a variable and then be inserted into the following queries
I've tried to set value to be used but its not working, as i want all these to work in one multi-query, down here are some code of the queries
$query = "INSERT INTO guests (
user_id,
first_name,
last_name,
nationality,
status,
sign_up)
VALUES (
'$user_id',
'$first_name',
'$last_name',
'$nationality',
'$status',
NOW());";
$query = "SELECT #last_id := LAST_INSERT_ID();"
$query .= "INSERT INTO bookings (
user_id,
guest_id,
coming_from,
going_to,
date_in,
date_out,
sign_up)
VALUES (
'$user_id',
#last_id,
'$coming_from',
'$going_to',
'$date_in',
'$date_out',
NOW());";
$query .= "INSERT INTO preference (
guest_id,
prefer,
alergy,
sign_up)
VALUES (
#last_id,
'$prefer',
'$alergy',
NOW());";
$query .= "INSERT INTO rooms (
user_id,
guest_id,
room_number,
room_type,
room_price,
sign_up)
VALUES (
'$user_id',
#last_id,
'$room_number',
'$room_type',
'$room_price',
NOW())";
if(mysqli_multi_query($conn, $query))
{
echo "Guest Received successfully!";
} else {
echo "Failed! Data input error!";
}
}
i expected it to fetch the first query guest id and insert it into #last_id column, Kindly any ideas?
On occasion, I've had to use this:
SELECT `auto_increment` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'tablename'
or
SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = your_tablename' AND table_schema = DATABASE( ) ;
Perhaps that will work for you
References:
https://dev.mysql.com/doc/refman/8.0/en/show-table-status.html
https://php.net/manual/en/mysqli.insert-id.php
I'm looking to insert values from two sources (variables and a field from another table) into a new table. After some research, I found that this was possible, but cannot figure out how to accomplish this with my query.
Let me know if I have not provided enough context or code.
//Query to INSERT data
$query3 = "INSERT INTO `Checked_Out` (`name`, `quantityCheckedOut`, `checkedOut`, `returnDate`, `image`, `ID`) VALUES ('$name', '$quantityTaken', '$checkedOut', '$returnDate', '$ID')
SELECT `image` FROM `Checked_In` WHERE `ID` = '$ID'";
Try this:
$query3 = "INSERT INTO `Checked_Out` (`name`, `quantityCheckedOut`, `checkedOut`, `returnDate`, `image`, `ID`)
SELECT '$name', '$quantityTaken', '$checkedOut', '$returnDate', `image`, '$ID' FROM `Checked_In` WHERE `ID` = '$ID'";
Is there any difference between this 3 query?
query 1
$query = "INSERT INTO reserve( c_id, c_username, r_id, r_name, checkin, checkout)";
$query .= " VALUES ( $c_id, $c_username, $r_id, $r_name, $checkin, $checkout )";
query 2
$sql = "INSERT INTO reserve( c_id, c_username, r_id, r_name, checkin, checkout)
VALUES ( '$c_id' , '$c_username', '$r_id', '$r_name', '$checkin', '$checkout' )";
query 3
$result = $mysqli->query("INSERT INTO reserve (c_id, c_username, r_id, r_name, checkin, checkout) VALUES ('$c_id' , '$c_username', '$r_id', '$r_name', '$checkin', '$checkout');")
And which one should I use to select a data from my database and which one should I use to insert data into database
On a quickly glance they all appear to do the same thing. You don't need to learn parameter passing at your stage. Be aware of injection attacks and clean those variables before you do calls.
I tend to do this for readability:
$sql = "INSERT INTO reserve(c_id
,c_username
,r_id
,r_name
,checkin
,checkout)
VALUES ('$c_id'
,'$c_username'
,'$r_id'
,'$r_name'
,'$checkin'
,'$checkout')";
I want the date to be added to an orders table I have created, once the user proceeds to checkout. The code I currently has just prints:
"Error: Column count doesn't match value count at row 1"
Here's my code:
$sql = "INSERT INTO orders (customer_id, total, order_date) VALUES ('$name', '$total', 'DATETIME: Auto NOW()', NOW() )";
The name and total columns store but the date does not. How can I solve this?
This way:
$sql = "INSERT INTO orders (customer_id, total, order_date) VALUES ('$name', '$total', NOW())";
You want to insert data inside customer_id, total, order_date (3 rows) but you are sendig '$name', '$total', 'DATETIME: Auto NOW()', NOW(), FOUR.
Your error means that you that the number of fields does not match the number of values. That seems correct: your query tries to insert 4 values into 3 fields. You'd probably have to rewrite the query to
$sql = "INSERT INTO orders (customer_id, total, order_date) VALUES ('$name', '$total', NOW() )";
It looks like you need 3 values: customer_id, total, order_date
but you give 4 :'$name', '$total', 'DATETIME: Auto NOW()', NOW()
Maybe it should look like this:
"INSERT INTO orders (customer_id, total, order_date) VALUES ('$name', '$total', NOW() )";
I have 2 tables customer and milk as shown below
customers
customers (cid, name, price);
milk
milk (mid, customer_id, milk_letters, mprice)
I need an update query to multiply price, milk_letters and store in mprice where customer_id = cid
This is my insert statement that is wrong, I need to change it to update statement.
$cid = $_POST['cid'];
$milk_letters = $_POST['milk_letters'];
$sql = "INSERT INTO milk (customer_id, milk_letters, milk_date)
VALUES ('$cid','$milk_letters', NOW())";
dbQuery($sql);
$sql2 = "INSERT INTO milk SELECT SUM(price * milk_letters) as mprice FROM customers , milk WHERE customers.cid = '$cid' and milk.customer_id = '$cid'";
$result2 = dbQuery($sql2);
You could do this in one query rather than 2
INSERT INTO milk (customer_id, milk_letters, milk_date, mprice)
SELECT {$cid}, {$milk_letters}, NOW(), customers.price * {$milk_letters}
FROM customers
WHERE customers.cid = {$cid}