View Single Post
  #2 (permalink)  
Old 08-07-2008, 08:18 AM
curtiss's Avatar
curtiss curtiss is offline
Moderator
 
Join Date: May 2003
Posts: 1,468
I see a number of things I would change in your code, but I can't yet say, specifically, where the error is being generated (in fact, from the code you provided, I don't even see where you're printing out the error, so that you would know that an error was occurring).

Please adjust your code to look like:
PHP Code:
<?php
$dbhost 
'mysql3.freehostia.com';
$dbuser 'vinmar10_net';
$dbpass 'Master';

$link mysql_connect($dbhost$dbuser$dbpass) or die                      ('Error connecting to mysql');

$username mysql_real_escape_string($_POST['entered_username'],$link);

$dbname 'vinmar10_net';
mysql_select_db($dbname);

if(
$sql_query mysql_query("SELECT `extcredits2` FROM `cdb_members` WHERE `username` = '$username'")) {
while(
$rs mysql_fetch_array($sql_query)) {
$creditamt $rs[0] + 1;
}
$sql_query "UPDATE cdb_members SET $creditamt = extcredits2 +1000 WHERE username = $username";
if(
mysql_query($sql_query)) {
echo 
"Credits Purchased Successfully";
}
else {
die(
"The procedure died while trying to add credits. Following is the error that was generated:\n\r".mysql_error());
}
else {
die(
"The procedure died while trying to retrieve information from the database. Following is the error that was generated:\n\r".mysql_error());
}

?>
Now, at least, you'll get a specific error generated, and you will know based on the text ahead of it which query caused the error.

Now, onto some other things:
1) I noticed you are not escaping the username. That's very bad practice. Always be sure to escape information you'll be sending to a database (especially information acquired directly from the user). Check out my latest blog entry to see a little bit about why and how: Avoiding SQL Injection with PHP | HTMLCenter - Web Help and Web Development Blog
I've taken the liberty of escaping your data in the code I provided above.
2) Your UPDATE query is very strange. It appears as though you're going to retrieve a numerical value from the database, then you're trying to use that numerical value as the name of a column in your update query. That's not going to work.
Basically, let's say that you retrieve the number "7" from your database, and you store that, plus 1 (which could either be 8 or 71, depending on whether PHP thinks it's a number or a string, and without casting the variable, you really have no reliable way of knowing), into the variable "$creditamt".
Then, with your UPDATE query written the way it is, it's going to look like:
PHP Code:
UPDATE cdb_members SET 8 extcredits2 +1000 WHERE username $username 
First of all, obviously "8" (or 71, depending on typing), is most likely not the name of your column.
Secondly, you have not surrounded $username by quotes, so the query's going to fail there, too, assuming that your username is a string and not a numerical value.

Your UPDATE query should probably look more like:
PHP Code:
UPDATE cdb_members SET extcredits2=extcredits+1000 WHERE username='$username' 
Good luck with it, and let us know if there's anything more we can do to help. Thanks.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote