tbritny Posted April 16, 2009 Share Posted April 16, 2009 Hi! I am very much a newbie to PHP(I started yesterday) I am working on learning how to get info into a database from a web form, but I have hit a road block pretty early on. My problem started when I entered the code from $dbc to mysql_close($dbc). More specifically this line " $result = mysql_query($dbc, $query) " is what is noted in the error message. I checked to make sure my location, user name, password, database name and table name were correct with my hosting company and that seems to be fine. More information: * PHP Version 4.4.8 * Hosting - MediaTemple Grid-Service This is the error message: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /web/web/web/web/web/web/website.com/html/test/report.php on line 35 Error querying database. Here is my code (copied from a book tutorial): <?php $first_name = $_POST['firstname']; $last_name = $_POST['lastname']; $when_it_happened = $_POST['whenithappened']; $how_long = $_POST['howlong']; $how_many = $_POST['howmany']; $alien_description = $_POST['aliendescription']; $what_they_did = $_POST['whattheydid']; $fang_spotted = $_POST['fangspotted']; $email = $_POST['email']; $other = $_POST['other']; $dbc = mysql_connect('location', 'username', 'password', 'database_name') or die('Error connecting to MySQL server.'); $query = "INSERT INTO table_name (first_name, last_name, " . "when_it_happened, how_long, how_many, alien_description, " . "what_they_did, fang_spotted, other, email) " . "VALUES ('Sally', 'Jones', '3 days ago', '1 day', 'four', " . "'green with six tentacles', 'We just talked and played with a dog', " . "'yes', 'I may have seen your dog. Contact me.', " . "'sally@gregs-list.net')"; $result = mysql_query($dbc, $query) or die('Error querying database.'); mysql_close($dbc); echo 'Thanks for submitting the form.<br />'; echo 'You were abducted ' . $when_it_happened; echo ' and were gone for ' . $how_long . '<br />'; echo 'Number of aliens: ' . $how_many . '<br />'; echo 'Describe them: ' . $alien_description . '<br />'; echo 'The aliens did this: ' . $what_they_did . '<br />'; echo 'Was Fang there? ' . $fang_spotted . '<br />'; echo 'Other comments: ' . $other . '<br />'; echo 'Your email address is ' . $email; ?> Thanks for any help you can give me! Let me know if more information is needed. Quote Link to comment https://forums.phpfreaks.com/topic/154395-solved-error-querying-database-web-form-to-database/ Share on other sites More sharing options...
gurroa Posted April 16, 2009 Share Posted April 16, 2009 As written in the manual: resource mysql_query ( string $query [, resource $link_identifier ] ) So try this: $result = mysql_query($query, $dbc) or better (if you have only one connection): $result = mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/154395-solved-error-querying-database-web-form-to-database/#findComment-811796 Share on other sites More sharing options...
.josh Posted April 16, 2009 Share Posted April 16, 2009 Also, $dbc = mysql_connect('location', 'username', 'password', 'database_name') or die('Error connecting to MySQL server.'); I hope that you just changed the location, username and password arguments to that for posting purposes, because you need to put the real info in there for it to work. Also, the 'database_name' does not go in the mysql_connect. You need to specify the database name with mysql_select_db after the mysql_connect Quote Link to comment https://forums.phpfreaks.com/topic/154395-solved-error-querying-database-web-form-to-database/#findComment-811806 Share on other sites More sharing options...
tbritny Posted April 16, 2009 Author Share Posted April 16, 2009 Thank you, I just utilized both of your suggestions! I am not getting the line error anymore, just the Error Querying Database part, but I'll keep working it. And yes, I used fake information for posting here...I checked with the hosting company and all that information is correct on my actual code. Anyway, thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/154395-solved-error-querying-database-web-form-to-database/#findComment-811823 Share on other sites More sharing options...
.josh Posted April 16, 2009 Share Posted April 16, 2009 change your die to: die(mysql_error() . "<br/>query: " . $query); it will post what the actual error is and what you are trying to send to the db. Maybe you mispelled a table/column name (they are case sensitive even). Quote Link to comment https://forums.phpfreaks.com/topic/154395-solved-error-querying-database-web-form-to-database/#findComment-811826 Share on other sites More sharing options...
tbritny Posted April 16, 2009 Author Share Posted April 16, 2009 Thank you for your help, that completely solved my problem. There was more information going into the table than there were columns. It is working perfectly now. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/154395-solved-error-querying-database-web-form-to-database/#findComment-811838 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.