Values will not insert in database - php

I have an array of dates from session to be stored to the database. I used foreach to save each array value to the database.
$guestID = $_SESSION['guestID'];
$reservationQuery = mysqli_query($conn, "SELECT MAX(reservationID) AS resID FROM reservation WHERE guestID = $guestID;") OR die("Error in SELECT: ".mysqli_error($conn));
$reservationID = mysqli_fetch_array($reservationQuery);
foreach($_SESSION['noOfRooms'] AS $type => $rooms){
foreach($_SESSION['dates'] AS $dates){
$reserves = mysqli_query($conn, "INSERT INTO roomreservation (reservationID, date_scheduled, roomDetailsNo, roomNo)
SELECT $reservationID[resID], '$dates', roomDetailsNo, roomNo
FROM rooms
WHERE roomDetailsNo = $type AND room_status = 'available' LIMIT $rooms[0];") OR die("Error: ".mysqli_error($conn));
}
$update = mysqli_query($conn, "UPDATE rooms SET room_status = 'reserved' WHERE roomDetailsNo = $type AND room_status = 'available' LIMIT $rooms[0];") OR die("ERROR in Update: ".mysqli_error($conn));
}
When I ran the program. the table roomreservation was empty and did not save any of the values I inserted. I don't see the problem. can anyone help me?

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");
}

Using result of one MySQL query as variable in another MySQL query

I'm trying to show the result HTML table based on team name
I'm able to echo right team name but unable to use it into a variable in my 2nd query I'm not able to find out what I'm doing wrong here why the query result is not visible. do I need to change something in my code?
<?php
include_once("connection.php");
$sql = "SELECT TeamName FROM `superuser` WHERE id = '303016'";
$queryRecords2 = mysqli_query($conn, $sql) or die("error to fetch employees data");
while ($row2 = $queryRecords2->fetch_assoc()) {
echo $row2['TeamName']."<br>";
}
if(isset($_POST['search']))
{
$valueToSearch = $row2['TeamName'];
$valueToSearch2 = $_POST['valueToSearch2'];
$valueToSearch3 = $_POST['valueToSearch3'];
$sql = "SELECT * FROM `dailydata` WHERE TeamName = '".$valueToSearch."' and Date BETWEEN '".$valueToSearch2."' AND '".$valueToSearch3."'";
$queryRecords = mysqli_query($conn, $sql) or die("error to fetch employees data");
}
else {
$sql = "SELECT * FROM `dailydata` WHERE TeamName = ''";
$queryRecords = mysqli_query($conn, $sql) or die("error to fetch employees data");
}
?>
For security purposes you should use prepared statements or even better you should use PDO instead of mysqli.
to fix this issue simply assign the value of $valueToSearch variable inside the loop.
<?php
include_once("connection.php");
$sql = "SELECT TeamName FROM `superuser` WHERE id = '303016'";
$queryRecords2 = mysqli_query($conn, $sql) or die("error to fetch employees data");
$valueToSearch;
while ($row2 = $queryRecords2->fetch_assoc()) {
$valueToSearch = $row2['TeamName'];
}
if(isset($_POST['search']))
{
$valueToSearch2 = $_POST['valueToSearch2'];
$valueToSearch3 = $_POST['valueToSearch3'];
$sql = "SELECT * FROM `dailydata` WHERE TeamName = '".$valueToSearch."' and Date BETWEEN '".$valueToSearch2."' AND '".$valueToSearch3."'";
$queryRecords = mysqli_query($conn, $sql) or die("error to fetch employees data");
}
else {
$sql = "SELECT * FROM `dailydata` WHERE TeamName = ''";
$queryRecords = mysqli_query($conn, $sql) or die("error to fetch employees data");
}
?>

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;

SQL update & insert

I have a MYSQL table named issues_tot including following columns:
v_code, oid, amount, mod_date
02) Then I need to update or insert records of the table according to the given condition as follows:
if(($vt == $vote)||($of == $ono)){
03) update is working properly, but insert is not (else part). My code is showing below:
if (isset($_POST["submit"]))
{
$ono =$_POST["oid"];
$amt =$_POST["amt"];
$allo=mysql_fetch_array(mysql_query("SELECT * FROM allocation WHERE al_code='{$_GET['al_code']}'"));
$vote=$allo['v_code'];
$current_date = date("Y-m-d H:i:s");
$query ="select * from issues_tot where v_code='$vote' ";
$result = mysql_query($query) or die ( mysql_error());
$row = mysql_fetch_assoc($result);
$vt = $row['v_code'] ;
$of = $row['oid'] ;
if(($vt == $vote)||($of == $ono)){
$query ="UPDATE issues_tot SET oid = $ono, amount = amount + $amt WHERE v_code=$vote";
$result = mysql_query($query) or die ( mysql_error());
$rc = mysql_affected_rows();
}else {
$query ="INSERT INTO issues_tot (v_code, oid, amount, mod_date) VALUES ('$vote', '$ono', '$amt', '$current_date')";
$result = mysql_query($query) or die ( mysql_error());
$rc = mysql_affected_rows();
}
}
I can not understand what I am going wrong. Can anyone help me ?. Pls

My mysql update query isnt working

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."'"

Categories