Hello Everyone,
I am trying to build a small application but i have encountered some issues adding my form data to my database. I have created the form and written the insert query to insert the data to my database. All fields are inserted correctly to the database except for one field. Thats is the first field in the INSERT query. The field name in the database matches the the names used in the INSERT query. Really not sure where i am going wrong. I will appreciate some help. There is no error messages presented when i submit the form. Here is my code below.
INDEX.PHP
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php';
if (isset($_GET['addcustomer']))
{
include 'form.html.php';
exit();
}
include_once $_SERVER['DOCUMENT_ROOT'] . '/sps/includes/style.css';
include_once $_SERVER['DOCUMENT_ROOT'] . '/test/includes/db.inc.php';
if (isset($_POST['submit']))
{
// Find the author?
$authorname = mysqli_real_escape_string($link, $_POST['authorname']);
$authoremail = mysqli_real_escape_string($link, $_POST['authoremail']);
$sql = "SELECT id FROM author WHERE authorname = '$authorname' AND authoremail = '$authoremail'";
if (!$result = mysqli_query($link, $sql))
{
$error = 'Error selecting existing author: ' . mysqli_error($link);
include 'error.html.php';
exit();
}
if (mysqli_num_rows($result) > 0)
{
// Existing author
$row = mysqli_fetch_array($result);
$authorid = $row['id'];
}
else
{
//Create a new author
$sql = "INSERT INTO author (authorname, authoremail) VALUES ('$authorname', '$authoremail')";
if (!mysqli_query($link, $sql))
{
$error = 'Error adding new author: ' . mysqli_error($link);
include 'error.html.php';
exit();
}
$authorid = mysqli_insert_id($link);
}
$sql = "INSERT INTO customer
(customerID, name, add1, add2, city, state, postcode, country, startdate, enddate)
VALUES
('$_POST[customerID]', '$_POST[name]', '$_POST[add1]', '$_POST[add2]', '$_POST[city]', '$_POST[state]',
'$_POST[postcode]', '$_POST[country]', '$_POST[startdate]', '$_POST[enddate]'
)";
if (!mysqli_query($link, $sql))
{
$error = 'Error adding submitted customer: ' . mysqli_error($link);
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
// Selecting the table(s) to query.
$result = mysqli_query($link, 'SELECT customer.id, customerID, name, startdate, enddate, postcode
FROM customer INNER JOIN author ON authorid = author.id WHERE visible = \\'YES\\'' );
if (!$result)
{
$error = 'Error selecting customer: ' . mysqli_error($link);
include 'error.html.php';
exit();
}
// Table design.
echo "<table class='imagetable'>";
echo "<tr>
<th>ID</th> <th>Customer ID</th> <th>Name</th> <th>Start Date</th> <th>End Date</th> <th>Post Code</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
// Variables defined so they are easy to remember!
$id = $row['id']; $customerID = $row['customerID']; $name = $row['name']; $startdate = $row['startdate']; $enddate = $row['enddate'];
$postcode = $row['postcode'];
// Now for each looped row
echo "<tr>
<td>".$id."</td> <td>".$customerID."</td> <td>".$name."</td> <td>".$startdate."</td> <td>".$enddate."</td>
<td>".$postcode."</td>
</tr>";
}// End our while loop
$customers = array();
while ($row = mysqli_fetch_array($result))
{
$customers[] = array
(
'id' => $row['id'],
'customerID' => $row['customerID'],
'name' => $row['name'],
'startdate' => $row['startdate'],
'enddate' => $row['enddate'],
'contact1' => $row['contact1'],
'tel1' => $row['tel1'],
'email1' => $row['email1'],
'salesperson' => $row['salesperson']
);
}
include 'customer.html.php';
echo "</table>"
?>
FORM.HTML.PHP
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Add Customer</title>
</head>
<body>
<form action="?" method="post">
<div>
<h2><b><p>Add New Customer</h2></b></p>
<table>
<tr align="left">
<th><label for="authorname">Author Name:</th></label>
<td><input type="text" id="authorname" name="authorname"></d>
</tr>
<tr align="left">
<th><label for="authoremail">Author Email:</th></label>
<td><input type="text" id="authoremail" name="authoremail"></d>
</tr>
<tr align="left">
<th><label for="custid">Customer ID:</th></label>
<td><input type="text" id="custid" name="customerid"></d>
</tr>
<tr align="left">
<th><label for="name">Customer Name:</th></label>
<td><input type="text" id="name" name="name"></d>
</tr>
<tr align="left">
<th><label for="add1">Address 1:</th></label>
<td><input type="text" id="add1" name="add1"></d>
</tr>
<tr align="left">
<th><label for="add2">Address 2:</th></label>
<td><input type="text" id="add2" name="add2"></d>
</tr>
<tr align="left">
<th><label for="city">City:</th></label>
<td><input type="text" id="city" name="city"></d>
</tr>
<tr align="left">
<th><label for="tel1">State:</th></label>
<td><input type="text" id="state" name="state"></d>
</tr>
<tr align="left">
<th><label for="postcode">Post Code:</th></label>
<td><input type="text" id="postcode" name="postcode"></d>
</tr>
<tr align="left">
<th><label for="country">Country:</th></label>
<td><input type="text" id="country" name="country"></d>
</tr>
<tr align="left">
<th><label for="startdate">Start Date:</th></label>
<td><input type="text" id="startdate" name="startdate"></d>
</tr>
<tr align="left">
<th><label for="enddate">End Date:</th></label>
<td><input type="text" id="enddate" name="enddate"></d>
</tr>
</table>
</div>
<div><input type="submit" name="submit" value="submit"></div>
</form>
</body>
</html>