0

This is not going online anytime soon. I am just trying to learn PHP/MySQL. This is my first attempt with inserting to a database. I cannot figure out why the data won't insert. This is the HTML form:

    <form action="testdbserverside.php" method="post">
    Name:<br />
    <input type="text" id="ContactName" name="Name" /><br />
    E-mail:<br />
    <input type="text" id="ContactEmail" name="Email" /><br />
    Comment:<br />
    <input type="text" id="ContactComment" name="Comment" size="50" />
    <br /><br />
    <input type="submit" id="submit" name="submit" value="Send">
    <input type="reset" id ="reset" value="Reset">
    </form>

This is the PHP:

    <?php
     $ContactName = $_POST["ContactName"];
     $ContactEmail = $_POST["ContactEmail"];
     $ContactComment = $_POST["ContactComment"];

     $sql_connection = mysqli_connect("localhost:8889","root","root","derek_website_tmp");

     if (mysqli_connect_errno())
        {
         echo "failed to connect" . mysqli_connect_error();
        }

     $sql = "INSERT INTO MyContacts (

                ContactName,
                ContactEmail,
                ContactComment,
                ContactDateCreated
            )
            VALUES (
              '$ContactName',
              '$ContactEmail',
              '$ContactComment',
              NOW()
        )";

     if (!mysqli_query($sql_connection,$sql))
        {
        die('Error : ' . mysqli_error($sql_connection));
        }

     mysql_close ($sql_connection);

    ?>

And this is the database:

    mysql> USE derek_website_tmp;
    Database changed
    mysql> CREATE TABLE MyContacts  (
        -> ContactID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        -> ContactName VARCHAR(100),
        -> ContactEmail VARCHAR(100),
        -> ContactComment VARCHAR(200),
        -> ContactDateCreated DATETIME
        -> );
    Query OK, 0 rows affected (0.19 sec)

Any help would be greatly appreciated. I realize this is not sanitizing anything and is not worthy to go live. I'm just working on getting it to insert for now.

Thank you,

5
  • 2
    What error are you getting? Commented Sep 17, 2013 at 2:29
  • 1
    You need to close your form tag.. also Commented Sep 17, 2013 at 2:30
  • 1
    (1) What isn't working? (2) Please don't use those insertions like "$variable" for queries they are very fragile to SQL injection attacks... Commented Sep 17, 2013 at 2:31
  • I am not getting any errors. The only thing being inserted is ContactID and the DateCreated. Commented Sep 17, 2013 at 2:33
  • 1
    In future cases please add error_reporting(-1); ini_set('display_errors', 'On'); during development work. Commented Sep 17, 2013 at 2:42

3 Answers 3

1
$ContactName = $_POST["ContactName"]; 
$ContactEmail = $_POST["ContactEmail"]; 
$ContactComment = $_POST["ContactComment"];

Should be:

$ContactName = $_POST["Name"]; 
$ContactEmail = $_POST["Email"]; 
$ContactComment = $_POST["Comment"];

The name is your parameter not the id

And your query should be:

$sql = "INSERT INTO MyContacts (ContactName, ContactEmail, ContactComment, ContactDateCreated ) VALUES ( "'.$ContactName.'", "'.$ContactEmail.'", "'.$ContactComment.'", NOW() )";
Sign up to request clarification or add additional context in comments.

Comments

0

You tried to read values from "ID" instead of "NAME" attribute.

The form can be modified like this (ID part is optional in terms of form submission):

Name:<br />
<input type="text" id="ContactName" name="ContactName" /><br />
E-mail:<br />
<input type="text" id="ContactEmail" name="ContactEmail" /><br />
Comment:<br />
<input type="text" id="ContactComment" name="ContactComment" size="50" />

All the rest of your code should work fine.

FYI, You can use print_r() or var_dump() to print the contents of given variable.

<?
// this will show what really is coming in through the $_POST
print_r($_POST);
?>

Comments

0

In your PHP page, you are gathering the value by the id, it won't work like that you have to gather the value by text field name like

$ContactName = $_POST["Name"];
$ContactEmail = $_POST["Email"];
$ContactComment = $_POST["Comment"];  

I think this will work fine.

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.