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,
$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() )";
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);
?>
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.