Jump to content
xisto Community
outwarsecrets

Code Problem

Recommended Posts

here is the code from the entire page. Everything seems to work except for the update query. Any suggestions on how I would go about fixing this?

<?php include ('includes/header.php'); ?><table cellspacing="1" cellpadding="1" border="0" width="100%" bgcolor="000000">  <tr>     <td align="center">Car Dealer</td>  </tr>  <tr>     <td bgcolor="444444"><?php$query = mysql_query("SELECT * FROM `cars` ORDER BY `price` desc LIMIT 0 , 30"); while ($rows = mysql_fetch_array($query)){ $carid = $rows['id']; $price = $rows['price'];$year = $rows['year'];$name = $rows['name'];$image = $rows['image'];$description = $rows['description']; echo"$year $name for \$$price <a href=?buycar=$carid>Buy</a><br>"; } //check to see if there has been a call to delete a row if($_GET['buycar']){ //check to make sure that id exists   $check = mysql_num_rows(mysql_query("SELECT * FROM `cars` WHERE `id` = '$carid'"));     if($check < '1'){       echo"Invalid ID";     }else{       mysql_query("UPDATE `users` SET car='$carid', money=(money - price) WHERE uname='{$uname}'");       echo"Car has been purchased.";     }  }?></td>  </tr></table><?php include ('includes/footer.php'); ?>

this is also what the page looks like

1992 Saturn S-Series for $600 Buy
1987 Toyota Tercel for $500 Buy
1991 Chrysler Town and Country for $400 Buy
1989 Honda Accord for $350 Buy
1993 Ford Ranger for $300 Buy
1995 Geo Metro for $200 Buy

the words Buy are links like the 1995 Geo Metro for $200 has the link dealer.php?buycar=5

If anyone can help me out I would really appreciate it.

Share this post


Link to post
Share on other sites

I have checked, the db connection works fine. The list of cars at the bottom was taken from the database. Alos the other table is coonected correctly too. the users table has the following fields: uname, car, and money. Those all match up fine with what i have there.

Share this post


Link to post
Share on other sites

I'm not quite clearly about your problem,is that something wrong with this:mysql_query("UPDATE `users` SET car='$carid', money=(money - price) WHERE uname='{$uname}'");then try:mysql_query("UPDATE `users` SET car='$carid', money=($money - $price) WHERE uname='$uname'");OR:mysql_query("UPDATE `users` SET car=\"".$carid."\", money=($money - $price) WHERE uname=\"".$uname."\"");

Share this post


Link to post
Share on other sites

i don't think it is something wrong withmysql_query("UPDATE `users` SET car='$carid', money=(money - price) WHERE uname='{$uname}'");because i use it on another page on the site and that works fine. I will give it a try, but i think it is something with the if/else statements somewhere. Because everything works except when u click the link. It says "The car has been purchased" but nothing in the database changes. If anyone knows another way I could write something like that I would try to get that to work.

Share this post


Link to post
Share on other sites

i don't think it is something wrong withmysql_query("UPDATE `users` SET car='$carid', money=(money - price) WHERE uname='{$uname}'");


Are you sure?
in php,every vary name should be followed by a "$"
this:
money=(money - price)
may cause problem,or the value of the
money equals 1,that's because of the operation of '()'

Share this post


Link to post
Share on other sites

I'd go so far as to try:

sqlstring="UPDATE `users` SET car=\"".$carid."\", money=".($money - $price)." WHERE uname=\"".$uname."\"";
or
sqlstring="UPDATE `users` SET car=".$carid.", money=".($money - $price)." WHERE uname=\"".$uname."\"";
followed by
mysql_query(sqlstring);
Depending on if carid is a varchar or int. I assume that the money field is an int type.

Anyway, you could always try to echo the sqlstring and commenting out the querying. That way you can see exactly what your code is trying to execute.

And you might want to check that uname='{$uname}'" shouldn't be uname='$uname'".

Share this post


Link to post
Share on other sites

 

echo"$year $name for \$$price <a href=?buycar=$carid>Buy</a><br>"; 
just a little problem, you forgot an " " (space) :D

  $check = mysql_num_rows(mysql_query("SELECT * FROM `cars` WHERE `id` = '$carid'")); 
I belive this ` is not really needed

mysql_query("UPDATE `users` SET car='$carid', money=(money - price) WHERE uname='{$uname}'"); 
first, you must put an $ before "price". And what is this {}?? Erase it. It will be:

mysql_query("UPDATE users SET car='$carid', money=(money - $price) WHERE uname='$uname'"); 

Here is all the errors I've saw here. Btw, what is the problem with this? Had u received an error msg? Post here..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.