ismoore Posted September 18, 2013 Share Posted September 18, 2013 Hi. I need some help please. I'm still new to all this coding and have been at a road block for the past few days trying to figure this out. Here's my problem: I have created a form for my customer to use to submit orders. It is nothing complex, basically when the form has been filled out and the submit button pressed, the following should occur: 1. Information is loaded into a SQL database created through phpmyadmin 2. Generate a generic text file with the form results and email this to me. Please note that the text file is given an order # based on the next available order number in the database 3. A confirmation page is shown confirming the order has been placed and shows the order # given to the order For some reason, I'm not able to get the information submitted to stay into the database. When the submit button is pressed, the information is loaded into the database and the text file is created successfully and emailed to me. I receive the email fine and all the requested data is in it. However, once this process is completed, the information disappears from the database and instead of going to the confirmation page it goes back to an empty form ready for another order to be placed. Any ideas of what this could be? Below is my code. (some of the information changed to hide customer information) I would appreciate any help on this. Thanks. <?PHPset_include_path('.:/usr/share/php/');require_once "Mail.php";//get inventoryinclude('inventory/read_abc_inventory.php');define('SIS_TEST_MODE',false); // SET THIS TO TRUE TO NOT WRITE TO THE DATABASE AND TO SEND TESTS TO ISRAEL//define('SIS_THIS_PAGE_URL','abcform.php');define('SIS_THIS_PAGE_URL','/extranet/node/5');$form = "YES";if ($_POST['submitted']) {// Put in where form came from$IP = $_SERVER['REMOTE_ADDR'];//Inspect imported variables againrequire_once ('php_uploads/abcconnect.php'); // Connect to the db.$query = "SELECT id FROM abcorders ORDER BY id desc LIMIT 0,1"; $result = @mysql_query ($query); // Run the query.while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {$ordernumber=$row['id']+1; // incrementing the counter value by one}//$query = "UPDATE `abc_db`.`abcordernumber` SET ordernum=$ordernumber WHERE `ordernumber`.`id` =1 LIMIT 1";//$result = @mysql_query ($query); // Run the query. if (empty($errors)) { // If everything's OK. if (!SIS_TEST_MODE) { $to = "imoore@abc.com"; } else { $to = "scott@abc.com"; }$_POST['order_number'] = $ordernumber;$zzz = $_POST['PN_or_FRU'];$_POST['Part_Description'] = $partdesc[$zzz];// Assemble Arrays to create csv file to email $rfqfields1 = array('order_number','Date','purchaser','PO','Shipper','Shipping_Service_Level','Company_Name','Contact_Name','Phone_number','Street_1','Street_2','Suite_or_Bld','City','State','Zip','Country');$rfqfields2 = array();$s = 1;foreach ($inventoryarray as $key => $value) { // Set name of current row so that it can be used as a variable if ($key != '0') { $rfqfields2[] = $key.'_Quantity'; $partno[$s] = $_POST[$key.'_Quantity']; $s ++; }} $rfqfields3 = array('Special_Instructions');// MERGE ARRAYS$rfqfields = array_merge($rfqfields1, $rfqfields2, $rfqfields3); //print_r($rfqfields); $bodyb = ''; $completeparts = ''; foreach ($rfqfields as $A) { // Set name of current row so that it can be used as a variable $n1 = 'columnname'; $n1 = $A; // handle the parts for db in correct order. if (in_array ($A, $rfqfields2, FALSE )) {$ANAME = str_replace('_Quantity', '', $A); $partsquery = "SELECT id FROM abcparts WHERE active='yes' AND partno='$ANAME'"; //echo $partsquery; $partsresult = @mysql_query ($partsquery); // Run the query. while ($parts = mysql_fetch_array($partsresult, MYSQL_ASSOC)) { $completeparts .= ' `part_'.$parts['id'].'`,'; }} $columnname = $_POST[$A]; //if ($bodya == '') { $bodya = ucwords(dunderscore($A));} else { $bodya .= "\t".ucwords(dunderscore($A)); } if ($bodyb == '') { $bodyb = $columnname;} else { $bodyb .= "\t".$columnname; } }//echo $completeparts;// BEGIN ORDER DATABASING HERE$dbquery = "INSERT INTO `abcorders` (`id`, `thedate`, `purchaser`, `PO`, `shipper`, `shipping_service_level`, `company_name`, `contact_name`, `phone`, `address1`, `address2`, `address3`, `city`, `state`, `zip`, `country`,";$dbquery .= $completeparts;$dbquery .= "`special_instructions`) VALUES ("; foreach ($rfqfields1 as $A) { // Set name of current row so that it can be used as a variable $n1 = 'columnname'; $n1 = $A; $columnname = mysql_escape_string ($_POST[$A]); if ($A == 'order_number') {$dbquery .= "'".dreturn($columnname)."'";}else { $dbquery .= ", '".dreturn($columnname)."'";} } foreach ($partno as $key => $value) { $dbquery .= ", '".dreturn($value)."'"; } foreach ($rfqfields3 as $A) { // Set name of current row so that it can be used as a variable $n1 = 'columnname'; $n1 = $A; $columnname = $_POST[$A]; $dbquery .= ", '".mysql_escape_string (dreturn($columnname))."'"; } $dbquery .= ")"; //echo $dbquery; //fix query //$findex ="!"; //$repex = ",'!'"; //$dbquery = str_replace($findex, $repex,$dbquery);if (!SIS_TEST_MODE) { $dbresult = mysql_query ($dbquery); // Run the query. } else { echo 'ORDER INSERT QUERY = '.$query.'<hr />'; }if (!$dbresult) { $to2 = 'imoore@abc.com'; $headers2 = "From: error@abc.com\n"; $headers2 .= "MIME-Version: 1.0\n"; "Content-type: text/html; charset=iso-8859-1"; $subject2 = "Failure adding order to abc db"; $body2 = $dbquery; $body2 .= 'Part numbers:\n '.print_r($partno, true).'\nPost Items:\n '.print_r($_POST, true); $mailsuccesserr = mail($to2, $subject2, $body2, $headers2);}$host = "mail.abc.com";$username = "imoore@abc.com";$password = "abcabc";// create the new text file to attach to email.$textfile = $bodyb;$smtp = Mail::factory('smtp',array ('host' => $host,'auth' => true,'username' => $username,'password' => $password));$from = "ABC Extranet <imoore@abc.com>";$subject = "ABC Form Submission - Order # ".$ordernumber;$body = "ABC Form Submission\n";$headers = array ('From' => $from,'Subject' => $subject,'MIME-Version' => '1.0','Content-type' => "application/xml; name=".$ordernumber.".txt");if (!SIS_TEST_MODE) { $mail = $smtp->send($to, $headers, $textfile); //$mail_sent = @mail($to, $subject, $textfile, $headers); } } //$to = 'derron@abc.com'; //$mail_sent = @mail($to, $subject, $textfile, $headers); $to = 'andy@abc.com'; //$mail_sent = @mail($to, $subject, $textfile, $headers); $mail = $smtp->send($to, $headers, $textfile); $to = 'johnnette@abc.com'; //$mail_sent = @mail($to, $subject, $textfile, $headers); $mail = $smtp->send($to, $headers, $textfile); $to = 'steven@abc.com'; //$mail_sent = @mail($to, $subject, $textfile, $headers); //$mail = $smtp->send($to, $headers, $textfile);}//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" //echo $mail_sent ? "Mail sent" : "Mail failed"; //echo $textfile; $form = "NO";{ // Error occurred. $error = '<p><font color="red">You forgot to fill in some required fields, Please try again</font></p>'; $form = 'DISPLAY'; } // Insert address into address book if not duplicate$Company_Name = $_POST['Company_Name'];$Contact_Name = $_POST['Contact_Name'];$Phone_number = $_POST['Phone_number'];$Street_1 = $_POST['Street_1'];$Street_2 = $_POST['Street_2'];$Suite_or_Bld = $_POST['Suite_or_Bld'];$City = $_POST['City'];$State = $_POST['State'];$Zip = $_POST['Zip'];$Country = $_POST['Country'];$query = "SELECT * FROM abc_address_book WHERE Company_Name='$Company_Name' AND Contact_Name='$Contact_Name' AND Phone_number='$Phone_number' AND Street_1='$Street_1' AND Street_2='$Street_2' AND Suite_or_Bld='$Suite_or_Bld' AND City='$City' AND State='$State' AND Zip='$Zip' AND Country='$Country'"; $result = @mysql_query ($query); // Run the query.//echo $query.'<br />';if ($result && @mysql_num_rows($result) == 1) { $addedtodb='no'; } else {$query = "INSERT INTO `abc_address_book` (`id`, `Company_Name`, `Contact_Name`, `Phone_number`, `Street_1`, `Street_2`, `Suite_or_Bld`, `City`, `State`, `Zip`, `Country`) VALUES(NULL, '$Company_Name', '$Contact_Name', '$Phone_number', '$Street_1', '$Street_2', '$Suite_or_Bld', '$City', '$State', '$Zip', '$Country')"; if (!SIS_TEST_MODE) { $result = @mysql_query ($query); // Run the query. } else { echo 'ADDRESS BOOK QUERY = '.$query.'<hr />'; }$addedtodb = 'yes';}if (isset($_POST['save_as_template']) && $_POST['save_as_template'] == 'yes') {$template_name = mysql_escape_string ($_POST['template_name']); if ($template_name != '') { $query = "INSERT INTO `abc_db`.`abc_order_templates` ( `id` , `temp_name` , `temp_purchaser` , `temp_PO` , `temp_shipper` , `temp_shipping_service_level` , `created` ) VALUES ( NULL, '$template_name', '".mysql_escape_string ($_POST['purchaser'])."', '".mysql_escape_string ($_POST['PO'])."', '".mysql_escape_string ($_POST['Shipper'])."', '".mysql_escape_string ($_POST['Shipping_Service_Level'])."', NOW())"; $result = @mysql_query ($query); // Run the query. if ($result) { echo '<p>Your Template has been saved</p>'; } else { echo 'Error saving template - '.$query; } } else { echo '<p style="color:red;">Sorry, the template was not added because a name was not set.</p>'; }}// end if form submittedinclude_once('php_uploads/abcconnect.php');?><!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><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>ABC Order Form</title></head><body><script type="text/javascript"><!--function AddressBook(){<?PHP$query = "SELECT * FROM abc_address_book ORDER BY Company_Name";$result = @mysql_query ($query); // Run the query.$stored_addresses = '';function cleanBreak($in) { $search = array("\n","\r"); $replace = array('',''); $out = str_replace($search,$replace,$in); return $out;}while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {$stored_addresses .= '<option value="'.$row['id'].'">'.$row['Company_Name'].' - '.$row['Contact_Name'].'</option>';echo 'if (document.abook.stored.value == '.$row['id'].') {Company_Name = \''.cleanBreak($row['Company_Name']).'\';Contact_Name = \''.cleanBreak($row['Contact_Name']).'\';Street_1 = \''.cleanBreak($row['Street_1']).'\';Phone_number = \''.cleanBreak($row['Phone_number']).'\';Street_2 = \''.cleanBreak($row['Street_2']).'\';Suite_or_Bld = \''.cleanBreak($row['Suite_or_Bld']).'\';City = \''.cleanBreak($row['City']).'\';State = \''.cleanBreak($row['State']).'\';Zip = \''.cleanBreak($row['Zip']).'\';Country = \''.cleanBreak($row['Country']).'\';}';}?>document.abook.Company_Name.value=Company_Name;document.abook.Contact_Name.value=Contact_Name;document.abook.Street_1.value=Street_1;document.abook.Phone_number.value=Phone_number;document.abook.Street_2.value=Street_2;document.abook.Suite_or_Bld.value=Suite_or_Bld;document.abook.City.value=City;document.abook.State.value=State;document.abook.Zip.value=Zip;document.abook.Country.value=Country;}//--></script><?PHP if (isset($form) && $form != "NO") {//<form action="/extranet/node/5" method="post">?><script language="JavaScript">function disableEnterKey(e){var key;if(window.event)key = window.event.keyCode; //IEelsekey = e.which; //firefoxreturn (key != 13);}</script><div style="padding:5px;margin:5px;border:1px solid #666666;background-color:#e8e8e8;"><form action="<?PHP echo SIS_THIS_PAGE_URL;?>" method="get" name="temp">Order Template: <select name="temp_id" onchange="this.form.submit()" ><option>Select Template</option><?PHP $query = "SELECT * FROM `abc_db`.`abc_order_templates` ORDER BY `temp_name`"; $result = @mysql_query ($query); // Run the query. if (mysql_num_rows($result) >=1) { while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo '<option value="'.$row['id'].'">'.$row['temp_name'].'</option>'; } }?></select></form></div><?PHPif (isset($_GET['temp_id'])) { $temp_id = mysql_escape_string ($_GET['temp_id']); $query = "SELECT * FROM `abc_db`.`abc_order_templates` WHERE `id`=$temp_id"; $result = @mysql_query ($query); // Run the query. if (mysql_num_rows($result) >=1) { while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { extract($row); } }}?><form action="<?PHP echo SIS_THIS_PAGE_URL;?>" method="post" name="abook"><table cellpadding="3" cellspacing="0" border="0" width="700"><tr valign="top"><td>Current Date & Time:</td><td><input type="text" onKeyPress="return disableEnterKey(event)" style="width:300px;" name="Date" value="<?PHP echo date("Y-m-d H:i:s", strtotime('+1 hour')); ?>" /></td></tr><tr valign="top"><td>Purchaser:</td><td><select name="purchaser" ><option value="Please Select">Please Select</option><option value="Jacob" <?PHP if (isset($_GET['temp_id']) && $temp_purchaser=='Jacob') { echo ' selected="selected" ';}?>>Jacob</option><option value="Patrick"<?PHP if (isset($_GET['temp_id']) && $temp_purchaser=='Patrick') { echo ' selected="selected" ';}?>>Patrick</option></select></td></tr><tr valign="top"><td>PO # </td><td><input type="text" onKeyPress="return disableEnterKey(event)" style="width:300px;" name="PO"<?PHP if (isset($_GET['temp_id'])) { echo ' value="'.$temp_PO.'" ';}?> /></td></tr><tr valign="top"><td>Shipper </td><td><select name="Shipper" ><option value="Please Select">Please Select</option><option value="UPS"<?PHP if (isset($_GET['temp_id']) && $temp_shipper=='UPS') { echo ' selected="selected" ';}?>>UPS</option><option value="FedEX"<?PHP if (isset($_GET['temp_id']) && $temp_shipper=='FedEX') { echo ' selected="selected" ';}?>>FedEX</option><option value="LTL"<?PHP if (isset($_GET['temp_id']) && $temp_shipper=='LTL') { echo ' selected="selected" ';}?>>LTL</option></select></td></tr><tr valign="top"><td>Shipping Service Level: </td><td><select name="Shipping_Service_Level" ><option value="Please Select">Please Select</option><option value="Next Day Air"<?PHP if (isset($_GET['temp_id']) && $temp_shipping_service_level=='Next Day Air') { echo ' selected="selected" ';}?>>Next Day Air</option><option value="Next Day Air Saver"<?PHP if (isset($_GET['temp_id']) && $temp_shipping_service_level=='Next Day Air Saver') { echo ' selected="selected" ';}?>>Next Day Air Saver</option><option value="2nd Day Air"<?PHP if (isset($_GET['temp_id']) && $temp_shipping_service_level=='2nd Day Air') { echo ' selected="selected" ';}?>>2nd Day Air</option><option value="Ground"<?PHP if (isset($_GET['temp_id']) && $temp_shipping_service_level=='Ground') { echo ' selected="selected" ';}?>>Ground</option></select></td></tr><tr valign="top"><td colspan="2"> </td></tr><tr valign="top"><td >Save Above as Template?</td><td><select name="save_as_template"><option value="no" selected="selected">No</option><option value="yes">Yes</option></select> Template Name: <input type="text" name="template_name" onKeyPress="return disableEnterKey(event)" /></td></tr><tr valign="top"><td colspan="2"> </td></tr><tr valign="top"><td colspan="2"><strong>Address Fields</strong></td></tr><tr valign="top"><td style="padding-left:10px;">Stored Addresses</td><td><select name="stored" onChange="AddressBook();"><?PHPecho $stored_addresses;?></select><br /><span style="font-size:80%;color:#666666;">Select an address from the address book above, or enter a new address in the fields below.</span></td></tr><tr valign="top"><td style="padding-left:10px;">Company Name</td><td><input type="text" onKeyPress="return disableEnterKey(event)" style="width:300px;" name="Company_Name" /></td></tr><tr valign="top"><td style="padding-left:10px;">Contact Name</td><td><input type="text" onKeyPress="return disableEnterKey(event)" style="width:300px;" name="Contact_Name" /></td></tr><tr valign="top"><td style="padding-left:10px;">Phone number</td><td><input type="text" onKeyPress="return disableEnterKey(event)" style="width:300px;" name="Phone_number" /></td></tr><tr valign="top"><td style="padding-left:10px;">Street 1</td><td><input type="text" onKeyPress="return disableEnterKey(event)" style="width:300px;" name="Street_1" /></td></tr><tr valign="top"><td style="padding-left:10px;">Street 2</td><td><input type="text" onKeyPress="return disableEnterKey(event)" style="width:300px;" name="Street_2" /></td></tr><tr valign="top"><td style="padding-left:10px;">Suite/Bld</td><td><input type="text" onKeyPress="return disableEnterKey(event)" style="width:300px;" name="Suite_or_Bld" /></td></tr><tr valign="top"><td style="padding-left:10px;">City</td><td><input type="text" onKeyPress="return disableEnterKey(event)" style="width:300px;" name="City" /></td></tr><tr valign="top"><td style="padding-left:10px;">State</td><td><input type="text" onKeyPress="return disableEnterKey(event)" style="width:100px;" name="State" /></td></tr><tr valign="top"><td style="padding-left:10px;">Zip/Postal Code</td><td><input type="text" onKeyPress="return disableEnterKey(event)" style="width:100px;" name="Zip" /></td></tr><tr valign="top"><td style="padding-left:10px;">Country </td><td><input type="text" onKeyPress="return disableEnterKey(event)" style="width:300px;" name="Country" /></td></tr><tr valign="top"><td colspan="2"></td></tr><tr valign="top"><td colspan="2"><strong>Parts Current Inventory:</strong> </td><tr valign="top"><td colspan="2"><?PHPecho '<table width="100%" border="0" cellspacing="4" cellpadding="0" style="margin-left:10px;margin-bottom:20px;"><tr><td><strong>Part Number</strong></td><td><strong>Part Description</strong></td><td><strong>Current Inventory</strong></td><td><strong>Order Quantity</strong></td></tr>';foreach ($inventoryarray as $key => $value) { // Set name of current row so that it can be used as a variable if ($key != '0') { $query = "SELECT partdesc as A FROM abcparts WHERE partno='$key' AND active='yes' LIMIT 1"; $result = @mysql_query ($query); // Run the query. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $thedesc = $row['A'];} echo '<tr><td>'.$key.'</td><td>'.$thedesc.'</td><td>'.$value.'</td>'; echo '<td><input type="text" onKeyPress="return disableEnterKey(event)" name="'.$key.'_Quantity" value="0"/></td></tr>'; }}echo '</table>'; ?></td></tr><tr valign="top"><td>Special Instructions: </td><td><textarea name="Special_Instructions" style="width:350px;"></textarea></td></tr><tr valign="top"><td colspan="2"><input type="submit" name="submitted" value="Submit"/></td></tr></table></form><?PHP } else {if (isset($addedtodb) && ($addedtodb == 'yes')) { $ordernumber .= ' <br />The Address you entered has been stored to speed up future orders.'; }echo !PEAR::isError($mail) ? "<h2>Order sent</h2>Your request has been submitted and assigned ABC order # ".$ordernumber." " : "<h2>Order failed</h2>"; echo '<p>To submit another request <a href="/extranet/node/5">Click Here</a></p>';} ?></body></html> Quote Link to comment https://forums.phpfreaks.com/topic/282258-form-submits-but-doesnt-save-into-database/ Share on other sites More sharing options...
akphidelt2007 Posted September 18, 2013 Share Posted September 18, 2013 You shouldn't suppress your errors like that, they might tell you what is going wrong. Quote Link to comment https://forums.phpfreaks.com/topic/282258-form-submits-but-doesnt-save-into-database/#findComment-1450152 Share on other sites More sharing options...
jcbones Posted September 18, 2013 Share Posted September 18, 2013 Remove the error suppression: @ Next you need to return any errors that the database is sending. $result = mysql_query($query) or trigger_error(mysql_error()); Additionally, this code will not work very long, as mysql has been depreciated and is slated for removal. You should change over to the PDO or mysqli classes, in order to avoid future downtime. mysql_query(PHP 4, PHP 5) mysql_query — Send a MySQL query WarningThis extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_query() PDO::query() Quote Link to comment https://forums.phpfreaks.com/topic/282258-form-submits-but-doesnt-save-into-database/#findComment-1450156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.