autoglass Posted January 4, 2011 Share Posted January 4, 2011 I am quite new to this and am working on a site of my own. I am wanting to input data into a database based on the values of two textboxes. I have the general engine set up but there are some problems. For example when I do the INSERT with values such as: mysql_query("INSERT INTO PageTable (uniqueid, ImageLink, ImageLink2, IP) VALUES ('3434343', 'http://www.example.com/1.jpg', 'http://www.example.com/1.jpg', '94.123.123.9')"); This will go into the database perfectly. However if I do this: mysql_query("INSERT INTO PageTable (uniqueid, ImageLink, ImageLink2, IP) VALUES ($random2, $img1, $img2, $ip)"); Where random2 is rand() and img1 and img2 are the values of the text boes and ip is the ip of the user. When I do this there is no insert into the database. Another thing I noticed was that if I put in '111111111111111111' into the textbox the value in the database is something like 111111111111111.0904039 or something like that, however if I put '111111111111111111' into the INSERT statement it goes into the database perfectly. Also if I put '1' into the textbox it goes in fine but putting 'a' into the textbox and it doesn't! I am sure I am missing something very very basic but I just can't get my head round this! Any help would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/223351-forms-and-database-help/ Share on other sites More sharing options...
litebearer Posted January 4, 2011 Share Posted January 4, 2011 the VALUES need be encased by single quotes - ie '$var1', '$var2' etc etc Quote Link to comment https://forums.phpfreaks.com/topic/223351-forms-and-database-help/#findComment-1154578 Share on other sites More sharing options...
revraz Posted January 4, 2011 Share Posted January 4, 2011 Put your query in a string, then echo it to validate it. Put single quotes around your variables in your insert statement. Use mysql_error after your query to diagnose sql errors. We also don't see your form code, so hard to say if you're doing that part right as well. Quote Link to comment https://forums.phpfreaks.com/topic/223351-forms-and-database-help/#findComment-1154581 Share on other sites More sharing options...
autoglass Posted January 4, 2011 Author Share Posted January 4, 2011 the VALUES need be encased by single quotes - ie '$var1', '$var2' etc etc Thanks! Ill try that now! what is the reason for this? just out of interest. for example I have not put $random2 in quotes and it works ok, it it because they are varchars? Quote Link to comment https://forums.phpfreaks.com/topic/223351-forms-and-database-help/#findComment-1154582 Share on other sites More sharing options...
autoglass Posted January 4, 2011 Author Share Posted January 4, 2011 Put your query in a string, then echo it to validate it. Put single quotes around your variables in your insert statement. Use mysql_error after your query to diagnose sql errors. We also don't see your form code, so hard to say if you're doing that part right as well. Thanks! I was validating it so I knew the data that should be going in was fine! It looks like the single quotes are working, just didn't realise I needed them! Can you expand on the mysql_error? Quote Link to comment https://forums.phpfreaks.com/topic/223351-forms-and-database-help/#findComment-1154583 Share on other sites More sharing options...
BlueSkyIS Posted January 4, 2011 Share Posted January 4, 2011 Can you expand on the mysql_error? mysql_query("INSERT INTO PageTable (uniqueid, ImageLink, ImageLink2, IP) VALUES ('$random2', '$img1','$img2','$ip')") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/223351-forms-and-database-help/#findComment-1154642 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.