Matt Sanford Matt Sanford - 8 months ago 41
HTML Question

php only writing some values into database

I am having an issue with my php not writing the values it is getting from my ajax script into my MySQL database. I know that the php script is getting the values because they are being echoed in my browser. but when i check my database, only two out of the five values are being inputted. I am sure this isn't a nuance, but I can't seem to crack this.


The values that aren't being written are first name, last name, and job. ($fname, $lname, and $job respectively)




//db connecting variables
$hostname = "foobase";
$username = "foobase";
$dbname = "contactformbase";
$password = "password";
$con = new mysqli($hostname, $username, $password, $dbname);
$tbl_name = "client_base";

//Connecting to your database
if ($con->connect_error) {
die('Connect error (' . mysqli_connect_errno() . ')' . mysqli_connect_errno());
echo 'success!...' . $con->host_info . "\n";
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$address = $_POST['address'];
$job = $_POST['job'];
$message = $_POST['message'];

//adding values into the database.
$sql="INSERT INTO $tbl_name (First Name, Last Name, Email, Address, Job)VALUES('POST_['first_name']', '$lname', '$address', '$email')";
$result = mysqli_query($con, $sql);
echo "success";
else {
echo "error";


<script type="text/javascript">
$("#submit").click(function(e) {
var data_string = $("form#contact").serializeArray();
type: "POST",
url: "database.php",
data: data_string,
success: function(){
return false;


<form action="" method="POST" id="contact">
<td><h2>First Name: </h2></td>
<td><h2>Last Name: </td>
<td><h2>Email Address: </td>
<td><input type="text" name="first_name" placeholder="Johnny"></td>
<td><input type="text" name="last_name" placeholder="Appleseed"></td>
<td><input type="text" name="email" placeholder=""></td>
<td><h2>Street Address:</h2></td>
<td><h2>What's Dirty?</h2></td>
<td><input type="text" name="address" placeholder="123 Applegrove Rd. Appletown, VA 12345"></td>
<select name="job" form="contact">
<option value="house">House</option>
<option value="roof">Roof</option>
<option value="garage-shed">Garage/shed</option>
<option value="other">Other</option>
<td><h2>Message: </h2></td>
<textarea name="message">

Answer Source

You have the wrong field names in your PHP.


$fname = $_POST['fname'];
$lname = $_POST['lname'];


$fname = $_POST['first_name'];
$lname = $_POST['last_name'];

Also check the insert statement. It has wrong values against the fields - Address for Email, Eamil for Address.