My mysql update query isnt working - php

for some reason, I can't get this to work.
Thanks in advance, a secnod eye might help!
$sql3 = "SELECT order_id FROM orders WHERE order_code = '$order_code'";
$result3 = $conn->query($sql3) or exit("Error code ({$conn->errno}): {$conn->error}");
$row = mysqli_fetch_assoc($result3);
$order_id = $row['order_id'];
$deliv_date = date('Y-m-d');
$sql = "UPDATE orders SET deliv_date = $deliv_date
WHERE order_id = $order_id";
$result = $conn->query($sql) or exit("Error code ({$conn->errno}): {$conn->error}");
$sql1 = "INSERT INTO invoice VALUES (0,'$order_code','$deliv_date','','$order_id')";
$result1 = $conn->query($sql1) or exit("Error code ({$conn->errno}): {$conn->error}");

try this
$sql = "UPDATE `orders` SET `deliv_date` = '".$deliv_date."'
WHERE `order_id` = '".$order_id."'"

Related

How can I apply MySQL transactions feature using php with it?

I am trying to build a sale invoice page that takes multiple products as input and also some information about the customer. It interacts with four MySQL tables (sales, sale_details, posting, and product_inventory). You'll get the idea of what will happen if a query fails. I want to avoid this. For this purpose I am trying to implement transactions feature of the InnoDB database engine using PHP and MySQL (PHPMyAdmin). Another problem is I've heard that autocommit is turned off by default do I need to turn it ON and then OFF every time I fire a query? Here is my PHP code snippet:
$sal_date = trim($_POST["sale_date"]);
$cust_id = trim($_POST["cust_id"]);
$book_no = trim($_POST["sal_book_no"]);
$rem = $_POST["sal_remarks"];
$st = trim($_POST["sub_total"]);
$disc = floatval($_POST["total_disc"]) + floatval($_POST["adj_disc"]);
$total = trim($_POST["grand_total"]);
$query1 = mysqli_query($link, "INSERT INTO sales
(cust_id, book_no, sale_date, sub_total, discount, total, remarks)
VALUES ('$cust_id', '$book_no', '$sal_date', '$st', '$disc', '$total', '$rem')") or die(mysqli_error($link));
$query2 = mysqli_query($link, "SELECT LAST_INSERT_ID() as last_row") or die(mysqli_error($link));
$sal_id = mysqli_fetch_array($query2);
$sal = intval($sal_id["last_row"]);
$the_query1 = mysqli_query($link, "INSERT INTO `posting`(`type`, `account_id`, `tr_id`, `tr_date`, `debit`) VALUES ('SI','$cust_id','$sal', '$sal_date', '$total')") or die(mysqli_error($link));
$the_query2 = mysqli_query($link, "INSERT INTO `posting`(`type`, `account_id`, `tr_id`, `tr_date`, `credit`) VALUES ('SI','10002','$sal', '$sal_date', '$total')") or die(mysqli_error($link));
for($count=0; $count<$_POST["total_item"]; $count++)
{
$prod_id = floatval(trim($_POST["product_name"][$count]));
$quantity = floatval(trim($_POST["qty"][$count]));
$disc = floatval(trim($_POST["disc"][$count]));
$query3 = mysqli_query($link, "INSERT INTO sale_details (sal_id, prod_id, quantity, discount) VALUES ('$sal', '$prod_id', '$quantity', '$disc')") or die(mysqli_error($link));
$query4 = mysqli_query($link, "INSERT INTO product_inventory (invoice_id, product_id, qty_out, in_date) VALUES ('$sal', '$prod_id', '$quantity', '$sal_date')") or die(mysqli_error($link));
}
I'll appreciate your suggestions.
You can use this method
mysqli_query($db, "START TRANSACTION");
$query1 = mysqli_query($db, "Query 1");
$query2 = mysqli_query($db, "Query 2");
$query3 = mysqli_query($db, "Query 3");
if($query1 && $query2 && $query3) {
mysqli_query($db, "COMMIT");
} else {
mysqli_query($db, "ROLLBACK");
}

How to sum column in database using php

i'm trying to sum a column name "total". and i want to display the total sorting by id. if user A login he can see total booking in his account.
I keep get the error:
"Notice: Array to string conversion in Array."
can someone help me? I want to echo the total in input form.
this is my php code:
<?php
include ('connect.php');
$sql = "SELECT * FROM penjaga WHERE p_username = '".$_SESSION['username']."'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($result);
$id = $row['p_id'];
$sql2 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$row2 = mysql_fetch_array($sql);
$sum = $row['total'];
?>
Try this,
$sql2 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$result2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_array($result2) or die(mysql_error());
$sum = $row['total'];
i got it! thanks this is my code
this is the code:
<?php
include ('connect.php');
$sql8 = "SELECT * FROM penjaga WHERE p_username = '".$_SESSION['username']."'";
$result8 = mysqli_query($conn,$sql8);
$row8 = mysqli_fetch_assoc($result8);
$id = $row8['p_id'];
$sql9 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$result9 = mysqli_query($conn,$sql9);
$row9 = mysqli_fetch_array($result9);
$sum = $row9['total'];
?>

Error when adding FIRST record to table

Ok. I ran into an issue. My code is able to insert records into my merchandise table. I truncated the table and a record is still inserted into the table but with an error "Undefined variable last_id". I assume that this is because when the table was truncate, there isn't a previous id since the record being inserted is the FIRST. Can someone help me resolve this issue. Thanks!
$sql = "SELECT m_id FROM merchandise";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)) {
$last_id = $row["m_id"];
}
$next_id = $last_id+1;
$conc = number_format($next_id/100,2,'-','');
$query = "INSERT INTO merchandise (mfr,type,description,mer_sku,price,qty) ";
$query .="VALUES ('$mfr','$type','$desc','MR{$mfr}{$conc}','$price','$qty')";
$add_sku_query = mysqli_query($connection, $query);
Declare last id as zero in case there are no rows.
$last_id = 0;
$sql = "SELECT m_id FROM merchandise";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)) {
$last_id = $row["m_id"];
}
$next_id = $last_id+1;

