Jump to content


Photo

Stores ID instead of the value


  • Please log in to reply
4 replies to this topic

#1 badeand

badeand

    Advanced Member

  • Members
  • PipPipPip
  • 35 posts

Posted 30 January 2013 - 12:09 PM

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();


}
?>


#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,152 posts
  • LocationWA

Posted 30 January 2013 - 02:01 PM

What ID? And yes, please post the form.

#3 badeand

badeand

    Advanced Member

  • Members
  • PipPipPip
  • 35 posts

Posted 30 January 2013 - 03:42 PM

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>


#4 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,861 posts
  • LocationCanada

Posted 30 January 2013 - 04:43 PM

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());

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#5 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 30 January 2013 - 04:50 PM

+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 by PFMaBiSmAd, 30 January 2013 - 04:51 PM.

Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com