Question:
PHP MYSQL Why isn't this working?
Huggies12345
2011-12-04 17:47:59 UTC
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:


Name:



Regnum:







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.
Three answers:
riple
2011-12-04 17:57:31 UTC
make

$password="example";

you can change example with something you want

change

mysql_connect("$host", "$username", "$password") or die(mysql_error());

to

mysql_connect("localhost", "root", "example") or die(mysql_error());

and

mysql_select_db("$database") or die(mysql_error()); to

mysql_select_db("lab2") or die(mysql_error());
Gabs777
2011-12-05 05:27:03 UTC
Why do you use : VALUES(NULL, '$name', '$regnum')") or die(mysql_error()); ?

Describe the table structure will be very helpful.





If the table structure is only name and regnum, insert as : VALUES('$name', '$regnum')") or die(mysql_error());



But if the table structure is ID, Name, Regnum, you have to make the ID as Unique or Primary and it should be auto increment field. then you have to insert like this : VALUES('', '$name', '$regnum')") or die(mysql_error());
cali_guy_0024
2011-12-05 01:54:51 UTC
Not exactly sure, but at first glance, there's nothing in the html that's actually connecting it to php. (ie. the submit button). You need the submit button to activate a php function.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...