Jump to content


Photo

I can guarantee this is a stupid question, but...


  • Please log in to reply
5 replies to this topic

#1 habook2

habook2
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 16 June 2006 - 11:51 PM

How do I insert a variable's value into a table?

Like:

mysql_query ('INSERT INTO `users` (`username`, `password`, `userid`) VALUES (\'I WANT A VARIABLE HERE\', \'I WANT A VARIABLE HERE\', NULL);)

When I just tried using the variables, they would actually add "$username" and "$pass" as the username and password.

#2 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 16 June 2006 - 11:54 PM

sql = "INSERT INTO `table` (`field_1`) VALUES ('$field_1')";
z..z..z..z..z..z..z..z..

#3 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 16 June 2006 - 11:55 PM

Variables are only parsed when contained within double quotes, so...
mysql_query("INSERT INTO `users` (`username`, `password`, `userid`) VALUES ('$v1', '$v2', NULL);");


#4 habook2

habook2
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 16 June 2006 - 11:56 PM

No, I mean the value of the variable. If, by a previous form, the variable $username had been set to "flamino", then how would I insert "flamingo" instead of "$username" ?

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 16 June 2006 - 11:56 PM

problem is your quotes. do it like this:
mysql_query("insert into users (username,password,userid) values ('$username','$password','$userid')");
or, if your userid is auto-generated by sql then don't include it in that list:
mysql_query("insert into users (username,password) values ('$username','$password')");

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 17 June 2006 - 12:14 AM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]No, I mean the value of the variable.[/quote]
Just as I showed you!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users