Forum Moderators: coopster

Message Too Old, No Replies

database not being updated

will not update even though the comand is correct

         

newguy101

4:49 am on Nov 12, 2004 (gmt 0)

10+ Year Member



On my site I am almost finished designing a forum using PHP and MySQL, but the edit feature is not working and I can not figure out why. Here is the code:
<?
$id=$_POST['id'];
$text=$_POST['post'];
$name=$_POST['username'];
$a=$_POST['value'];
echo $a;
echo $text;
echo $id;

$username="*******";
$password="************";
$database="forumguild";
mysql_connect(localhost,$username,$password);

$query="UPDATE genpost$a SET name = '$text' WHERE id = '$id'";
mysql_query($query);
echo "Post Editied";
mysql_close();
?>

The echos are included to make sure that the variables are not blank and they are not. However genpost$a is never updated. Does anyone know what I am doing wrong?

moltar

5:07 am on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WW newguy101!

Looks like you need to issue an update command between the following two lines:


$query="UPDATE genpost$a SET name = '$text' WHERE id = '$id'";
$result = mysql_query($query);
echo "Post Editied";

[edited by: moltar at 5:10 am (utc) on Nov. 12, 2004]

newguy101

5:09 am on Nov 12, 2004 (gmt 0)

10+ Year Member



Ok I have done
echo $query;
and used the information given in PHPMYADMIN. This worked perfectly but my script is still not working. This is one of the most confusing problems I have ever encountered.

newguy101

5:10 am on Nov 12, 2004 (gmt 0)

10+ Year Member



I also realized that mistake right after posting it has been corrected how ever it is still not working.

moltar

5:12 am on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How does the SQL code look after it has been echo'ed?

newguy101

5:15 am on Nov 12, 2004 (gmt 0)

10+ Year Member



It looks like this
UPDATE genpost1 SET name = 'I Hope this works' WHERE id = '11'
the code seems to be working perfectly

moltar

5:18 am on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you sure you are connecting well? Username and password correct? Try adding some error checking like this:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

and

$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}

It will tell you if there were any errors.

newguy101

5:25 am on Nov 12, 2004 (gmt 0)

10+ Year Member



Ok I'm getting this I guess it is my problem, but I'm not sure what to do about it.
Warning: mysql_connect(): Access denied for user: 'mysql_user@localhost' (Using password: YES) in /drive2/fpgshttpd/home/forum/login/general/edited.php on line 12
Could not connect:

Salsa

5:27 am on Nov 12, 2004 (gmt 0)

10+ Year Member



And more:

Put localhost in quotes. You don't have to quote
variable arguments, but you do have to quote strings:

mysql_connect('localhost',$username,$password);

Select a database after you connect:

mysql_select_db($forumguild);

In your query string, are you concatinating $a to genpost, so if $a = x, the table name is genpostx? If not, that's a problem.

$query="UPDATE genpost$a SET name = '$text' WHERE id = '$id'";

The major thing is that you never executed the query:

$result = mysql_query($query);

Always test the result of your query so that if something goes wrong, you know what it is.

if (!$result) die("query error: ".mysql_error());

I hope this helps.

newguy101

5:31 am on Nov 12, 2004 (gmt 0)

10+ Year Member



Nevermind my above post I forgot to input my password...*blushes*
Anyway I figured it out I wasn't selecting a database. Thanks aton for your help. The internet needs more people like you.

Salsa

5:46 am on Nov 12, 2004 (gmt 0)

10+ Year Member



Well, I guess you don't absolutely have to put string arguments in quotes, but if you don't, PHP will assume that it's a defined constant. Then, when it doesn't find such a constant, it will produce an error and, finally, assume that it's simply a string and quote it for you. If you do have a constant defined by that name, however, it definitely won't work (unless the definition is the same as the constant).