i am trying to simplify my code which has multiple checks.. following is my code -
$query2 = "SELECT * FROM sample.temp";
$rslt = $dbo->query($query2);
if($rslt->rowCount() > 0)
{
foreach($rslt as $item)
{
$id = $item['entry_id'];
$Qty = $item['Qty'];
$group_ID = $item['group_ID'];
$component = $item['component'];
$vendor_ID = $item['vendor_ID'];
$stmt = $dbo->prepare("SELECT Qty FROM sample.temp WHERE vendor_ID=$vendor_ID AND component='$component' LIMIT 1");
if ($stmt->execute(array($_GET['Qty'])))
{
while ($row = $stmt->fetch(PDO::FETCH_OBJ))
{
$Q = $row->Qty;
}
}
$query4 = "UPDATE sample.stock SET Qty=(Qty+$Q) WHERE stock_ID=$vendor_ID AND component='$component'";//updating single existing entries
if ($dbo->query($query4))
{echo "Production updated !";}
else {echo "Production not updated!";}
$stm = $dbo->prepare("SELECT Qty FROM sample.temp WHERE vendor_ID=$vendor_ID AND component!='$component' LIMIT 1");
if ($stm->execute(array($_GET['Qty'])))
{
while ($row = $stm->fetch(PDO::FETCH_OBJ))
{
$Qt = $row->Qty;
}
$query5 = "INSERT INTO sample.stock (stock_ID, component, Qty) VALUES ($vendor_ID, '$component', $Qt)";//inserting new entry
if ($dbo->query($query5))
{echo "Production updated !";}
else {echo "Production not updated!";}
$st = $dbo->prepare("SELECT Qty FROM sample.temp WHERE vendor_ID!=$vendor_ID AND component='$component' LIMIT 1");
if ($st->execute(array($_GET['Qty'])))
{
while ($row = $st->fetch(PDO::FETCH_OBJ))
{
$Qtt = $row->Qty;
}
$query6 = "INSERT INTO sample.stock (stock_ID, component, Qty) VALUES ($vendor_ID, '$component', $Qtt)";//inserting new entry
if ($dbo->query($query6))
{echo "Production updated !";}
else {echo "Production not updated!";}
}
}
is there any way i can possibly reduce this code or change the way conditions are checked?
If i could use if statements in all three select statements it would be great !
Firstly i select a temporary table from where i transfer data into stock table on the basis of 3 checks -
updating existing values in stock table if they have same stock_ID and component as it is in temporary table
inserting new entry in stock table if it is not having the component corresponding to that stock_ID
inserting new entry in stock table if it is having the component but does not corresponds to any stock_ID in stock table
Note- vendor_ID in temporary table is same as stock_ID in stock table.
Related
I'm trying to get total price from cart table. The product_price in other table which is product. I'm only getting the latest price not the total price. Thanks
// function total_price (){
$total = 0;
global $db;
$ip = getIp();
$sql = $db->query("SELECT * from cart WHERE ip_add='$ip'");
$no=$sql->rowCount(); // number of rows affected by the last SQL statement
if ($no == 0){
echo "";
} else {
foreach($sql as $row)
$product_id = $row["p_id"];
$sql = $db->query("SELECT product_price from product WHERE product_id='$product_id'");
$no=$sql->rowCount(); // number of rows affected by the last SQL statement
if ($no == 0){
echo "";
}
else
{
foreach($sql as $row)
$product_price = array($row["product_price"]);
$values = array_sum($product_price );
$total += $values;
}
}
echo "RM" . $total;
}
if I'm reading the structure right this one query should be all you need:
select sum(product_price) from product
inner join cart on product.product_id=cart.product_id
I am trying to fetch a cart array through foreach loop. I can fetch array items successfully and when i echo them it shows the correct values. But when i apply insert query for each item to enter in my database table, It does not insert any thing but it shows success. I have 4 tables named:
customers-->serial primary,name,email,password,address,phone,city
products-->productid primary,name,description,image,price
orders-->serial primary,date,customerid foreign key
order_detail-->orderid,productid,quantity,price,color,size
order_detail and orders table should be updated when i click on button.
Here is my code. Any suggestions will be appreciated.
Code
if (isset($_SESSION['login_email'])) {
$email = $_SESSION['login_email'];
//Insert values in order table. This works correctly!!
$query = mysqli_query($con, "SELECT serial FROM customers WHERE email = '$email'");
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$cust_id = $row['serial'];
$date = date('Y-m-d');
$que = mysqli_query($con, "INSERT INTO orders VALUES('', '$date', '$cust_id')");
}
// Fetching serial from orders table. This also works correct.
$q = mysqli_query($con, "SELECT serial FROM orders WHERE customerid = '$cust_id'");
while ($row1 = mysqli_fetch_array($q, MYSQLI_ASSOC)) {
$serial = $row1['serial'];
echo $serial ."\n";
}
//Problem comes here. Here i am trying to insert values in order_details
foreach ($_SESSION['cart'] as $id => $value) {
$subtotal = $value['price'] * $value['quantity'];
$pid = $value['id'];
$quantity = $value['quantity'];
$color = $value['color'];
$size = $value['size'];
}
mysqli_query($con, "INSERT INTO order_detail VALUES ($serial, $pid, $quantity, $subtotal, $color, $size)");
if (true) {
echo "success";
} else {
echo"not success";
}
}
Did you already try this?
if( mysqli_query($con, "INSERT INTO order_detail VALUES ($serial, $pid, $quantity, $subtotal, $color, $size)") ){
echo "success";
} else {
echo"not success";
}
I Think you better can do the insert in this way:
INSERT INTO order_detail (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
Hello I have a Database named as "admin" in which i have two tables
Table 1 Name = "register"
Table 2 Name = "noti"
In Register Table i've approx more than 10+ User entries which comes through Registration Page
In Noti Table, its empty at this time (Column Name is also "noti")
I want to perform this thing
First I want to count the total no. of records in "register" table
and it checks, if the records are greater than ZERO then it runs the INSERT query otherwise it runs the UPDATE Query
And i want to INSERT and UPDATE that count value into "noti" table
Here's my code
<?php
include('config.php');
$sql2 = "SELECT count(*) as count FROM register";
$result2 = mysqli_query($con, $sql2);
if($result2->num_rows>0)
{
while($rw1=$result2->fetch_array())
{
$value1 = $rw1['count'];
$result = mysqli_query($con, "SELECT count(*) as count FROM register ");
if(!empty($value1)) {
mysqli_query($con, "UPDATE noti SET noti = '$value1' ");
}
else
{
mysqli_query($con, "INSERT INTO noti(noti) VALUES ('$value1') ");
}
}
}
?>
Use this:
include('config.php');
$sql2 = "SELECT count(*) as count FROM register";
$result2 = mysqli_query($con, $sql2);
$count = $result->num_rows;
if($count != 0)
{
mysqli_query($con, "UPDATE noti SET noti = '$count' ");
}
else
{
mysqli_query($con, "INSERT INTO noti(noti) VALUES ('$value1') ");
}
I have a script that allows an admin to view orders that customers have made, where they can decide to view the order details or delete the order. A user can order 1 or more items, for which entries in the database are created with the same order_id, but with different product_id's. When I display my orders these duplicate entries show up, however I only want 1 entry for every order_id. Below is my function
function viewOrdersAdmin(){
//This block grabs the orders
$order_list = "";
//Selecting all the orders in the table from that member
$sql = mysql_query("SELECT * FROM `transactions`") or die(mysql_error());
while ($transactions = mysql_fetch_array($sql)) {
//creating variables from the information
$order_id = $transactions["order_id"];
$mem_id = $transactions["mem_id"];
$order_details = mysql_query("SELECT * FROM `transactionDetails` WHERE `order_id` = $order_id") or die(mysql_error());
$orderDetailsCount = mysql_num_rows($order_details);
while ($row = mysql_fetch_array($order_details)) {
//creating variables from the information
$order_product_id = $row["Product_ID"];
$member_details = mysql_query("SELECT * FROM `members` WHERE `mem_id` = $mem_id") or die(mysql_error());
$memberDetailsCount = mysql_num_rows($member_details);
while ($row2 = mysql_fetch_array($member_details)) {
//creating variables from the information
$order_mem_fname = $row2["mem_first_name"];
$order_mem_lname = $row2["mem_last_name"];
$order_list .= "Order ID:$order_id - Customer Name: $order_mem_fname $order_mem_lname <a href='manage_order.php?orderid=$order_id'>View</a> • <a href='manage_orders.php?deleteid=$order_id'>Delete</a><br/>";
}
}
}
if (count($orderDetailsCount) == 0) {
$order_list = "You have no orders to display";
}
print_r($order_list);
}
SELECT *
FROM transactions
GROUP BY order_id
I have been going over this for a few days now and keep reaching stumbling blocks. I am trying to select a unique id from table2 and match it against an id in table1. If the id is matched, update the row in table1 and remove the record from table2. If there is no match then insert the record into table1.
I have got to a point where I can update the records and insert the new ones but I cannot seem to delete the matched record before the insert therefore creating a duplicate. I have tried a delete join query after the update but because the new logon_id has a character prepended to it, it no longer matches in table2.
I was doing an update join before for updates but I had too many queries going on so trying to keep it simple.
Any advice? Still a newb at this game.
$table2_query = "SELECT * FROM table2";
$table2_result = mysql_query($table2_query);
$table2_count = mysql_num_rows($table2_result);
if($table2_count == 0) {
if(mysql_error()) {
echo 'Error: '.mysql_error();
}
}
while($table2_row = mysql_fetch_array($table2_result, MYSQL_ASSOC)) {
$check_number_length = strlen($table2_row['unique_id']);
if($check_number_length < 7) {
if(substr($table2_row['unique_id'], 0, 2) < 35) {
$logon_id = 'n' . $table2_row['unique_id'];
}else {
$logon_id = 'v' . $table2_row['unique_id'];
}
}else {
$logon_id = $table2_row['unique_id'];
}
// Set variables for insert query for creation of a new user record
$first_name = $table2_row['firstname'];
$last_name = $table2_row['lastname'];
$email_address = $table2_row['email'];
$duplicates_query = "SELECT * FROM table1 WHERE '$logon_id' = logon_id";
$duplicates_result = mysql_query($duplicates_query);
$duplicates_row = mysql_fetch_array($duplicates_result);
$duplicates_count = mysql_num_rows($duplicates_result);
if($duplicates_count == 0) {
if(mysql_error()) {
echo 'Error: '.mysql_error();
}
}else {
$update_records_query = "UPDATE table1 SET first_name='$first_name', last_name='$last_name', email_address='$email_address'";
$update_records_result = mysql_query($update_records_query);
$update_records_count = mysql_affected_rows();
echo $update_records_count;
}
$create_records_query = "INSERT INTO table1 (first_name, last_name, email_address) VALUES ('$first_name', '$last_name', '$email_address')";
$create_records_result = mysql_query($create_records_query);
$create_records_count = mysql_affected_rows();
if($create_records_count == 0){
if(mysql_error()){
echo 'Error: '.mysql_error();
}
}
echo $create_records_count . ' record(s) created.';
}
$res = mysql_query ("SELECT count(table1.login_id) AS count FROM table2 LEFT JOIN table1 ON table2.login_id = table1.login_id WHERE table2.login_id = \"".$login_id."\";") or die ("Error joining tables");
/*joins both tables together and selects both id's from tables */
$row = mysql_fetch_assoc($res); // should only return one row so grab first
if ($row['count'] > 0) { // check if id exists in both tables (duplicates)
// updates rows from one table into another
mysql_query('UPDATE table1,table2 SET table1.username = table2.username, table1.password = table2.password, table1.email = table2.email WHERE table1.login_id = "'.$login_id.'" AND table2.login_id = "'.$login_id.'";') or die ("error updating table1");
//delete old row
mysql_query('DELETE FROM table2 WHERE login_id = "'.$login_id.'";') or die("error deleting from table2");
}else { // if id doesn't exist in table1
mysql_query ("INSERT INTO table1(username,password,email) SELECT username,password,email FROM table2 where login_id = '".$login_id."';") or die ("error inserting into table1");
}
remove the n at the beginning of login_id on table1
UPDATE table1 set login_id = replace(login_id,"n","");
new update query if remove n at begin of login_id
mysql_query ('UPDATE table1,table2 SET table1.username = table2.username, table1.password = table2.password, table1.email = table2.email WHERE table1.login_id = table2.login_id AND table1.login_id = "'.$login_id.'";');