I'm trying to set up a very simple form to submit data to a MySQL database (I've never actually set up a MySQL database or the connections required to interact with it via a web form myself before).
I have a simple HTML form in my web page, to allow the user to enter a name, email address, and a question that they want to ask:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
Page title
</head>
<body>
<h1>Customer Information</h1>
<form id="customerInfoForm" accept-charset = "utf-8" action = "insert.php" method = "post" >
<table id = "customerInfoTable">
<tr><td>Name: </td><td><input type = "text" name = "customerName" ></input></td></tr>
<tr><td>Email: </td><td><input type = "text" name = "customerEmail"></input></td></tr>
<tr><td>Enquiry: </td><td><input type = "text" name = "enquiryText"></input></td></tr>
</table>
<br>
<input type = "submit" value = "Sumit"></input>
</form>
</body>
</html>
<?php
$con = mysql_connect('localhost','[retracted]','[retracted]');
if(!$con) {
echo 'Not connected to server!';
}
$Name = $_POST['customerName'];
$Email = $_POST['customerEmail'];
$Query = $_POST['enquiryText'];
$sql = "INSERT INTO Enquiries(Name, Email, Query) VALUES ('$Name', '$Email', '$Query')";
if(!mysql_query($con, $sql)) {
echo 'Data not inserted';
} else {
echo 'Data inserted';
}
?>
In the insert.php
file that's called when clicking the 'Submit' button on the form, I have the following:
<?php
//Check if data has been entered
if( isset( $_POST['data'] ) && !empty( $_POST['data'] ) )
{
$data = $_POST['data'];
} else {
header( 'location: form.html' );
exit();
}
//set up mysql
$sql_server = 'localhost';
$sql_user = 'user';
$sql_pwd = 'password';
$sql_db = 'database';
//Connect to sql database
$mysqli = new mysqli( $sql_server, $sql_user, $sql_pwd, $sql_db) or die( $mysqli->error );
//Insert details into table
$insert = $mysqli->query( "INSERT INTO Enquiries ('data') VALUE ( '$data' )" );
//Close mysqli connection
$mysqli->close;
?>
However, currently, when I click the 'Submit' button on my form, I get an error displayed in my browser:
error ); //Insert details into table $insert = $mysqli->query( "INSERT INTO Enquiries ('data') VALUE ( '$data' )" ); //Close mysqli connection $mysqli->close; ?>
i.e. the above error is displayed as the content of the webpage, so it seems I'm doing something wrong with the database connection, but I'm not sure what... Can anyone point me in the right direction?