Jump to content

[SOLVED] need help with a loop of sorts to do an insert into mysql


Recommended Posts

Here's my predicament. I have a flash form that pull data via php and displays some data.

The user then selects various items and it populates a datagrid component (in flash), now I need to be able to save that selection back to the database.

 

I was told PHP can't accept an array from flash.

So here's my question. I can format the results any which way , i.e. string/array etc to send to the PHP.I need to know on the php side.

1. what format is best for large amount of data from a datagrid.

2. how to take that data and do an insert into in using a loop or whatever.

 

total noob so keep it to 1st grader terminology if you can assist please. thanks :-)

Hi Will. Yes the flash sends the data through via a post.

i can get the variable back to php, i'm thinking in this format

i1=Symbol=Oyster,Odds=1,Prize=test1&i2=Symbol=Sardine,Odds=2,Prize=test2&i3=Symbol=Lobster,Odds=4,Prize=test4&i4=Symbol=skeleton,Odds=5,Prize=test5

 

but what I need help with is that php then takes that and does an INSERT into db with that data. So i'm assuming it needs to be converted to an array or somethign that PHP can loop through. I don' t know how to do that.

PHP will get it as a string.

 

 

mikesta707, thanks for the pointer but I can get the variables from flash into php. I'm struggling with spliting the string I Post to PHP for PHP to insert it.

I've been fiddling a bit, and testing in my browser with the following URL.

 

http://localhost/fishing/fishing/savePrizes.php?i1=Symbol=Oyster,Odds=1,Prize=test1&i2=Symbol=Sardine,Odds=2,Prize=test2&i3=Symbol=Lobster,Odds=4,Prize=test4&i4=Symbol=skeleton,Odds=5,Prize=test5

 

my resulting echos:

Array ( [i1] => Array ( [symbol] => Oyster [Odds] => 1 [Prize] => test1 ) [i2] => Array ( [symbol] => Sardine [Odds] => 2 [Prize] => test2 ) [i3] => Array ( [symbol] => Lobster [Odds] => 4 [Prize] => test4 ) [i4] => Array ( [symbol] => skeleton [Odds] => 5 [Prize] => test5 ) ) Array size with count: 4inside for loopinside not empty should insert Oysterinside for loopinside not empty should insert Sardineinside for loopinside not empty should insert Lobster

 

so it's getting the values but the insert is not happening, and it's not giving me any errors either.

 

here's my PHP thus far:

 

<?php

 

session_start();

include 'config.php';

include 'opendb.php';

$gameID = $_REQUEST['gameID'];

$data = array();

foreach($_GET as $key => $value){

      $raw[$key] = explode(',' ,$_GET[$key]);

  foreach($raw[$key] as $value){

$info = explode('=', $value);

$data[$key][$info['0']] = $info['1'];

}

}

 

print_r($data);

 

if ( is_array( $data ) ){

  // Find the number of keys in the array //

 

  echo "Array size with count: ".count($data);

    $n = count($data);

  for ( $i = 1; $i < $n; $i++ ){

 

 

echo "inside for loop";

          $key = 'i'.$i;

          // Read the various keys, you should know what to expect! //

          if( !empty($data[$key]['Symbol'])){

echo "inside not empty should insert ".$data[$key]['Symbol'];

  $sql = 'INSERT INTO gamePrizes(name, prize, odds, gameID) VALUES("'.$data[$key]['Symbol'].'","'.$data[$key]['Odds'].'", "'.$data[$key]['Prize'].'",, "'.$gameID.'",)';

   

          }else{

    $errorMsg = "Error inserting into db ".sql.error();

    echo $errorMsg;

        }

    }

}

 

 

?>

 

 

hey after pasting the code I saw the error of my ways, apart from what you said, also had a syntax error which I only managed to catch with some proper error checking.

 

thanks, it's working now :-)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.