Jump to content

[SOLVED] problem updating info into table


jakebur01

Recommended Posts

ok heres my problem, I am trying to update new information on a row in my mysql database.

 

This works fine:

$query = "UPDATE `orders` SET `c_phone` = '$c_phone', `c_email` = '$c_email', `card_type` = '$card_type', `card_number` = '$card_number', `card_month` = '$card_month', `card_year` = '$card_year', `card_name` = '$card_name', `three_number` = '$amex_code', `total_amount` = '$c_total', `total_cost` = '$t_price', `total_shipping` = '$c_ship' WHERE `orders` .`ship_name` = '$name'";

 

And this way does not:

$query = "UPDATE `orders` SET `c_phone` = '$c_phone', `c_email` = '$c_email', `card_type` = '$card_type', `card_number` = '$card_number', `card_month` = '$card_month', `card_year` = '$card_year', `card_name` = '$card_name', `three_number` = '$amex_code', `total_amount` = '$c_total', `total_cost` = '$t_price', `total_shipping` = '$c_ship' WHERE `orders` .`ship_name` = '$name' and `orders` .`amount` ='$t_price'";

 

I get my problem when I added the  {and `orders` .`amount` ='$t_price'} part on the end. I need to match the new information with the row by using two values " by name and total".

 

I have tried printing the querys out in all different ways. When I paste it into the sql, it accepts it but it has 0 affected rows even when amount does equal $t_price.

Link to comment
Share on other sites

Could be.  I could use just one value if I could get the auto-incremented number off of my last insertion.

 

How would I do this? If on the previous page I inserted data into a new row and has it auto-increment a number under the column `orderid` ? I want to find out what number it created when it inserted the data and store that number into a session.

Link to comment
Share on other sites

when checking against the database the field type shouldn't really matter, the value should.  So if you're orderid in the database is "1234" and the value you're checking with is "1234", it should work.

 

I'm assuming you're using a form to update the other info.  When you pull the other info from the database, pull the orderid as well.  Then submit it (maybe as a hidden input field) with the other info.

 

Then do your validation and run your query.

<?php
//short version
$query = "UPDATE `orders` SET `c_phone` = '$c_phone' WHERE `orderid` = ' " . $_POST['orderid'] . " ' ";
?>

Link to comment
Share on other sites

This is bad programming habit?

 

never update without minuim addslashes and never use the POST in the query ok.

<?php
//short version
$query = "UPDATE `orders` SET `c_phone` = '$c_phone' WHERE `orderid` = ' " . $_POST['orderid'] . " ' ";
?>

 

corrected

 

<?php
//correct version

// database connection.

$c_phone=addslashes($_POST['c_phone']);
$orderid=addslashes($_POST['orderid']);

$query = "UPDATE `orders` SET `c_phone` = '$c_phone' WHERE `orderid` = ' $orderid ' ";
$result=mysql_query($query)or die("mysql_error()");

if(mysql_affected_rows($result)){

echo" database updated";

}else{

echo"database not updated";
}
?>

 

Link to comment
Share on other sites

Your concept is wrong first select the criteria the = what ever then  update and only use a where cluse with no and/&& ok.

 

 

 

example

 

<?php
//correct version

// database connection.


$c_phone=addslashes($_POST['c_phone']);
$orderid=addslashes($_POST['orderid']);

$query="SELECT * FROM `payments` WHERE `c_phone`='442423442423' and `orderid`='3293494'";

$result=mysql_query($query)or die("mysql_error"); 

while($x=mysql_fetch_assoc($result)){

$c_phone=$x['c_phone'];
$orderid=$x['order_id'];

$query1 = "UPDATE `orders` SET `c_phone` = '$c_phone' WHERE `orderid` = ' $orderid ' ";
$result1=mysql_query($query1)or die("mysql_error()");

if(mysql_affected_rows($result1)){

echo" database updated";

}else{

echo"database not updated";
}
}
?>

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.