I have a simple form that’s supposed to enter submissions in a MySQL database but whenever the form is submitted I get a server 500 error. I’m new to the PHP/MySQL so i’m not seeing the problem. Any ideas?
HTML Form Excerpt:
<form method="post" action="submit.php">
<label for="fname">First Name *:</label>
<input type="text" id="fname" name="fname" /><br />
<label for="lname">Last Name *:</label>
<input type="text" id="lname" name="lname" /><br />
<label for="email">Email *:</label>
<input type="email" id="email" name="email" /><br />
<label for="phone">Phone Number:</label>
<input type="tel" id="phone" name="phone" /><br />
<input type="checkbox" id="ageverify" />
<label for="ageverify">I am at least 18 years of age *</label><br />
<input type="checkbox" id="terms" />
<label for="terms">I agree to the Terms & Conditions *</label><br />
<input type="submit" value="Submit" />
</form>
Entirety of submit.php
<?
mysql_connect("localhost","USERNAME","PASSWORD");
mysql_select_db("DB_NAME");
$sql = "INSERT into entries (fname,lname,email,phone,ageverify,terms) ";
$sql .= "VALUES (";
$sql .= $_POST['fname'] . ',' . $_POST['lname'] . ',' . $_POST['email'] . ',';
$sql .= $_POST['phone'];
if (isset($_POST['ageverify']) // if checked will exist, otherwise it won't
$sql .= 'yes' . ',';
else
$sql .= 'no' . ',';
if (isset($_POST['terms'])
$sql .= 'yes' . ',';
else
$sql .= 'no' . ',';
$sql .= ')';
$result = mysql_query($sql);
if($result){
echo("Thanks");
} else{
echo("Something went wrong");
}
?>
UPDATE: WORKING CODE (As rudimentary as it may be)
<?php
mysql_connect("localhost","USERNAME","PASSWORD");
mysql_select_db("DB_NAME");
$sql = "INSERT into entries (fname,lname,email,phone,ageverify,terms) ";
$sql .= "VALUES (";
$sql .= '"' . $_POST['fname'] . '"' . ',' . '"' . $_POST['lname'] . '"' . ',' . '"' . $_POST['email'] . '"' . ',';
$sql .= '"' . $_POST['phone'] . '"';
if (isset($_POST['ageverify'])) // if checked will exist, otherwise it won't
$sql .= ',' . '"yes"' ;
else
$sql .= ',' . '"no"';
if (isset($_POST['terms']))
$sql .= ',' . '"yes"';
else
$sql .= ',' . '"no"';
$sql .= ')';
echo $sql;
$result = mysql_query($sql);
if($result){
echo("Thanks");
} else{
echo("Something went wrong");
}
?>
First of all: Read up on SQL injection! You are definitely vulnerable!
Second, you should enable error reporting while developing – this would have told what was wrong.
The status 500 appears because something serverside went wrong. You can check your error logs, when this occurs.
Though, if you enable error reporting, you get the errors on screen. This would have told you that you have syntax errors.
You are missing ending parentheses on line 11 and 16:
should be
And the same goes for line 16.
Thirdly, always start PHP tags with
<?phpand NOT just<?. Try avoiding short tags.