Reputation: 13
I'm having trouble inserting form data into my database. It connects successfully to the database, but the data just doesn't get inserted into my MySQL database. Hope you can help! Thank you in advance!
This is the form code:
<form id="score" method="post" action="process.php">
<fieldset>
<div class="box1">
<legend>Your details</legend>
<ol>
<li>
<label for=name>Name</label>
<input id=name name=name type=text placeholder="First and last name" required autofocus>
</li>
<li>
<label for=email>Email</label>
<input id=email name=email type=email placeholder="[email protected]" required>
</li>
<li>
<label for=phone>Phone (country code if overseas)</label>
<input id=phone name=phone type=tel placeholder="Eg. +447500000000" required>
</li>
</ol>
</div>
</fieldset>
<fieldset style="border: none;">
<button type=submit>Continue</button>
</fieldset>
</form>
This is the php code (filename process.php):
<?php
if( $_POST )
{
$con = mysql_connect("localhost","database_user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_name", $con);
$insert_query = "insert into feedback(
name,
phone,
email
) values (
".$_POST['name'].",
".$_POST['phone'].",
".$_POST['email'].")";
($insert_query);
echo "<h2>Thank you for your feedback!</h2>";
mysql_close($con);
}
?>
Upvotes: 1
Views: 3856
Reputation: 635
mysql_query()
is missing. Replace
($insert_query);
with
mysql_query($insert_query);
Upvotes: 0
Reputation: 1788
You have missed mysql_query
function :
Use this way:
$retval = mysql_query( $insert_query, $con );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
Reference link
Upvotes: 2
Reputation: 192
Try this way:
<?php
if( $_POST )
{
$con = mysql_connect("localhost","database_user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_name", $con);
$insert_query = "insert into feedback(
name,
phone,
email
) values (
'".$_POST['name']."',
'".$_POST['phone']."',
'".$_POST['email']."')";
mysql_query($insert_query);
echo "<h2>Thank you for your feedback!</h2>";
mysql_close($con);
}
?>
mysql_query() Is missing.
Also close the query variables with ''. Since they're strings you need to have them with that or else will fail.
Cheers
Upvotes: 1