Using a good SQL query instead of PHP code

I have a page that is taking a kind of long time to load, and I'm almost sure that this is caused by too many sql requests (AKA caused by my bad SQL skills). Is there anyway to join these 3 queries into one?
What I want to do with this query is to try to select a specific id from cardapios and, if there is anything there (if $num_rows > 0) the only thing I want to do is select that id. If there is nothing there, then I want to insert something and then select the id of that.
$query = "SELECT id FROM cardapios WHERE nome='$nome'";
$sql = mysqli_query($con,$query);
$num_rows = mysqli_num_rows($sql);
if ($num_rows > 0){
while ($row = mysqli_fetch_array($sql)){
$_SESSION['id_cardapio'] = $row['id'];
$num_rows = 0;
}}else{
$query = "INSERT INTO cardapios (nome, kcal, semana)
VALUES('$nome', '$kcal', '$semana')" or die(mysqli_error($con));
$sql = mysqli_query($con,$query);
$query = "SELECT id FROM cardapios WHERE nome='$nome' ";
$sql = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($sql)){
$_SESSION['id_cardapio'] = $row['id'];
}
}
I am trying to put all of this into one query but getting nowhere. Is there anyway to use just one query for doing all of this?
Thanks in advance!
You can replace the last query by getting the mysqli_insert_id($con); as you already have the insert id available after the insert
$query = "SELECT id FROM cardapios WHERE nome='$nome'";
$sql = mysqli_query($con,$query);
$num_rows = mysqli_num_rows($sql);
if ($num_rows > 0){
while ($row = mysqli_fetch_array($sql)){
$_SESSION['id_cardapio'] = $row['id'];
$num_rows = 0;
}
}else{
$query = "INSERT INTO cardapios (nome, kcal, semana)
VALUES('$nome', '$kcal', '$semana')" or die(mysqli_error($con));
$sql = mysqli_query($con,$query);
if ( $sql !== false) { // did insert work
$_SESSION['id_cardapio'] = mysqli_insert_id($con);
} else {
// insert did nto work??
}
}

why does my while loop stop after executed another query inside?closed

my while loop stops after executed another query inside... can you correct my codes? I want to update the column status in table ordered_items_supplier to "Pending" when the pi_number is found in the table purchased_items_supplier and if not found the column status is "Active".
$sql2 = "select * from ordered_items_supplier";
$result = $connect->query($sql2);
if($result->num_rows > 0){
while ($row = $result->fetch_assoc()) {
$pi_number = $row['pi_number'];
$sql = "select * from purchased_items_supplier where pi_number = '$pi_number'";
$result = $connect->query($sql);
if($result->num_rows > 0){
while ($row2 = $result->fetch_assoc()) {
$pi_number = $row2['pi_number'];
$sql = "update ordered_items_supplier set status = 'Pending' where pi_number = '$pi_number'";
$query = $connect->query($sql);
}
}else{
$sql = "update ordered_items_supplier set status = 'Delivered' where pi_number = '$pi_number'";
$query = $connect->query($sql);
}
}
}
here's my mysql.. it should update the status "Delivered" in ID 11
The problem is overwriting the same variable each time.
Check that you use $result for the outer and inner query both.That's why the problem occur. So don't overwriting the $result variable.

Categories