Coding error - can't INSERT into table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Coding error - can't INSERT into table

Post by cdickson »

I had this working, and then I changed something but I don't know what. Can anyone tell me what I need to fix so that this form will insert into the "orders" table? :oops:

Code: Select all

<?php
header("Cache-control: private"); // IE 6 Fix
ini_set('display_errors', 1);		//Handle errors
error_reporting (E_ALL & ~E_NOTICE);	  
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
	  <?php 
	  if (isset($_POST['submit'])) {			//Handle form
	  
	  session_start(); 									//Start session
	  $_SESSION['order_no'] = $orderno;
	  $_SESSION['order_date'] = $orderdate;
		$_SESSION['collateral_owner'] = $cowner;
		$_SESSION['collateral_address'] = $caddress;
		$_SESSION['collateral_city'] = $ccity;
		$_SESSION['collateral_state'] = $cstate;
		$_SESSION['collateral_zip'] = $czip;
		$_SESSION['collateral_county'] = $ccounty;
		$_SESSION['collateral_twp'] = $ctwp;
		$_SESSION['legal_descr'] = $clegal;
		$_SESSION['loan_no'] = $loanno;
		$_SESSION['lender_name'] = $lname;
		$_SESSION['lender_address'] = $laddress;
		$_SESSION['lender_city'] = $lcity;
		$_SESSION['lender_state'] = $lstate;
		$_SESSION['lender_zip'] = $lzip;
		$_SESSION['lender_phone'] = $lphone;
		$_SESSION['lender_fax'] = $lfax;
		$_SESSION['lender_id'] = $lenderid;

	  
	  //Connect and select
	  if ($dbc = @mysql_connect ('localhost', 'michigan', 'mapflood')) 
	  {
	  
	  		if (!@mysql_select_db ('mfmdb')) {
			die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>');
		}
	
	} else {
		die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>');
	}
	  
	  //Define query
	  $query1 = "INSERT INTO orders (order_no, order_date, collateral_owner, collateral_address,
	  collateral_city, collateral_state, collateral_zip, collateral_county, collateral_twp,
	  legal_descr, loan_no, lender_name, lender_address, lender_city, lender_state, lender_zip,
	  lender_phone, lender_fax, lender_id) VALUES (0, NOW(), '{$_POST['collateral_owner']}', 
	  '{$_POST['collateral_address']}', '{$_POST['collateral_city']}', 
	  '{$_POST['collateral_state']}', '{$_POST['collateral_zip']}', '{$_POST['collateral_county']}', 
	  '{$_POST['collateral_twp']}', '{$_POST['legal_descr']}', '{$_POST['loan_no']}',
	  '{$_POST['lender_name']}', '{$_POST['lender_address']}', '{$_POST['lender_city']}',
	  '{$_POST['lender_state']}', '{$_POST['lender_zip']}', '{$_POST['lender_phone']}',
	  '{$_POST['lender_fax']}', '{$_POST['lender_id']}')";
	  
	  //Execute query
	  if (@mysql_query ($query1)) {
	  	echo '<p>Thank you for your order.  You will receive a confirmation.</p>';
	    //Send email notice to MFM
		$body = "xxxxxxxxx just received an online order from {$_POST['lender_name']}.";
	  	mail ('xxxx@xxxxxxxxxxxxx.com', 'Order Received', $body, 'From: Web Site');
	  	
		} else {
		
		echo "<p>We're sorry but your order could not be executed because: 
		<b>" . mysql_error() . "</b>. 
		Please report this error to xxxxxxxxxxxxxx.</p>";
		}
		mysql_close();
		}

		//Display the form
	  ?>
      <form action="confirmorder.php" method="post" name="orderform">
	  Today's Date <input name="order_date" type="text" class="navtext" id="order_date"><br />
	  <b>Lender Information</b><br />
	  Lender ID #<input name="lender_id" type="text" class="navtext" id="lender_id"><br />
	  Lender Name<input name="lender_name" type="text" class="navtext" id="lender_name"><br />
	  Lender Address <input name="lender_address" type="text" class="navtext" id="lender_address"><br />
	  City<input name="lender_city" type="text" class="navtext" id="lender_city"><br />
	  State<input name="lender_state" type="text" class="navtext" id="lender_state" value="MI" size="10" maxlength="10"><br />
	  Zip<input name="lender_zip" type="text" class="navtext" id="lender_zip" size="10" maxlength="10"><br />
	  Phone<input name="lender_phone" type="text" class="navtext" id="lender_phone"><br />
	  Fax<input name="lender_fax" type="text" class="navtext" id="lender_fax"><br /><br />
	  
	  Property Information</b><br />
	  Loan ID #<input name="loan_no" type="text" class="navtext" id="loan_no"><br />
	  Owner Name<input name="collateral_owner" type="text" class="navtext" id="collateral_owner"><br />
	  Property Address<input name="collateral_address" type="text" class="navtext" id="collateral_address"><br />
	  City<input name="collateral_city" type="text" class="navtext" id="collateral_city2"><br />
	  State<input name="collateral_state" type="text" class="navtext" id="collateral_state" value="MI" size="10" maxlength="10"><br />
	  Zip<input name="collateral_zip" type="text" class="navtext" id="collateral_zip" size="10" maxlength="10"><br />
	  County<input name="collateral_county" type="text" class="navtext" id="collateral_county"><br />
	  Township<input name="collateral_twp" type="text" class="navtext" id="collateral_twp"><br />
	  Legal Description<textarea name="legal_descr" cols="50" id="legal_descr"></textarea><br />
                <input type="submit" name="submit" value="Place Order" class="smallbodytext">
      </form>

</body>
</html>
?>
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

If order_no is auto_increment (and the primary key) then try drop it from the INSERT.
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Post by cdickson »

Thanks for the suggestion, Buddha, but it didn't work.

The thing I don't get is that I had this blasted thing working, even with the order_no as the auto-incremented key field, and I have no clue what I did to change it. :?
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Any other unique fields? May also want to check the database - empty and repair it if you can? I had a heck of a time getting rid of zero entry once.
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Post by cdickson »

Since we are still in the development stages, we only have 8 dummy records in the database, so it doesn't matter what I lose.

I analyzed the table in phpMyAdmin and also ran the repair, and everything checked out OK. I'm still trying to troubleshoot... but appreciate your suggestions!
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

Buddha443556 wrote:If order_no is auto_increment (and the primary key) then try drop it from the INSERT.
meaing in the VALUE part you just put empty single quote
like this :

Code: Select all

INSERT INTO table_name (order_no, column2,...)
VALUES ('', value2,....)
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Post by cdickson »

There is something in the coding of my confirmorder.php page that is keeping the data from inserting... still testing...
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

shot in the blue.. err try putting that session_start() of yours at the first line of php (befor the headers)
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Post by cdickson »

Actually there is something in the code whereby if the form action is any page other than the order.php page where the form is, the information does not get inserted into the database.

Whether I set the action to "confirmorder.php" (where my sessions are currently messed up by the way) or if it is just the page "success.php" where the only thing on the page is the word "Success!', then INSERT does not happen.
Post Reply