huggies12345 Posted December 6, 2011 Share Posted December 6, 2011 Hi, I have two html made text boxes one that is called "name" and another that is called "regnum". I also have a submit button. They are both used to add data to a database. The name text box should add a name to the database under the "name" heading and the regnum should add a number under the "regnum" heading Here is the code for them: HTML Code: <form action="" method="post"> <p> Name: <input type="text" name="name"/> </p> <p> Regnum: <input type="text" name="regnum"/> </p> <p> <input type="submit" value="Add To Database" name = "submit2" /> </p> </form> Here is the PHP code that i am using for adding the user to the database: PHP Code: $host="localhost"; $username="root"; $password=""; $database="lab2"; mysql_connect("$host", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); $name = $_POST['name']; $regnum = $_POST['regnum']; if(!$_POST['submit2']){ echo "Enter A Vaue"; }else{ mysql_query("INSERT INTO lab2('name', 'regnum') VALUES(NULL, '$name', '$regnum')") or die(mysql_error()); echo "User Added To Database"; } The problem i get with this is "Undefined Index name and regnum". I watched a video on youtube and this is how the guy did it but it worked for him and for some reason it doesn't work for me. Can anyone help?? Thanks. Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted December 6, 2011 Share Posted December 6, 2011 Well, for starters: mysql_query("INSERT INTO lab2('name', 'regnum') VALUES(NULL, '$name', '$regnum')") or die(mysql_error()); echo "User Added To Database"; You're telling the insert that you're going to add values to 2 columns, 'name' and 'regnum'. You then insert 3 values: "...VALUES(NULL, '$name', '$regnum')..." If name is your primary key, or has an index, you're inserting a null value and that won't work. Regardless, you can't insert 3 values if you've only specified 2 columns to insert into. Quote Link to comment Share on other sites More sharing options...
huggies12345 Posted December 6, 2011 Author Share Posted December 6, 2011 Hi, I changed what you said and it's still doing the same thing. Quote Link to comment Share on other sites More sharing options...
SergeiSS Posted December 6, 2011 Share Posted December 6, 2011 You would better show a new code. It could happen that you change your code in a wrong way. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 6, 2011 Share Posted December 6, 2011 Your errors are due to these lines $name = $_POST['name']; $regnum = $_POST['regnum']; You are getting an error because you have a higher error reporting level than the guy did in the video - which is a good thing. If you copied the code from the tutorial, then that tutorial is full of bad practices. 1. Always prevent against referencing variables that might not be set. 2. Don't reference variables from POST/GET unless you need them. In the code you provided you tried to define $name and $regnum before you even knew whether you would use them. That's a waste. <?php if(!isset($_POST['submit2'])) { //Form was not submitted $message = "Enter A Value"; } else { //Form was submitted $name = (isset($_POST['name'])) ? trim($_POST['name']) : false; $regnum = (isset($_POST['regnum'])) ? trim($_POST['regnum']) : false; if(empty($name) || empty($regnum)) { //Required fields were not set or were empty $message = "<span style='color:#ff0000'>Name and registration number are required</span>"; } else { //Required fields were present. Connect to database $host="localhost"; $username="root"; $password=""; $database="lab2"; mysql_connect("$host", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); //Sanitize values and create/run query $nameSql = mysql_real_escape_string($name); $regnumSql = mysql_real_escape_string($regnum); $query = "INSERT INTO lab2('name', 'regnum') VALUES('$nameSql', '$regnumSql')"; $result = mysql_query($query); if(!$result) { $message = "<span style='color:#ff0000'>There was a problem adding the result</span>"; //Next line for debugging purposes only. $message .= "<br>Query: {$query}<br>Error: " . mysql_error(); } else { $message = "User Added To Database"; } } } ?> <html> <body> <?php echo $message; ?> <br> <form action="" method="post"> <p> Name: <input type="text" name="name"/> </p> <p> Regnum: <input type="text" name="regnum"/> </p> <p> <input type="submit" value="Add To Database" name = "submit2" /> </p> </form> </body> </html> 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.