1

Hi I do not understand why this code is not inserting the data from the html text feilds into my actually database. I am trying to sample it using just First_Name to start with.

Anyways HTML code is as followed :

<form action="Proform.php" name="Myform" method="post">

        <input type ="hidden" value="1" name="check_submit" />
        Please Enter First Name: <input type ="text" name="First_Name" /> <br />
        Please Enter Second Name: <input type ="text" name="Second_Name" /><br />
        Please Enter Email Address: <input type ="text" name="Email_Address" /><br />
        Please Enter A Password: <input type="password" name="Password" /><br />
        <input type ="submit" name"Submit" /><br />

    </form>

And php and MYSQL is as followed :

 <?php

    $dbname='ecig';
    $dbhost='localhost';
    $dbpass='password';
    $dbuser='eciguser';



    $dbhandle = mysql_connect($dbhost, $dbuser, $dbpass)
      or die("Unable to connect to MySQL");
    echo "Connected to MySQL<br>";


    $selected = mysql_select_db("ecig",$dbhandle)
      or die("Could not select examples");



    $res=mysql_query("INSERT INTO Persons (First_Name, Second_Name) VALUES ('$_POST[First_Name]')");

    if (array_key_exists ('check_submit', $_POST )) 

    echo "Your Name is : {$_POST['First_Name']}<br />";
    echo "Your Second Name is : {$_POST['Second_Name']}<br />";
    echo "Your Email Address is : {$_POST['Email_Address']}<br />";
    echo "Your Password Is : {$_POST['Password']}<br />";

    ?>

It must have something to do with this line of code but I cannot spot it.. Can any of you spot what is going wrong?

$res=mysql_query("INSERT INTO Persons (First_Name, Second_Name) VALUES ('$_POST[First_Name]')");

Any help would be much appreciated. Thanks .

1

6 Answers 6

1
$res=mysql_query("INSERT INTO Persons (First_Name, Second_Name) VALUES ('$_POST[First_Name]')");

I can see a couple things wrong with this query. First you say you only want to start with the first name, so you need only insert into the first name column only:

Persons (First_Name  ) VALUES
                    ^

Then, looking, you want to make sure you are getting your variable to fill in the string. You want to use:

('{$_POST['First_Name']}')
  ^       ^          ^ ^

Also consider error testing since this is more than likely a MySQL Syntax error. You can add

if (!$res) {
    die('Invalid query: ' . mysql_error());
}

NOTICE: Do not use MySQL_* as it has been deprecated as of PHP 5.5. Use MySQLi_* or PDO. Also, you should use prepared statements and SQL escapes. You are open for injection.

Sign up to request clarification or add additional context in comments.

1 Comment

Helped allot got the database collecting data now. Thanks !
1

You're telling the query you are going to supply first_name and last_name but you're only providing the first_name:

$res = mysql_query("INSERT INTO Persons (First_Name, Second_Name) VALUES ('$_POST[First_Name]')");

Try:

$res = mysql_query("INSERT INTO Persons (First_Name, Second_Name) VALUES ('$_POST[First_Name]', '$_POST[Lirst_Name]')");

Comments

1

Your Insert Query is incorrect. You must provide the same number of values as columns.

$res=mysql_query("INSERT INTO Persons (First_Name, Second_Name) VALUES ('$_POST[First_Name]')");

Should be:

$res=mysql_query("INSERT INTO Persons (First_Name, Second_Name) VALUES ('$_POST[First_Name]', '$_POST[Last_Name]')");

Comments

1

You are only passing one value in

$res=mysql_query("INSERT INTO Persons (First_Name, Second_Name) VALUES ('$_POST[First_Name]')");

, and in your insert statement you are inserting 2 values, First Name and second name.

Try this:

$res=mysql_query("INSERT INTO Persons (First_Name, Second_Name) VALUES ('$_POST[First_Name]', '$_POST[Second_Name]')");

Comments

1

Try this:

add this to the php code:

$firstname             =    $_POST['First_Name'];
$secondname        =    $_POST['Second_Name'];

and then change the query to:

    $res=mysql_query("INSERT INTO Persons (First_Name, Second_Name) VALUES ('".$firstname."', '".$lastname."')");

Comments

0
$res=mysql_query("INSERT INTO Persons (First_Name, Second_Name) VALUES ('$_POST[First_Name]', '$_POST[Second_Name]')");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.