updating a table with exploded variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

updating a table with exploded variables

Post 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.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: updating a table with exploded variables

Post 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
)
 */
 
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Re: updating a table with exploded variables

Post 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
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: updating a table with exploded variables

Post by susrisha »

dude its a typo error.. have a look at the example..
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Re: updating a table with exploded variables

Post 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?
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: updating a table with exploded variables

Post 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..
 
Post Reply