cmaclennan Posted May 15, 2009 Share Posted May 15, 2009 Hey Guys, I have a form that at one time was submitting to a database as it should and for some reason it is submitting ok and not giving any kind of errors yet the data isnt ending up in the table?? What could i be missing? when I echo out the statement to see if the data is being posted by the script everything shows up normal but it's not getting entered. Thanks for your help Quote Link to comment Share on other sites More sharing options...
.josh Posted May 15, 2009 Share Posted May 15, 2009 post code. Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted May 15, 2009 Author Share Posted May 15, 2009 Sorry one other thing i have just noticed the form submits to 2 tables and the data is going into the second table ok but not the first, below is the query. // Make the query. $query = "INSERT INTO rma (rma_id, date, store_company, address, address2, city, state_prov, zip_postal, country, phone, ext, contact) VALUES ('$ri', '$dt', '$co', '$ad' , '$ad2', '$ct', '$sp', '$zp', '$cou', '$ph', '$ex', '$cn')"; $result = @mysql_query ($query); // Run the query. $rma_id = mysql_insert_id(); $result = @mysql_query ($query); // Run the query. foreach($_POST as $key => $value) { //looping through all posted fields if(strpos($key, 'item_') !== false) { //found a part field, need to find out the _x number, then insert the entire row $suffix = substr($key, -1); //this gets the _x number $item = $_POST['item_' . $suffix]; $description = $_POST['description_' . $suffix]; $qty = $_POST['qty_' . $suffix]; $serial = $_POST['serial_' . $suffix]; $mfg = $_POST['mfg_' . $suffix]; $tariff = $_POST['tariff_' . $suffix]; $value = $_POST['value_' . $suffix]; $query = "INSERT INTO rma_parts (item, description, qty, serial, mfg, tariff, value, rma_id) VALUES ('$item', '$description', '$qty', '$serial', '$mfg', '$tariff', '$value', '$rma_id')"; $result = mysql_query($query); } } if ($result) { // If it ran OK. // Send an email, if desired. // Print a message. echo '<h1 id="mainhead">Thank you!</h1> <p>Your entry has now been registered.</p><p><br /></p>'; // Include the footer and quit the script (to not show the form). exit(); } else { // If it did not run OK. echo '<h1 id="mainhead">System Error</h1> <p class="error">Your entry could not be registered due to a system error. We apologize for any inconvenience.</p>'; // Public message. echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message. exit(); } mysql_close(); // Close the database connection. } else { // Report the errors. echo '<h1>Error!</h1> <p class="error">The following error(s) occurred:<br />'; foreach ($errors as $msg) { // Print each error. echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p><p><br /></p>'; } // End of if (empty($errors)) IF. } // End of the main Submit conditional. Quote Link to comment Share on other sites More sharing options...
.josh Posted May 15, 2009 Share Posted May 15, 2009 you sure you're looking in the right table/database? Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted May 15, 2009 Author Share Posted May 15, 2009 Yep all the tables im using are on the same database and its the only one the site connects too and there is only the one table with that name and it enters the parts (2nd part of query) but not the first. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 15, 2009 Share Posted May 15, 2009 And remove the @ in front of the mysql_query() statements. If you are getting a php error generated on a mysql_query() statement, it means that your database connection is not present and you should never get to the point of executing a mysql_query() statement. What php errors where you getting before you put the @ in the code? Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted May 15, 2009 Author Share Posted May 15, 2009 This is the same query structure as i am using succesfully on a number of other forms, I have just noticed that when i try to enter the query directly in the database it seems to think it's creating a duplicate entry for the primary key and won't allow it to go in. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 15, 2009 Share Posted May 15, 2009 So why would you run it twice? See the lines with your two comments up there? You ran the same query twice. Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted May 15, 2009 Author Share Posted May 15, 2009 Im not sure i understand or perhaps i explained wrong? what am i running twice? and I dont get any errors when i remove the @ from the mysql_query () statements Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 15, 2009 Share Posted May 15, 2009 You don't see the 2 lines you have comments on? The comments that says "// Run the query" Read the first 6 lines of the code you posted. Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted May 15, 2009 Author Share Posted May 15, 2009 Yes ok I do see that now, I understood from someone else that it should be that way given the mysql_insert_id tag? I assume that is not necessary then? Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted May 15, 2009 Author Share Posted May 15, 2009 Ok so I removed that part but am still stuck with the original issue of the data posting to the second table in the query bit not the first. Could there be something else I am missing? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 15, 2009 Share Posted May 15, 2009 Gah... that's not what I meant. First of all, mysql_insert_id isn't a tag, whatever that means. It's a function. Anyways, the point is you ran the same query twice. Forget about mysql_insert_id(). If you looked at the code, you will see you ran the same query twice. Why would you run it twice? That's the error you described. You're inserting rma_id twice, which caused a duplicate conflict because I'm guessing it's a PK. Though, you never really need to insert anything into a PK column. And don't remove the mysql_insert_id() line either because you'll need it in your second query. Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted May 15, 2009 Author Share Posted May 15, 2009 Ok I removed the second submit of the query as you described not the mysql_insert_id () so it is not submitting twice however I am still having the same problem, and the only reason I have anything in the rma_id field is a function to generate a unique id number as opposed to just auto incrementing a basic number. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 15, 2009 Share Posted May 15, 2009 Are you getting any errors? Also, please post an updated version of your code. Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted May 15, 2009 Author Share Posted May 15, 2009 Still not getting any error as at all and here is the code for the queries again, if you would like me to post the page just let me know. // Make the query. $query = "INSERT INTO rma (rma_id, date, store_company, address, address2, city, state_prov, zip_postal, country, phone, ext, contact) VALUES ('$ri', '$dt', '$co', '$ad' , '$ad2', '$ct', '$sp', '$zp', '$cou', '$ph', '$ex', '$cn')"; $result = mysql_query ($query); // Run the query. $rma_id = mysql_insert_id(); foreach($_POST as $key => $value) { //looping through all posted fields if(strpos($key, 'item_') !== false) { //found a part field, need to find out the _x number, then insert the entire row $suffix = substr($key, -1); //this gets the _x number $item = $_POST['item_' . $suffix]; $description = $_POST['description_' . $suffix]; $qty = $_POST['qty_' . $suffix]; $serial = $_POST['serial_' . $suffix]; $mfg = $_POST['mfg_' . $suffix]; $tariff = $_POST['tariff_' . $suffix]; $value = $_POST['value_' . $suffix]; $query = "INSERT INTO rma_parts (item, description, qty, serial, mfg, tariff, value, rma_id) VALUES ('$item', '$description', '$qty', '$serial', '$mfg', '$tariff', '$value', '$rma_id')"; $result = mysql_query($query); } } if ($result) { // If it ran OK. // Send an email, if desired. // Print a message. echo '<h1 id="mainhead">Thank you!</h1> <p>Your entry has now been registered.</p><p><br /></p>'; // Include the footer and quit the script (to not show the form). exit(); } else { // If it did not run OK. echo '<h1 id="mainhead">System Error</h1> <p class="error">Your entry could not be registered due to a system error. We apologize for any inconvenience.</p>'; // Public message. echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message. exit(); } mysql_close(); // Close the database connection. } else { // Report the errors. echo '<h1>Error!</h1> <p class="error">The following error(s) occurred:<br />'; foreach ($errors as $msg) { // Print each error. echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p><p><br /></p>'; } // End of if (empty($errors)) IF. } // End of the main Submit conditional. Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted May 15, 2009 Author Share Posted May 15, 2009 it would appear as though when i submit the form it's not creating a unique id for rma_id as i intended and it's trying to enter the same one over and over again which is not allowing it to submit to the first table but i have no idea why its doing that? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 15, 2009 Share Posted May 15, 2009 Where are you defining $ri? Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted May 15, 2009 Author Share Posted May 15, 2009 At the top of the page along with a number of trim functions and checks for required fields. Its the first entry as follows: // Generate Unique RMA ID { $ri = uniqid (rand (), false); } Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted May 15, 2009 Author Share Posted May 15, 2009 I figured it out, thanks for your help everyone. Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted May 15, 2009 Author Share Posted May 15, 2009 It would appear as though I spoke to soon, back to having the same problem, it's only submitting to the second table and keeps trying to submit the same primary key to the first but i don't know why?? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 15, 2009 Share Posted May 15, 2009 Are you running INSERT inside a loop? What is rma_id? Is it a PK? Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted May 15, 2009 Author Share Posted May 15, 2009 No not that im aware of at least, sorry im still a noob as im sure you've noticed. Quote Link to comment Share on other sites More sharing options...
cmaclennan Posted May 15, 2009 Author Share Posted May 15, 2009 Ok so i've now even removed anything that would enter anything into the rma_id field in hopes of just allowing it to auto incremement and it still will not go into that table, im at a loss as to what's going on here and what i should do as the next step? Quote Link to comment 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.