ddanielsmith07 Posted November 10, 2012 Share Posted November 10, 2012 Hi everyone. I'm having a bit of trouble with my registration script. When I go to my action.php file, I get these error: Notice: Undefined index: uname1 in C:\xampp\htdocs\series\action.php on line 2 Notice: Undefined index: pword1 in C:\xampp\htdocs\series\action.php on line 3 But, When I test my script, Everything works fine. I don't understand what's going on. These are my two files: index.php: <html> <body> <form action="action.php" method="post"> Username: <input type="text" name="uname1" /> Password: <input type="password" name="pword1" /> <input type="submit" value="Login" /> </form> </body> </html> action.php: <?php $username_1 = $_POST['uname1']; $password_1 = $_POST['pword1']; $con = mysql_connect("localhost", "root", ""); if (!$con) { die('Could not connect: '. mysql_error()); } mysql_select_db("user1", $con); mysql_query("INSERT INTO userlogin (username, password) VALUES ('$username_1', '$password_1')"); mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/270524-is-there-anything-wrong-with-this-script/ Share on other sites More sharing options...
MDCode Posted November 10, 2012 Share Posted November 10, 2012 If you go to it directly, $username_1 and $password_1 will give that error because you are not submitting the form first and the $_POST variables are undefined. To get rid of the error, check that the form has been submitted first: if(isset($_POST['submit'])) { $username_1 = $_POST['uname1']; $password_1 = $_POST['pword1']; // add the rest of your code here } Also for this to work you will need to add name="submit" to your submit button Link to comment https://forums.phpfreaks.com/topic/270524-is-there-anything-wrong-with-this-script/#findComment-1391412 Share on other sites More sharing options...
Pikachu2000 Posted November 10, 2012 Share Posted November 10, 2012 Or better yet, since some browsers mangle submit buttons: if( strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) { Link to comment https://forums.phpfreaks.com/topic/270524-is-there-anything-wrong-with-this-script/#findComment-1391416 Share on other sites More sharing options...
AyKay47 Posted November 10, 2012 Share Posted November 10, 2012 Or use a hidden checksum.. Link to comment https://forums.phpfreaks.com/topic/270524-is-there-anything-wrong-with-this-script/#findComment-1391437 Share on other sites More sharing options...
Pikachu2000 Posted November 10, 2012 Share Posted November 10, 2012 You really don't need to go to the bother of using a hidden checksum simply to prevent undefined index notices. Link to comment https://forums.phpfreaks.com/topic/270524-is-there-anything-wrong-with-this-script/#findComment-1391438 Share on other sites More sharing options...
AyKay47 Posted November 10, 2012 Share Posted November 10, 2012 You really don't need to go to the bother of using a hidden checksum simply to prevent undefined index notices. No, you don't. However either method will work, bit of a habit for myself to use checksums since they have other advantages as well. What we can both agree on here is that relying on a submit button value may cause problems in some browsers. Link to comment https://forums.phpfreaks.com/topic/270524-is-there-anything-wrong-with-this-script/#findComment-1391491 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.