Jump to content

PHP MYSQL Putting users into database using HTML form??


huggies12345

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.