papaJ Posted June 15, 2008 Share Posted June 15, 2008 Hello there, I'm pretty new to PHP but an old hand at front-end stuff. I'm trying to insert some data into a table but I seem to be having a problem. Here's the code I'm using: $dbHost = 'localhost'; $dbUser = 'xxxxx'; $dbPassword = 'xxxxxx'; $dbName = 'xxxxx'; $dbConnect = mysql_connect($dbHost, $dbUser, $dbPassword) or die ('Error connecting to mysql'); $dbSelect = mysql_select_db($dbName); $sql="INSERT INTO articles (id, date, artist, title, label, short_text, long_text, thumbnail, image, tracklisting, vendor_1, vendor_1_url, vendor_2, vendor_2_url, vendor_3, vendor_3_url ) VALUES ('$_POST[id]','$_POST[date]','$_POST[artist]','$_POST[title]','$_POST[label]','$_POST[intro_text]','$_POST[long_text]','$_POST[thumb]','$_POST[image]','$_POST[tracklisting]', '$_POST[vendor1]','$_POST[vendor1Url]','$_POST[vendor2]','$_POST[vendor2Url]','$_POST[vendor3]','$_POST[vendor3Url]')"; if (!mysql_query($sql,$dbConnect)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($dbConnect); And here's the form I'm using: <form id="form1" action="includes/postDb.php"> <table border="0" cellspacing="5" cellpadding="5"> <tr> <td> <label for="id">ID</label></td> <td><input type="text" name="id" id="id" /></td> </tr> <tr> <td width="100"><label for="artist">Artist</label></td> <td width="300"><input type="text" name="artist" id="artist" /></td> </tr> <tr> <td> <label for="title">Title</label></td> <td><input type="text" name="title" id="title" /></td> </tr> <tr> <td> <label for="label">Label</label></td> <td><input type="text" name="label" id="label" /></td> </tr> <tr> <td valign="top"><label for="intro_text">Intro Text</label></td> <td><textarea name="intro_text" id="intro_text" cols="45" rows="5"></textarea></td> </tr> <tr> <td valign="top"><label for="long_text">Long Text</label></td> <td><textarea name="long_text" id="long_text" cols="45" rows="5"></textarea></td> </tr> <tr> <td><label for="thumb">Thumbnail URL</label></td> <td><input type="text" name="thumb" id="thumb" /></td> </tr> <tr> <td><label for="image">Image URL</label></td> <td><input type="text" name="image" id="image" /></td> </tr> <tr> <td><label for="tracklisting">Tracklist XML</label></td> <td><input type="text" name="tracklisting" id="tracklist" /></td> </tr> <tr> <td><label for="vendor1">Vendor 1</label></td> <td><input type="text" name="vendor1" id="vendor1" /></td> </tr> <tr> <td><label for="vendor1Url">Vendor 1 URL</label></td> <td><input type="text" name="vendor1Url" id="vendor1Url" /></td> </tr> <tr> <td><label for="vendor2">Vendor 2</label></td> <td><input type="text" name="vendor2" id="vendor2" /></td> </tr> <tr> <td><label for="vendor2Url">Vendor 2 URL</label></td> <td><input type="text" name="vendor2Url" id="vendor2Url" /></td> </tr> <tr> <td><label for="vendor3">Vendor 3</label></td> <td><input type="text" name="vendor3" id="vendor3" /></td> </tr> <tr> <td><label for="vendor3Url">Vendor 2 URL</label></td> <td><input type="text" name="vendor3Url" id="vendor3Url" /></td> </tr> </table> <input type="submit" class="submit" id="submit" value="Submit" /> </form> When I submit the form I don't get any errors, and the row gets added to the database but all the fields are blank (apart from id which is my table index). Here's a screenshot of my table from phpMyAdmin: If anyone could shed any light I'll be their best friend for ever and ever. Quote Link to comment Share on other sites More sharing options...
40esp Posted June 15, 2008 Share Posted June 15, 2008 You gave all of the information except the error message. What was it? Quote Link to comment Share on other sites More sharing options...
papaJ Posted June 15, 2008 Author Share Posted June 15, 2008 Tha't just the thing - I don't get any error messages. The script says: '1 record added' and the row appears in the table. The problem is that all of the fields are empty, which makes me suspect this might have something to do with the way I've set up my database. I can provide more data on the dB if needed. Quote Link to comment Share on other sites More sharing options...
OriginalBoy Posted June 15, 2008 Share Posted June 15, 2008 Could you link us to the form so we can see what sort of information you are adding. Quote Link to comment Share on other sites More sharing options...
realjumper Posted June 15, 2008 Share Posted June 15, 2008 Break it down to something more simple....... Just echo (say) the value of 'artist'. If you get the result you want, try to insert just 'artist' and see what happens.... $dbHost = 'localhost'; $dbUser = 'xxxxx'; $dbPassword = 'xxxxxx'; $dbName = 'xxxxx'; $dbConnect = mysql_connect($dbHost, $dbUser, $dbPassword) or die ('Error connecting to mysql'); $dbSelect = mysql_select_db($dbName); $artist = $_POST['artist']; echo "$artist "; if the above works, try to insert JUST 'artist' Quote Link to comment Share on other sites More sharing options...
rawb Posted June 15, 2008 Share Posted June 15, 2008 Nothing jumps out at me, but it would be helpful to the debugging process if you would echo that query back before you insert it into the database so you can ensure that your query is what you intended. i.e. $sql="INSERT INTO articles (id, date, artist, title, label, short_text, long_text, thumbnail, image, tracklisting, vendor_1, vendor_1_url, vendor_2, vendor_2_url, vendor_3, vendor_3_url ) VALUES ('$_POST[id]','$_POST[date]','$_POST[artist]','$_POST[title]','$_POST[label]','$_POST[intro_text]','$_POST[long_text]','$_POST[thumb]','$_POST[image]','$_POST[tracklisting]', '$_POST[vendor1]','$_POST[vendor1Url]','$_POST[vendor2]','$_POST[vendor2Url]','$_POST[vendor3]','$_POST[vendor3Url]')"; echo $sql . "<br />"; Quote Link to comment Share on other sites More sharing options...
papaJ Posted June 15, 2008 Author Share Posted June 15, 2008 Thanks for your help guys, I think we're one step closer. I stuck an echo $sql; in like you said and it returned: INSERT INTO articles (id, artist, title, label, short_text, long_text, thumbnail, image, tracklisting, vendor_1, vendor_1_url, vendor_2, vendor_2_url, vendor_3, vendor_3_url ) VALUES ('','','','','','','','','','','','','','','') So it looks like the values from the form aren't being picked up. Do I have to assign them to variables before I use them in the SQL Query? LIke this: $artist = $_POST[artist] Quote Link to comment Share on other sites More sharing options...
realjumper Posted June 15, 2008 Share Posted June 15, 2008 Did you try what I suggested? Quote Link to comment Share on other sites More sharing options...
papaJ Posted June 15, 2008 Author Share Posted June 15, 2008 Just tried it, an it returned 'Error: the query was empty', so I guess the data isn't being picked up from the form. I think I read somewhere that $_POST might not be supported by default, would this have something to do with it? Quote Link to comment Share on other sites More sharing options...
papaJ Posted June 15, 2008 Author Share Posted June 15, 2008 *MASSIVE FOREHEAD SLAP* I missed out the method attribute in the form tag. Why is it always the simplest of things that cause the biggest headaches? Thanks for all your help guys, sorry for (sort of) wasting your time. Quote Link to comment Share on other sites More sharing options...
realjumper Posted June 15, 2008 Share Posted June 15, 2008 I've never heard of $_POST not being supported, but here is a way to kill a couple of birds with one stone....... Make a file called test.php and put this code in it. Enter some text, hit 'submit' and tell me what you get..... <?php if (isset($_POST['submit'])) { $whatever=$_POST['whatever']; echo "$whatever"; exit(); } ?> <html> <head> <title>Test</title> </head> <body> <form name="test" method="POST" action=""> <input type="text" name="whatever"> <input type="submit" name="submit" value="submit"> </body> </html> Quote Link to comment Share on other sites More sharing options...
realjumper Posted June 15, 2008 Share Posted June 15, 2008 *MASSIVE FOREHEAD SLAP* I missed out the method attribute in the form tag. Why is it always the simplest of things that cause the biggest headaches? Thanks for all your help guys, sorry for (sort of) wasting your time. Uh huh......more coffee is the answer :-) Quote Link to comment 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.