Jump to content

Use query to list data from db and update value on the first row from query


chrihb07

Recommended Posts

What is wrong with this code?

I am workin on a order system that confirms orders. I want to list the oldest order and then confirm the order, To to this I have queried out all the unconfirmed orders and listed the oldest to the screen. Then i have made an update query to change the unconfirmed order in the database to confirmed.

 

Here are the code:

 

$sql =  "SELECT carts.cart_id ,carts.fname, carts.lname, carts.country, carts.city, carts.street, carts.postnum,

          carts.epost, carts.comm, carts.delivery_name, carts.delivery_country, carts.admin_status,

          carts.delivery_city, carts.delivery_address, carts.delivery_postnum, carts.payment_method,

  carts.admin_status, cart_prods.cart_id, cart_prods.store_id, cart_prods.pname, cart_prods.pprice,

  cart_prods.amount, cart_prods.props

        FROM  carts

INNER JOIN cart_prods

ON carts.cart_id = cart_prods.cart_id

WHERE  carts.cart_id > 1

AND (status ='AUTHORIZED' AND admin_status = 1)

ORDER BY created DESC";

 

 

$result = mysql_query($sql);

 

if (!$result) {

    echo "Could not successfully run query ($sql) from DB: " . mysql_error();

    exit;

}

 

if (mysql_num_rows($result) == 0) {

    echo "Ingen ordre igjen:-) Godt jobbet!!!";

    exit;

}

 

$i=0;

while (($row = mysql_fetch_array($result)) && ($i<1)){

$totalt = $row["pprice"] * $row["amount"];

    echo "<br>  Bestilte Produkter: <br> " ;

echo "<br> Store Id: ";

echo $row["store_id"];

echo "<br> Navn: ";

echo $row["pname"];

echo "<br> Pris: ";

echo $row["pprice"];

echo "<br> Antall: ";

echo $row["amount"];

echo "<br> Totalt: ";

echo $totalt;

//echo "<br> Properties: ";

//echo $row["props"];

echo "<br> ";

echo "<br>  Personlig informasjon: <br> " ;

echo " <br> Fornavn: ";

echo $row["fname"];

echo "<br> Etternavn: ";

echo $row["lname"];

echo "<br>  Land: ";

echo $row["country"];

echo "<br>  By: ";

echo $row["city"];

echo "<br>  Gateadresse: ";

echo $row["street"];

echo "<br>  Postnr: ";

echo $row["postnum"];

echo "<br>  E-Post: ";

echo $row["epost"];

echo "<br>  Kommentar: <br> ";

echo $row["comm"];

echo "<br> Leveringsadresse:<br> ";

echo "<br>  Navn: ";

echo $row["delivery_name"];

echo "<br>  Land: ";

echo $row["delivery_country"];

echo "<br>  By: ";

echo $row["delivery_city"];

echo "<br>  Etternavn: ";

echo $row["delivery_address"];

echo "<br>  Postnr: <br> ";

echo $row["delivery_postnum"];

echo "<br>  Betalingsmetode: <br> ";

echo $row["payment_method"];

echo "<br> ";

/*echo "<br>  Status: <br> ";

echo $row["admin_status"];

echo "<br> ";

echo "<br> ";

*/$i++;

 

}

 

 

$per = mysql_fetch_array($result);{

$query = mysql_query("UPDATE carts

      Set admin_status = 5

      WHERE 'cart_id' = .'$per'. 'cart_id'");

  }

 

mysql_free_result($result);

 

Link to comment
Share on other sites

When I run the code it generates all orders that has the status = AuTHORIZED. The update function will not update the value on admin_status. When I check the db the value is still the same. My plan is to add a line in the first query, so that the query only list out status = AUTHORIZED and admin_status = 2. This is so that I only list out the unconfirmed orders. 

Link to comment
Share on other sites

 

What is wrong with this code?

I am workin on a order system that confirms orders. I want to list the oldest order and then confirm the order, To to this I have queried out all the unconfirmed orders and listed the oldest to the screen. Then i have made an update query to change the unconfirmed order in the database to confirmed.

 

Here are the code:

 

 $sql =  "SELECT carts.cart_id ,carts.fname, carts.lname, carts.country, carts.city, carts.street, carts.postnum, 
          carts.epost, carts.comm, carts.delivery_name, carts.delivery_country, carts.admin_status,
          carts.delivery_city, carts.delivery_address, carts.delivery_postnum, carts.payment_method, 
	  carts.admin_status, cart_prods.cart_id, cart_prods.store_id, cart_prods.pname, cart_prods.pprice, 
	   cart_prods.amount, cart_prods.props
        FROM   carts
	INNER JOIN cart_prods
	ON carts.cart_id = cart_prods.cart_id
	 WHERE  carts.cart_id > 1
	AND (status ='AUTHORIZED' AND admin_status = 1)
	ORDER BY created DESC";
  

	$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "Ingen ordre igjen:-) Godt jobbet!!!";
    exit;
}

$i=0;
while (($row = mysql_fetch_array($result)) && ($i<1)){
$totalt = $row["pprice"] * $row["amount"];
    echo "<br>  Bestilte Produkter: <br> " ;
echo "<br> Store Id: ";
echo $row["store_id"];
echo "<br> Navn: ";
echo $row["pname"];
echo "<br> Pris: ";
echo $row["pprice"];
echo "<br> Antall: ";
echo $row["amount"];
echo "<br> Totalt: ";
echo $totalt;
//echo "<br> Properties: ";
//echo $row["props"];
echo "<br> ";
echo "<br>  Personlig informasjon: <br> " ;
echo " <br> Fornavn: ";
echo $row["fname"];
echo "<br> Etternavn: ";
echo $row["lname"];
echo "<br>  Land: ";
echo $row["country"];
echo "<br>  By: ";
echo $row["city"];
echo "<br>  Gateadresse: ";
echo $row["street"];
echo "<br>  Postnr: ";
echo $row["postnum"];
echo "<br>  E-Post: ";
echo $row["epost"];
echo "<br>  Kommentar: <br> ";
echo $row["comm"];
echo "<br> Leveringsadresse:<br> ";
echo "<br>  Navn: ";
echo $row["delivery_name"];
echo "<br>  Land: ";
echo $row["delivery_country"];
echo "<br>  By: ";
echo $row["delivery_city"];
echo "<br>  Etternavn: ";
echo $row["delivery_address"];
echo "<br>  Postnr: <br> ";
echo $row["delivery_postnum"];
echo "<br>  Betalingsmetode: <br> ";
echo $row["payment_method"];
echo "<br> ";
/*echo "<br>  Status: <br> ";
echo $row["admin_status"];
echo "<br> ";
echo "<br> ";
*/$i++;

}


$per = mysql_fetch_array($result);{
$query = mysql_query("UPDATE carts
       Set admin_status = 5
       WHERE 'cart_id' = .'$per'. 'cart_id'");
   }

mysql_free_result($result);

Link to comment
Share on other sites

The troubleshooting method that I use to check on queries is to print them.  i.e where you have

$query = mysql_query("UPDATE carts Set admin_status = 5  WHERE 'cart_id' = .'$per'. 'cart_id'");

 

I do

 

$query = "UPDATE carts Set admin_status = 5 WHERE 'cart_id' = .'$per'. 'cart_id'";
print $query;

 

This allows me to

(1) ensure that the query is what I think it should be and

(2) run the query manually against my database and to check the results.

 

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.