update row value based on if else condition - php

In php Page once we click on "Submit" button , in database we are saving order id, its working fine....
Requirement :
If payment is "Cash on delivery" , than i want to save order id in "awb_type : COD" row.... otherwise in "awb_type : PPD" row....
here is full code , track.php : https://pastebin.com/zLjpee7A , call.php : https://pastebin.com/4LkcxTYE
But orders are updating twice in table - one row in PPD & one in COD
Please let me know if you need more information....
Update 2 :
Now i tried below code, but whatever is payment_type , its saving only in awb_type column : PPD rows....
$sqlc = "select * from ecomexpress_awb WHERE status='unused' AND awb_type='COD' limit 1";
$resultc = $db_handle->runSelectQuery($sqlc);
$sqld = "select * from ecomexpress_awb WHERE status='unused' AND awb_type='PPD' limit 1";
$resultd = $db_handle->runSelectQuery($sqld);
$payment_type='';
$sqlg="SELECT * FROM do_order where payment_type='".$payment_type."'";
$resultg = $db_handle->runSelectQuery($sqlg);
if($payment_type=="Cash on delivery")
{
$awb = $resultc[0]['awb'];
$sqle = "update ecomexpress_awb set orderid = '".$order_id."',status='used' WHERE awb ='".$awb."' limit 1";
$resulte = $db_handle->runSelectQuery($sqle);
}
else
{
$awba = $resultd[0]['awb'];
$sqlf = "update ecomexpress_awb set orderid = '".$order_id."',status='used' WHERE awb ='".$awba."' limit 1";
$resultf = $db_handle->runSelectQuery($sqlf);
}

Before I did't binded the payment_type with order_id, below code worked for me :
if(isset($_POST['order_id']) && $_POST['order_id']!='')
{
$order_id = $_POST['order_id'];
$payment_type=$_POST['payment_type'];
$sqlg="SELECT * FROM do_order where payment_type='".$payment_type."'";
$resultg = $db_handle->runSelectQuery($sqlg);
if($payment_type=="Cash on delivery")
{
$sqlc = "select * from ecomexpress_awb WHERE status='unused' AND awb_type='COD' limit 1";
}
else
{
$sqlc = "select * from ecomexpress_awb WHERE status='unused' AND awb_type='PPD' limit 1";
}
$resultc = $db_handle->runSelectQuery($sqlc);

Related

How can i order by id ascending?

I want to order data by Id, how can i do this ?
if($_GET["grupid"]>0){
$DUZEN = array();
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"];
$rsDuzen = mysql_query($sql, $conn) or die(mysql_error());
while ($r = mysql_fetch_assoc($rsDuzen)) {
$DUZEN[] = $r;
}
}
i can read all data with this code which have same group id. But data aline random.
You have to use mysql order clause in your query like order by id asc. Which you can use at the end of your query.
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"]." order by id asc";
Your sql query should be like given below...
$sql = "SELECT * FROM siparis_ana where grupid = " . $_GET['grupid'] . " ORDER BY id asc ";

sql statement select from DB limit didn't work

i have a question which is my limit statement didn't work i want the content select from database and limit the show content in 40 only but it didn't work
here is my SQL statement with php code
$chatroomID=$_GET['chatroomID'];
$userID = $_SESSION['id'];
$sql="SELECT * FROM chatroom_chat WHERE chatroom_id ='$chatroomID'";
$result1 = mysqli_query($connection, $sql) or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($result1)) {
$chat = $row['chat_id'];
$sql3 ="SELECT * FROM (
SELECT * FROM chat WHERE id = '$chat' ORDER BY id DESC LIMIT 0,40
) sub
ORDER BY id ASC ";
$getChatData = mysqli_query($connection,$sql3) or die(mysqli_error($connection));
/*here have statement to get username*/
while($row3 = mysqli_fetch_array($getChatData)) {
echo "<div>all content</div>";
}
}
does my code have any syntax error? i no sure why it didn't work
SELECT * FROM (
SELECT * FROM chat WHERE id = '$chat' ORDER BY id DESC LIMIT 40
) sub
ORDER BY id ASC

Where and Order By clause not working

