unreel Posted January 18, 2008 Share Posted January 18, 2008 I'm trying to take data from a form and send the information to my database. Whenever I try to submit my form, this script handles it. The error that I get is "Query was empty" whenever I try to submit my form. I'm thinking that it has something to do with my sql insert portion, but can't figure out what I am doing wrong. Any help is greatly appreciated. <?php // database information $host = 'localhost'; $user = 'root'; $password = 'mypass'; $dbName = 'mydb'; // connect and select the database $conn = mysql_connect($host, $user, $password) or die(mysql_error()); $db = mysql_select_db($dbName, $conn) or die(mysql_error()); // Check to make sure that the fields weren't empty. if (isset($_POST['first_name']) != '') { $first_name = stripslashes($_POST['first_name']); } if (isset($_POST['last_name']) != '') { $last_name = stripslashes($_POST['last_name']); } if (isset($_POST['email']) != '') { $email = stripslashes($_POST['email']); } // Insert the 3 into the DB $sql="INSERT INTO 'users' (first_name, last_name, email) VALUES('$first_name','$last_name','$email')"; $result = mysql_query($sql, $conn) or die(mysql_error()); ?> Thanks for reading, Brandon Quote Link to comment https://forums.phpfreaks.com/topic/86711-solved-inserting-string-to-db/ Share on other sites More sharing options...
papaface Posted January 18, 2008 Share Posted January 18, 2008 try: $sql="INSERT INTO `users` (first_name, last_name, email) VALUES('{$first_name}','{$last_name}','{$email}')"; $result = mysql_query($sql, $conn) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/86711-solved-inserting-string-to-db/#findComment-443106 Share on other sites More sharing options...
revraz Posted January 18, 2008 Share Posted January 18, 2008 You are striping slashes if its there, but you don't do anything if it's empty, you still try to insert it. Quote Link to comment https://forums.phpfreaks.com/topic/86711-solved-inserting-string-to-db/#findComment-443108 Share on other sites More sharing options...
unreel Posted January 18, 2008 Author Share Posted January 18, 2008 I took away the stripslashes() and changed the sql insert query but it is still giving me the same error. "Query was empty" Quote Link to comment https://forums.phpfreaks.com/topic/86711-solved-inserting-string-to-db/#findComment-443118 Share on other sites More sharing options...
revraz Posted January 19, 2008 Share Posted January 19, 2008 You didn't have to remove the stripslashes, you need to fix it so it doesn't try to INSERT if it's empty. Post your FORM code as well. Quote Link to comment https://forums.phpfreaks.com/topic/86711-solved-inserting-string-to-db/#findComment-443121 Share on other sites More sharing options...
unreel Posted January 19, 2008 Author Share Posted January 19, 2008 Here is the form.php and the updated formhandle.php <?php // database information $host = 'localhost'; $user = 'root'; $password = 'mypass'; $dbName = 'mydb'; // connect and select the database $conn = mysql_connect($host, $user, $password) or die(mysql_error()); $db = mysql_select_db($dbName, $conn) or die(mysql_error()); // insert new entry in the database if entry submitted if (isset($_POST['first_name']) && trim($_POST['first_name']) != '') { $first_name = $_POST['first_name']; $result = mysql_query($sql, $conn) or die(mysql_error()); } if (isset($_POST['last_name']) && trim($_POST['last_name']) != '') { $last_name = $_POST['last_name']; } if (isset($_POST['email']) && trim($_POST['email']) != '') { $email = $_POST['email']; } // Insert the 3 into the DB $sql="INSERT INTO users (first_name, last_name, email) VALUES(' {$first_name} ',' {$last_name} ',' {$email} ')"; $result = mysql_query($sql, $conn) or die(mysql_error()); ?> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form name="form1" method="post" action="formhandle.php"> <p> First Name: <input name="first_name" type="text" id="first_name"> </p> <p>Last name: <input name="last_name" type="text" id="last_name"> </p> <p>Email: <input name="email" type="text" id="email"> </p> <p> <input type="submit" name="Submit" value="Submit"> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/86711-solved-inserting-string-to-db/#findComment-443125 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.