I can't figure this annoying thing out I've tried it over and over but still getting this error: Notice: Undefined index: example1 in G:\xampp\htdocs\testing\whatever.php on line 15 Notice: Undefined index: example2 in G:\xampp\htdocs\testing\whatever.php on line 16 Please help :s The code: <?php $sql = mysql_connect("localhost","root","") or die (mysql_error()); if (!isset($sql)) {echo "Could not connect to DB";} $select_db = mysql_select_db("test", $sql); if (!isset($select_db)) { echo "Could not connect to DB";} $example1=($_POST['example1']); $example2= ($_POST['example2']); mysql_query ("INSERT INTO test (test1, test2) VALUE ('$example1', '$example2')"); ?> <html> <body> <form action="whatever.php" method="post"> Username: <input type="text" name="example1" /><br> Password: <input type="password" name="example2" /><br> <input type="submit" value ="register" /> </form> </body> </html>
Instead of this, use: $example1 = $_POST['example1']; $example2 = $_POST['example2']; mysql_query("INSERT INTO test (test1, test2) VALUES ('$example1', '$example2')"); PHP:
You need to remember, your using php with error reports set to all, which includes notices, which aren't that important and most likely wont appear on a hosted website. Your concerns should be whether the script works or not. For a quick fix just write the following before: $example1 = ""; $example2 = ""; $example1 = $_POST['example1']; $example2 = $_POST['example2']; PHP: Or use: if isset() or if empty() PHP: Not Tested try this: $example1 = isset($_POST['example1']) && !empty($_POST['example1']); $example2 = isset($_POST['example2']) && !empty($_POST['example2']); PHP:
mm actually now there's another problem. I tried your way and the errors no longer appeared however no data was being sent to the database. I also tried doing it this way: if (isset($_POST['example1']) && isset($_POST['example2'])){ $example1 = $_POST['example1']; $example2 = $_POST['example2']; mysql_query ("INSERT INTO test (example1, example2) VALUE ('$example1'.'$example2')"); }
mysql_query ("INSERT INTO test (example1, example2) VALUE ('$example1','$example2')"); // you had a dot. here should have been a comma, PHP: You could always just do this: mysql_query("INSERT INTO test (example1, example2) VALUES ('$_POST['example1']','$_POST['example2']')"); PHP:
Edit... It's working now thanks to the comma fix. Can't believe i wasted your time with that... Really appreciate the help though it's working now.
No problem: just a short note to tidy things up, you could use this if its any use. <?php if(isset($_POST['example1']) && $_POST['example1']!='') { mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); mysql_query("INSERT INTO test (example1, example2) VALUES ('$_POST['example1']','$_POST['example2']')"); echo "All done"; } ?> <html> <body> <form action="#" method="post"> Username: <input type="text" name="example1" /><br> Password: <input type="password" name="example2" /><br> <input type="submit" value ="register" /> </form> </body> Code (markup):