badeand Posted January 30, 2013 Share Posted January 30, 2013 Could anyone help me and see why the script stores the ID into "replacement" row instead of the value from the form. If needed i can post the form script. ANY help would be appreciated Here is the process code <? include("db.php"); echo "<pre>"; foreach($_POST['fields'] as $items) { $qcheck1= mysql_query("select * from fields where id='".$items['hmodel']."'") or die(mysql_error()); $rcheck1=mysql_fetch_array($qcheck1); $hmodel = $rcheck1['value']; $qcheck2= mysql_query("select * from fields where id='".$items['case']."'") or die(mysql_error()); $rcheck2=mysql_fetch_array($qcheck2); $case = $rcheck2['value']; $qcheck3= mysql_query("select * from fields where id='".$items['error']."'") or die(mysql_error()); $rcheck3=mysql_fetch_array($qcheck3); $error = $rcheck3['value']; $q=mysql_query("INSERT INTO `customer_data` (`id`, `name`, `number`, `address`, `case`, `hmodel`, `error`, `other`, `hardwareid`, `replacement`, `hardwarerep`) VALUES (NULL, '".mysql_real_escape_string($_POST['name'])."', '".mysql_real_escape_string($_POST['number'])."', '".mysql_real_escape_string($_POST['address'])."', '".mysql_real_escape_string($case)."', '".mysql_real_escape_string($hmodel)."', '".mysql_real_escape_string($error)."', '".mysql_real_escape_string($items['other'])."', '".mysql_real_escape_string($items['hardwareid'])."', '".mysql_real_escape_string($items['replacement'])."', '".mysql_real_escape_string($items['hardwarerep'])."')") or die(mysql_error()); echo "<br> Din registrering har ID : ".mysql_insert_id(); } ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted January 30, 2013 Share Posted January 30, 2013 What ID? And yes, please post the form. Quote Link to comment Share on other sites More sharing options...
badeand Posted January 30, 2013 Author Share Posted January 30, 2013 Here is the form code. It stores the ID from another row in the mysql database instead of the value of the this row. It should get the value from another mysql table but instead it gets the ID of the row that it should get the value from.. <script type="text/javascript"> function confirmSubmit(){ var r=confirm("Er du sikker på at du vil sende skjema?"); if(r) return true; return false; } </script> <script type="text/javascript"> function addNewCase() { $('#afterdiv').after('<fieldset><legend>Legg til Utstyr <a href="#r" onclick="removeCase(this);" style="font-size: 11px">(x)Slett</a></legend><div><div class="item"><label for="case">Type Sak</label><select name="fields['+currentval+'][case]" id="case"><option value="">Vennligst velg type sak</option><? $q=mysql_query("select * from fields where type='case_type'") or die(mysql_error()); while($r=mysql_fetch_array($q)) { ?><option value="<?=$r['id']?>"><?=$r['value']?></option><? } ?></select></div><div class="item"><label for="error">Feilmelding</label><select name="fields['+currentval+'][error]" id="error"><option value="">Vennligst velg feilmelding</option><? $q=mysql_query("select * from fields where type='error_message'") or die(mysql_error()); while($r=mysql_fetch_array($q)) { ?><option value="<?=$r['id']?>"><?=$r['value']?></option><? } ?></select></div><div><label for="other">Annen feil</label><textarea name="fields['+currentval+'][other]" id="other" rows="12" cols="30"></textarea></div><label>Innlevert Utstyr</label></br></br><div class="item"><label for="hardwareid">Hardware ID</label><input type="text" name="fields['+currentval+'][hardwareid]" value="" size="30" class="hardwareid" id="fields['+currentval+'][hardwareid]"></div><div class="item"><label for="hardware">Demontert Modell</label><select name="fields['+currentval+'][hmodel]" id="fields['+currentval+'][hmodel]" class="hmodel"><option value="">Vennligst velg type utstyr</option><? $q=mysql_query("select * from fields where type='hardware_model'") or die(mysql_error()); while($r=mysql_fetch_array($q)) { ?><option value="<?=$r['id']?>"><?=$r['value']?></option><? } ?></select></br></div></br></br><label>Erstatnings utstyr</label></br><br><div class="item"><label for="hardwarerep">Hardware ID</label><input type="text" name="fields['+currentval+'][hardwarerep]" class="hardwarerep" value="" size="30" id="hardwarerep"></div><div class="item"><label for="replacement">Monetert Modell</label><select name="fields['+currentval+'][replacement]" id="fields['+currentval+'][replacement]" class="rmodel"><option value="">Vennligst velg type utstyr</option><? $q=mysql_query("select * from fields where type='replacement_model'") or die(mysql_error()); while($r=mysql_fetch_array($q)) { ?><option value="<?=$r['id']?>"><?=$r['value']?></option><? } ?></select></div></div></fieldset>'); currentval++; } function removeCase(el) { $(el).parent().parent().remove(); currentval--; } $(".hardwareid").live('blur', function() { var hi = $(this).attr('id').replace("fields[", "").replace("][hardwareid]",""); $.post("select.php", { hardid: $(this).val() ,type: 'id' }, function(data) { $('.hmodel').each(function() { if($(this).attr("id") == "fields["+hi+"][hmodel]") { $(this).find("option[value='"+data+"']").attr("selected",true); } }); }); }); $(".hardwarerep").live('blur', function() { var hi = $(this).attr('name').replace("fields[", "").replace("][hardwarerep]",""); $.post("select.php", { hardid: $(this).val(),type: 'rep' }, function(data) { $('.rmodel').each(function() { if($(this).attr("id") == "fields["+hi+"][replacement]") { $(this).find("option[value='"+data+"']").attr("selected",true); } }); }); }); </script> Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 30, 2013 Share Posted January 30, 2013 Are you sure you really want to store the value and NOT the ID? That's sort of the point of why you have a relational database - so you can define a value (or values) in one place and reference that values in others without having to duplicate the data. Looking at the code in your first post I don't see why it would be saving the ID as opposed to the value (unless the values in that table are the same as the IDs). But, that code is very inefficient.You should NEVER run queries in loops. If you do store the IDs (as I think you should) you can create ALL the records with just ONE query. But, if you do need to store the values then you can at least replace those four queries in the loop with a single query. foreach($_POST['fields'] as $items) { //Process values for DB use $name = mysql_real_escape_string($_POST['name']); $number = mysql_real_escape_string($_POST['number']); $address = mysql_real_escape_string($_POST['address']); $other = mysql_real_escape_string($items['other']); $hardwareid = mysql_real_escape_string($items['hardwareid']); $replacement = mysql_real_escape_string($items['replacement']); $hardwarerep = mysql_real_escape_string($items['hardwarerep']); $caseID = intval($items['case']); $hmodelID = intval($items['hmodel']); $errorID = intval($items['error']); $query = "INSERT INTO `customer_data` (`name`, `number`, `address`, `other`, `hardwareid`, `replacement`, `hardwarerep`, `case`, `hmodel`, `error`) SELECT '$name', '$number', '$address', '$other', '$hardwareid', '$replacement', '$hardwarerep', (SELECT value FROM fields WHERE id='{$caseID}' LIMIT 1), (SELECT value FROM fields WHERE id='{$hmodelID}' LIMIT 1), (SELECT value FROM fields WHERE id='{$errorID}' LIMIT 1)" $result = mysql_query($query) or die(mysql_error()); echo "<br> Din registrering har ID : ".mysql_insert_id(); } But, if you do only store the IDs (as I think you should) then you only need to create one query by looping over the records. It will be much, much more efficient. Something like this $values = array(); foreach($_POST['fields'] as $items) { //Process values for DB use $name = mysql_real_escape_string($_POST['name']); $number = mysql_real_escape_string($_POST['number']); $address = mysql_real_escape_string($_POST['address']); $other = mysql_real_escape_string($items['other']); $hardwareid = mysql_real_escape_string($items['hardwareid']); $replacement = mysql_real_escape_string($items['replacement']); $hardwarerep = mysql_real_escape_string($items['hardwarerep']); $caseID = intval($items['case']); $hmodelID = intval($items['hmodel']); $errorID = intval($items['error']); $values[] = "('$name', '$number', '$address', '$other', '$hardwareid', '$replacement', '$hardwarerep', '$caseID', '$hmodelID', '$errorID')"; } //Create and execute ONE query $query = "INSERT INTO `customer_data` (`name`, `number`, `address`, `other`, `hardwareid`, `replacement`, `hardwarerep`, `case`, `hmodel`, `error`) VALUES " . implode(', ', $values); $result = mysql_query($query) or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 30, 2013 Share Posted January 30, 2013 (edited) +1 for running one query to process all the data you need at once (both in the form and the form processing code), and for storing the id. This will also reduce the amount of code you have, which will make it easier to see what your code is or is not doing. The reason that the id for the [replacement] form field is being stored is because that's the value that your code is putting into the INSERT query - $items['replacement'] In fact, your original code doesn't even have any code to SELECT the value that corresponds to the submitted id from the replacement form field. Edited January 30, 2013 by PFMaBiSmAd 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.