Page 1 of 2

UPDATE ???

Posted: Sun Jul 26, 2009 2:32 pm
by cronika
Why is not updating?

Code: Select all

 
 
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
 
mysql_select_db("joc", $con);
if (isset($_POST['submit'])) {
$user_id=$_POST['user_id'];
$xp=$_POST['xp'];
mysql_query("UPDATE xp FROM users SET xp=$_POST['xp'] WHERE  user_id='$user_id'")or die(mysql_error());
 
}
?>
<form action="insert_xp" method="post">
XP : <input type="text" name="xp" />
<input type="submit" value="submit">
</form> 
 
 
In browser i can see the the box where can i insert xp but after i press submit i got this error :


Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.
Error 404
localhost
07/26/09 22:05:17
Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i PHP/5.2.9

Im a rookie in php so please be gentle.

Re: UPDATE ???

Posted: Sun Jul 26, 2009 2:36 pm
by jackpf
Because your form action is pointing to a directory called insert_xp.

Does that directory exist?

Re: UPDATE ???

Posted: Sun Jul 26, 2009 2:37 pm
by cronika
jackpf wrote:Because your form action is pointing to a directory called insert_xp.

Does that directory exist?
Well the code is in the page called insert_xp

Re: UPDATE ???

Posted: Sun Jul 26, 2009 2:44 pm
by jackpf
If it's a page then it needs an extension.

Re: UPDATE ???

Posted: Sun Jul 26, 2009 2:51 pm
by cronika
thank you.

But another problem... in database is not inserting the number i put. I've log in and tried to insert xp and in db is still 0

Re: UPDATE ???

Posted: Sun Jul 26, 2009 3:10 pm
by jackpf
Your update syntax is incorrect.

It's
UPDATE table SET field1='something', field2='somethingelse' WHERE id='an_id'

You should get an error since you're using mysql_error().

Re: UPDATE ???

Posted: Sun Jul 26, 2009 3:30 pm
by cronika
Thank you again.

But another question... :oops: in form if i put 10 it will insert 10 in db, but after i've inserted 10 and i insert again 10 it suppose to show me 20 but it doesn't show. How can i do that?
I've try this with update because someone told me.. but it looks like is not doing what i want.

Re: UPDATE ???

Posted: Sun Jul 26, 2009 3:40 pm
by jackpf
Well look at your code logically, where have you told it to add onto the field?

You'd need to do something like

Code: Select all

SET `field`=(`field`+'$the_new_value')

Re: UPDATE ???

Posted: Sun Jul 26, 2009 3:44 pm
by cronika
jackpf wrote:Well look at your code logically, where have you told it to add onto the field?

You'd need to do something like

Code: Select all

SET `field`=(`field`+'$the_new_value')
You are saying that instead xp=$_POST['xp'] i should do xp=(xp + $_POST['xp'])

Re: UPDATE ???

Posted: Sun Jul 26, 2009 3:47 pm
by jackpf
Yes. But as a side not, you really should run some validation on it before you run it in a query.

Re: UPDATE ???

Posted: Sun Jul 26, 2009 3:52 pm
by cronika
It worked thank you

In db if i make another user and try to add xp it says : Duplicate entry '0' for key 'PRIMARY'
Entry 0 is user admin.

Re: UPDATE ???

Posted: Sun Jul 26, 2009 3:55 pm
by jackpf
Is `PRIMARY` auto increment?

What's your code?

Re: UPDATE ???

Posted: Sun Jul 26, 2009 3:56 pm
by cronika
user_id si auto increment

Re: UPDATE ???

Posted: Sun Jul 26, 2009 3:57 pm
by cronika
this is my db

Code: Select all

-- Table structure for table `users`
--
 
CREATE TABLE IF NOT EXISTS `users` (
  `user_id` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(60) NOT NULL,
  `password` varchar(60) NOT NULL,
  `email` varchar(120) NOT NULL,
  `xp` mediumint(15) NOT NULL,
  `bank` bigint(10) NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

Re: UPDATE ???

Posted: Sun Jul 26, 2009 4:15 pm
by jackpf
What's your code for making a new user?