So this piece of code doesn't work and I can't figure it out.
$productid = (isset($_REQUEST['productId'])) ? $_REQUEST['productId'] : '';
$query = "SELECT * FROM products WHERE productId = '$productid' ORDER BY rand() limit 3";
Perhaps try:
$productid = (isset($_REQUEST['productId'])) ? $_REQUEST['productId'] : '';
if ($productid != ''){
$query = "SELECT * FROM products WHERE productId = '$productid' ORDER BY rand() limit 3";
} else {
$query = "SELECT * FROM products ORDER BY rand() limit 3";
}
This should return all products if the productId isn't coming over.

String to Query for All Records in Table

When I pull each leadstatus individually (?leadstatus=New, ?leadstatus=Hot, etc.) they work, but when trying to get All, I can't seem to get it to work. The default on the page is New leads as you can see.
`$query = "SELECT * FROM contacts WHERE contacttype IN ('New','Buyer','Seller','Buyer / Seller','Investor') AND leadstatus = 'New' ORDER BY date DESC";
if(isset($_GET['leadstatus']) && in_array($_GET['leadstatus'], array('New', 'Hot', 'Warm', 'Cold', 'Rejected', 'Closed')))
{
$status = $_GET['leadstatus'];
$query = "SELECT * FROM contacts WHERE leadstatus = '".$status."' ORDER BY contacts.date DESC";
}`
Here are some of the strings I've tried with no luck:
?leadstatus=New&leadstatus=Hot&leadstatus=Warm&leadstatus=Rejected&leadstatus=Cold - Only pulls last listed, which is Cold
?leadstatus[]=New&leadstatus=[]Hot&leadstatus[]=Warm&leadstatus[]=Rejected&leadstatus[]=Cold - Returns default, which is New
?leadstatus=New&Hot&Warm&Rejected&Cold
Returns default, which is New
if(isset($_GET['leadstatus']) && $_GET['leadstatus'] == "all") {
$query = "SELECT * FROM contacts ORDER BY contacts.date DESC";
} else if (in_array($_GET['leadstatus'], array('New', 'Hot', 'Warm', 'Cold', 'Rejected', 'Closed'))) {
$status = $_GET['leadstatus'];
$query = "SELECT * FROM contacts WHERE leadstatus = '".$status."' ORDER BY contacts.date DESC";
}
Then, make leadstatus = all.
Try this:
if(isset($_GET['leadstatus']) && in_array($_GET['leadstatus'], array('New', 'Hot', 'Warm', 'Cold', 'Rejected', 'Closed')))
{
$status = $_GET['leadstatus'];
if(!empty($status)) {
$query = "SELECT * FROM contacts WHERE leadstatus = '".$status."' ORDER BY contacts.date DESC";
} else {
$query = "SELECT * FROM contacts ORDER BY contacts.date DESC";
}
}`
However, may I also suggest that you use a parameterized query? You are wide open to a SQL Injection attack here.
Something like this should match multiple conditions, allowing you to mix-and match several at a time, rather than 1 or all.
$status = join(',',$_GET['leadstatus']);
$query = "SELECT * FROM contacts WHERE leadstatus IN($status) ORDER BY contacts.date DESC";

set default based on several criteria

These links give me results for each when clicked, however how do I get 'All' to display all the 'Hot' 'Warm' and 'Cold' leads because 'All' is the default page?
<li>All</li>
<li>Appointments</li>
<li>Hot</li>
<li>Warm</li>
<li>Cold</li>
if(isset($_GET['contactstatus'])
&& in_array($_GET['contactstatus'], array('Hot', 'Warm', 'Cold')))
{
$status = $_GET['contactstatus'];
$query = "SELECT * FROM contacts WHERE contactstatus = '".$status."' ORDER BY contacts.firstname ASC";
}
if(isset($_GET['type'])
&& in_array($_GET['type'], array('Appointment')))
{
$todotype = $_GET['type'];
$query = "SELECT * FROM contacts,contacttodo,contactnotes WHERE contacts.ID = contacttodo.contacts_id = contactnotes.contacts_id AND contacttodo.type = '".$todotype."' ORDER BY contacts.firstname ASC";
}
UPDATE:
Got this to work by adding:
$query = "SELECT * FROM contacts WHERE contactstatus = 'Hot' OR contactstatus = 'Warm' OR contactstatus = 'Cold' ORDER BY contacts.contacttype ASC";
However, is this safe?
It's certainly 'safe', as long as you're never going to have any other contact statuses besides hot, warm, or cold.

Categories