cpharry Posted April 24, 2009 Share Posted April 24, 2009 Hey everyone, I have a form that goes to a processing page, there is a drop down menu on the form that gets locations from a database. The code for the form page is first and then process page. <? // Connect database mysql_connect("localhost","USERNAME TAKEN OUT","PASSWORD TAKEN OUT"); mysql_select_db("wowbasec_class"); // If submitted, check the value of "select". If its not blank value, get the value and put it into $select. if(isset($select)&&$select!=""){ $select=$_GET['select']; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> </head> <body> <table width="70" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#00FFFF"> <tr> <td height="34"><div align="center">Character Add Page </div></td> </tr> <tr> <td height="208"><form action="addcharfile.php" method="post" name="form1"> <table width="500" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="191"><div align="right">Name:</div></td> <td width="309"><input name="name" type="text" id="name"></td> </tr> <tr> <td><div align="right">Location:</div></td> <td><select name="location" id="location"> <option value="">--- Select ---</option> <? // Get records from database (table "name_list"). $list=mysql_query("select * from location order by id asc"); // Show records by while loop. while($row_list=mysql_fetch_assoc($list)){ ?> <option value="<? echo $row_list['id']; ?>" <? if($row_list['id']==$select){ echo "selected"; } ?>><? echo $row_list['name']; ?></option> </select> </td> </tr> <tr> <td><div align="right">Health:</div></td> <td><input name="health" type="text" id="health"></td> </tr> <tr> <td><div align="right">Mana:</div></td> <td><input name="mana" type="text" id="mana"></td> </tr> <tr> <td><div align="right">Notes:</div></td> <td><textarea name="notes" id="notes"></textarea></td> </tr> <tr> <td colspan="2"><div align="center"> <input type="submit" name="Submit" value="Add"> </div></td> </tr> </table> </form> </td> </tr> <tr> <td> </td> </tr> </table> </body> </html> <? // End while loop. } ?> <? $connection = mysql_connect("localhost","USERNAME TAKEN OUT","PASSWORD TAKEN OUT"); mysql_select_db("wowbasec_class", $connection); // Get values from form. $name=$_POST['name']; $location=$_POST['location']; $health=$_POST['health']; $mana=$_POST['mana']; $notes=$_POST['notes']; // Insert all parameters into database. // The id field is auto increment. You don't have to insert any value $sql = mysql_query("insert into char(name, location, health, mana, notes) values('$name', '$location', '$health', '$mana', '$notes')"); if (!mysql_query($sql,$connection)) { die('Error: ' . mysql_error()); } echo "1 record added"; // Close database connection mysql_close(); ?> Any help is apreciated, Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/155556-query-is-empty/ Share on other sites More sharing options...
rckehoe Posted April 24, 2009 Share Posted April 24, 2009 Try this for your insert quert: $sql = mysql_query("insert into char(`name`,`location`,`health`,`mana`,`notes`) values('$name', '$location', '$health', '$mana', '$notes')"); Quote Link to comment https://forums.phpfreaks.com/topic/155556-query-is-empty/#findComment-818616 Share on other sites More sharing options...
cpharry Posted April 24, 2009 Author Share Posted April 24, 2009 Nope still same error rckehoe. Quote Link to comment https://forums.phpfreaks.com/topic/155556-query-is-empty/#findComment-818619 Share on other sites More sharing options...
rckehoe Posted April 24, 2009 Share Posted April 24, 2009 sorry...I forgot to add: $sql = mysql_query("insert into `char` (`name`,`location`,`health`,`mana`,`notes`) values('$name', '$location', '$health', '$mana', '$notes')"); If this doens't work... what error are you getting, you never specified. Unless I missed it. Quote Link to comment https://forums.phpfreaks.com/topic/155556-query-is-empty/#findComment-818622 Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 your query looks fine. Instead of using mysql_query($sql,$connection) connect to your Database before you do the query, then simply do mysql_query($sql) that might work. hope that helped Quote Link to comment https://forums.phpfreaks.com/topic/155556-query-is-empty/#findComment-818623 Share on other sites More sharing options...
cpharry Posted April 24, 2009 Author Share Posted April 24, 2009 rckehoe when i changed to what you put i get the error. Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/155556-query-is-empty/#findComment-818626 Share on other sites More sharing options...
cpharry Posted April 24, 2009 Author Share Posted April 24, 2009 Does anyone know what that error message means? Its know inserting the information but gives that error. Quote Link to comment https://forums.phpfreaks.com/topic/155556-query-is-empty/#findComment-818654 Share on other sites More sharing options...
rckehoe Posted April 29, 2009 Share Posted April 29, 2009 It means that your SQL statement is messed up somewhere... It may have to do with the information that you are passing... You can try this: Change from this: values('$name', '$location', '$health', '$mana', '$notes') to this: values(\"$name\",\"$location\",\"$health\",\"$mana\",\"$notes\") and make sure you use the addslashes function on the variables you are passing to the query: http://us.php.net/addslashes Hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/155556-query-is-empty/#findComment-822182 Share on other sites More sharing options...
Kieran Menor Posted April 29, 2009 Share Posted April 29, 2009 Oh come on! $sql = mysql_query("insert into char(name, location, health, mana, notes) values('$name', '$location', '$health', '$mana', '$notes')"); if (!mysql_query($sql,$connection)) You're doing the query twice. Try: $sql = "INSERT INTO char (name, location, health, mana, notes) VALUES ('$name', '$location', '$health', '$mana', '$notes')"; if (!mysql_query($sql,$connection)) Quote Link to comment https://forums.phpfreaks.com/topic/155556-query-is-empty/#findComment-822192 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.