Jump to content


Photo

INSERT doesn't INSERT?


  • Please log in to reply
2 replies to this topic

#1 Kia

Kia
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 December 2005 - 10:06 PM

**SOLVED**

Part of a registration code I am using saves user details into a mysql database.

the database is there and has access (logins can be called from the database).
the user fills in a basic form and the details are supposed to be entered into the database.

join.php
$query="INSERT INTO users (uid, username, password, first_name, last_name, country, email, last_paid, signup_date) VALUES ('','$susername', '$spassword', '$sfirst_name', '$slast_name', '$scountry', 'free', '$nowdate')";
            mysql_query($query);

join.php calls another .php early on which contains the database login details.
the user gets a "success screen" and their randomly generated password is emailed to them (this bit works), however the details never make it into the database.

the login screen (I have manually entered several users for test purposes) uses the same .php file to get it's database login info and successfully completes queries to validate the user info.

any suggestions welcomed

#2 Cook

Cook
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationSingapore

Posted 19 December 2005 - 12:50 AM

[!--quoteo(post=328422:date=Dec 19 2005, 06:06 AM:name=Kia)--][div class=\'quotetop\']QUOTE(Kia @ Dec 19 2005, 06:06 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$query="INSERT INTO users (uid, username, password, first_name, last_name, country, email, last_paid, signup_date) VALUES ('','$susername', '$spassword', '$sfirst_name', '$slast_name', '$scountry', 'free', '$nowdate')";
            mysql_query($query);
[/quote]

I see two problems.

1- The number of values does not match the number of fields.
2- If uid is an auto-inc field, you're actually better off leaving it out of the list of fields (then of course you don't provide a value for it) and let MySQL take care of it automatically for you.

And a comment: it is a good idea, and actually best practice, to enclose identifiers with backticks in MySQL queries, so that in case you're using a reserved MySQL word as an identifier, MySQL still treats it as an identifier and does not complain:

$query = "INSERT INTO `users` (`username`, `password`, `first_name`, `last_name`, `country`, `email`, `last_paid`, `signup_date`) VALUES ('$susername', '$spassword', '$sfirst_name', '$slast_name', '$scountry', '$semail', 'free', '$nowdate')";
mysql_query($query);

NB: I assumed email would be stored in $semail...


Cook

#3 Kia

Kia
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 19 December 2005 - 09:29 AM

Thank you

that solved the problem, guess that's what I get for staring at lines of code for several hours straight.

Cheers, much appreciated.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users