Jump to content

inserting strings into a my sql database, using vairables


benthejack

Recommended Posts

hi ive just been slaving over this last problem for the last couple of hours (and am starting to get rather annoyed). ive been creating some database interaction scripts which require the insertion of some unknown strings into a database.the code i have so far is

$string = "some string";$session = 10;$currentTime = time();$hostname="mysql243.secureserver.net";$username="benthejack";$password="**********";$dbname="benthejack";$con = mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later.");mysql_select_db($dbname);mysql_query("INSERT INTO PList (pName, sessionID, timeStamp) VALUES ($string, $session, $currentTime)");

the problem im having is that the INSERT clause fails because of the $string value for some reason.the pName (the column $session goes into) column is a varChar type column, so i dont see why this shouldnt work.it seems to work fine if i put only numbers into the $session string, or if i input a string directly into the INSERT clause.this works:

$string = "185";  mysql_query("INSERT INTO PList (pName, sessionID, timeStamp) VALUES ($string, $session, $currentTime)");

and so does this:

mysql_query("INSERT INTO PList (pName, sessionID, timeStamp) VALUES ('some string', $session, $currentTime)");

but for some reason i cant get the code i need to work:

$string = "some string";  mysql_query("INSERT INTO PList (pName, sessionID, timeStamp) VALUES ($string, $session, $currentTime)");

anyone know why this is?cheersBenthejack

Link to comment
Share on other sites

Okay i just set up a database table and ran your script and here is what i did to get it to work. Get ready to kick your self :) take your insert query:

mysql_query("INSERT INTO PList (pName, sessionID, timeStamp)VALUES ($string, $session, $currentTime)");

and put single quotes around the varaible names like this:

mysql_query("INSERT INTO PList (pName, sessionID, timeStamp)VALUES ('$string', '$session', '$currentTime')");

and that should make it work :)

Link to comment
Share on other sites

heheheh i remember one time in school i was taking my programming final and we were doing the lab part where we had to write a program. Any how i was to code through with ease took me about 45 min to get done then when I went to test it I got an error and sure enough it took me the remaning two hours of class to find that i forgot a quote somewhere in my program.It's seems to be just a basic human nature we tend to over look the small things while looking at the bigger picture.Another quick tip instead of setting a timestamp variable you could do something like this. Have your time field in your database set to datetime. then use the now() function to insert the current time and date. like this

mysql_query("INSERT INTO PList (pName, sessionID, timeStamp)VALUES ('$string', '$session', now())");

that will save you a line of program and insure more vaild data

Link to comment
Share on other sites

I wish I saw this post last night when I was trying to figure out how to update my database from my code. I was making the exact same mistake!! At least I know I'm not alone!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...