UPDATE ???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

cronika
Forum Commoner
Posts: 27
Joined: Sun Jul 26, 2009 2:28 pm
Location: romania
Contact:

UPDATE ???

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: UPDATE ???

Post by jackpf »

Because your form action is pointing to a directory called insert_xp.

Does that directory exist?
cronika
Forum Commoner
Posts: 27
Joined: Sun Jul 26, 2009 2:28 pm
Location: romania
Contact:

Re: UPDATE ???

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: UPDATE ???

Post by jackpf »

If it's a page then it needs an extension.
cronika
Forum Commoner
Posts: 27
Joined: Sun Jul 26, 2009 2:28 pm
Location: romania
Contact:

Re: UPDATE ???

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: UPDATE ???

Post 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().
cronika
Forum Commoner
Posts: 27
Joined: Sun Jul 26, 2009 2:28 pm
Location: romania
Contact:

Re: UPDATE ???

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: UPDATE ???

Post 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')
cronika
Forum Commoner
Posts: 27
Joined: Sun Jul 26, 2009 2:28 pm
Location: romania
Contact:

Re: UPDATE ???

Post 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'])
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: UPDATE ???

Post by jackpf »

Yes. But as a side not, you really should run some validation on it before you run it in a query.
cronika
Forum Commoner
Posts: 27
Joined: Sun Jul 26, 2009 2:28 pm
Location: romania
Contact:

Re: UPDATE ???

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: UPDATE ???

Post by jackpf »

Is `PRIMARY` auto increment?

What's your code?
cronika
Forum Commoner
Posts: 27
Joined: Sun Jul 26, 2009 2:28 pm
Location: romania
Contact:

Re: UPDATE ???

Post by cronika »

user_id si auto increment
cronika
Forum Commoner
Posts: 27
Joined: Sun Jul 26, 2009 2:28 pm
Location: romania
Contact:

Re: UPDATE ???

Post 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 ;
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: UPDATE ???

Post by jackpf »

What's your code for making a new user?
Post Reply