Page 1 of 1

updating a table with exploded variables

Posted: Sun Mar 15, 2009 8:38 am
by Noobie
Hi everyone!

I'm trying to figure out how to update a table with information taken from an exploded variable but I can't figure out what's going wrong...

I've got two tables - orders and services.

I've got a form with a select box whose text is taken from two separate columns and whose value is the two concantented columns. I then want to take that concantented value and explode it and update the table with both bits going into separate columns. Trouble is that the first column is being updated with the unexploded info and the second column gets nothing. Can't figure out which bit is going wrong.

This is what I've got:

The select box and query (which I should add is being replicated multiple times on the page):

Code: Select all

 
<select  name="service" >
       <option value="Choose">Choose a Size</option>
            <?php
                   $result = @mysql_query("SELECT service_name, service_price
                   FROM services
                   ORDER BY service_id ASC
                    ");
                   if (!$result) {
                   exit('<p>Error performing query: ' . mysql_error() . '</p>');
                    }
 
                   while ($row = mysql_fetch_array($result)) {
                   $service_name = $row['service_name'];
                   $service_price = $row['service_price'];
                   $option_value = $row['service_name'] .'-' . $row['service_price'] ;
                   $service_details = explode("-", $option_value);
                   $service_name = htmlspecialchars($service_name);
 
           echo '<option value="' . $option_value . '">' . $service_name . ' &ndash; ' . $service_price .'</option>';
                   }
                   $formid = $formid + 1; ?>
      </select>
This is where it's going wrong I think as I'm not sure where to get the exploded values back.

Code: Select all

if (isset($_POST['value'])){ // if value set, add items to order table
 
$service_details = explode("-", '$_POST[$option_value]');
$service_update = '$_POST[$service_details[0]]';
$price_update = '$_POST[$service_details[1]]';
 
$result = @mysql_query("INSERT INTO orders
                          SET id = '$_POST[custid]',
                          photoref = '$_POST[value]',
                          qty = '$_POST[qty]',
                          service =  '$service_update',
                          price = '$price_update',
                          date = CURDATE();
                     ");
   if (!$result) {
      exit('<p>Error performing query: ' . mysql_error() . '</p>');
    }
}
Thanks in advance for any advice.

Re: updating a table with exploded variables

Posted: Mon Mar 16, 2009 5:33 am
by susrisha
Hope this will help you ..

Code: Select all

 
/*Explode function testing
 * 
 */
$string = 'naresh-kumar';
$string_var = explode('-','$string');
 
print_r($string_var);
/**
 OUTPUT
  Array
(
    [0] => $string
)
 */
$string_var2 = explode('-',$string);
print_r($string_var2);
/**
 * Array
(
    [0] => naresh
    [1] => kumar
)
 */
 

Re: updating a table with exploded variables

Posted: Mon Mar 16, 2009 9:17 am
by Noobie
Hi Sunrisha

Thanks for your answer but I'm not sure I understand..?

How do I get the exploded bits of the array into the update query?

Thanks

Re: updating a table with exploded variables

Posted: Tue Mar 17, 2009 2:03 am
by susrisha
dude its a typo error.. have a look at the example..

Re: updating a table with exploded variables

Posted: Tue Mar 17, 2009 6:26 am
by Noobie
Which bit's a typo? I had a look at the example and it looks like you've got the exploded bits to print out - but I don't want them printed I want to assign them to a variable then update the table with that variable - can't figure out where to assign them to the variable - before the update query?

Re: updating a table with exploded variables

Posted: Tue Mar 17, 2009 7:09 am
by susrisha
here it is

Code: Select all

 
$service_details = explode("-", '$_POST[$option_value]'); 
print_r($service_details);
 
Please let me know a sample output of this...

Code: Select all

 
$service_details=explode("-",$_POST[$option_value]); //this should work..