SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Dec 2010
    Location
    UK
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool PHP MySQL - Some Form Data Not Added to Database

    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
    Code 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
    Code 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>
    Last edited by Mittineague; Dec 24, 2013 at 15:19. Reason: reformatting bbcode tags

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2013
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure that the variable has a value?

    If your really stuck you could try executing the query directly in phpmyadmin or something like that, i find it a useful troubleshooting tool.

  3. #3
    SitePoint Member
    Join Date
    Dec 2010
    Location
    UK
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply. I am sure the variable have a value. I have tried running the insert query directly in phpmyadmin as you suggested and that worked. I have revisited the field names on the database and made some changes, but yet all other fields are inserted ok except the the 1st field 'cusid'. Here is the insert query executed directly in phpmyadmin
    INSERT INTO customer
    (cusid, name, add1, add2, city, state, postcode, country, startdate, enddate)
    VALUES
    ('COO3', 'Test', '36', 'Broad St', 'NY', 'NYC',
    'TF9 9LU', 'US', '27 Dec 2013', '31 Dec 2013'
    );
    The above ran correctly and inserted all fields into the database. However the below code missed out the 1st value but inserted all the other fields correctly. See code below:-
    $sql = "INSERT INTO customer
    (cusid, name, add1, add2, city, state, postcode, country, startdate, enddate)
    VALUES
    ('$_POST[cusid]', '$_POST[name]', '$_POST[add1]', '$_POST[add2]', '$_POST[city]', '$_POST[state]',
    '$_POST[postcode]', '$_POST[country]', '$_POST[startdate]', '$_POST[enddate]'
    )";

    Any help will be very much appreciated.

  4. #4
    SitePoint Member
    Join Date
    Dec 2010
    Location
    UK
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there please ignore my previous message. I have figured out the problem. Thanks for all your help. The issue was with my form. I made a spelling mistake on that particular field name on my form. Regards,

  5. #5
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there,
    I think it because the variable's name in the FORM.HTML.PHP is 'customerid'.
    Code:
    <tr  align="left">
    					<th><label for="custid">Customer ID:</th></label>
    					<td><input type="text" id="custid" name="customerid"></d>
    				</tr>
    But when you use it in the INDEX.PHP,it was 'cusid',
    Code:
    $sql = "INSERT INTO customer
    (cusid, name, add1, add2, city, state, postcode, country, startdate, enddate)
    VALUES
    ('$_POST[cusid]', '$_POST[name]', '$_POST[add1]', '$_POST[add2]', '$_POST[city]', '$_POST[state]',
    '$_POST[postcode]', '$_POST[country]', '$_POST[startdate]', '$_POST[enddate]'
    )";
    Hope that helps.

  6. #6
    SitePoint Member
    Join Date
    Dec 2010
    Location
    UK
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply, i have resolved the issue as in my previous message. However, now i have a new problem. I can not login as i keep getting the error message:- The specified email address or password was incorrect. Any ideas guys!!

  7. #7


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •