blue-genie Posted August 15, 2009 Share Posted August 15, 2009 i've got bits and pieces of code from various places, but I'm not able to achieve the final outcome I require. As a non php person, I'm trying to fiddle with the bits of code I get but nothing seems to work. here's an example I got from another forum <?php include 'config.php'; include 'opendb.php'; $gameID = '1'; $arr = "Symbol=Shark,Odds=1,Prize=sharkprize&Symbol=Crab,Odds=2,Prize=crabprize"; $data = explode(",", $arr); foreach($data as $k=>$v) { list($key,$value)=explode("=",$v); //now append the $key and $value to $data[$k] $data[$k][$key]=mysql_real_escape_string($value); } echo '<?xml version="1.0"?>'; echo '<dataxml>'; if ( is_array( $data ) ) { foreach($data as $key=>$v) { $insert = mysql_query("INSERT INTO gameprizes(name, prize, odds, gameID) VALUES('{$data[$key]['Symbol']}','{$data[$key]['Odds']}','{$data[$key]['Prize']}','{$gameID}')"); } if (!$insert) { //die("There's little problem: ".mysql_error()); $MyError = "An error occured while saving data. Please try again later." + mysql_error(); echo "<sqlError>".$MyError."</sqlError>"; } else { echo "<success>prizes added</success>"; } } echo '</dataxml>'; exit; ?> I nedd Shark insertedinto name, 0 into odds, and sharkprize into prize, this code inserts S, S, 0, 1, 1, 1 and s, s, 1 Quote Link to comment https://forums.phpfreaks.com/topic/170429-solved-need-help-fixing-this-code/ Share on other sites More sharing options...
ignace Posted August 15, 2009 Share Posted August 15, 2009 foreach($data as $key=>$v) { $query = 'INSERT INTO gameprizes (name, prize, odds, gameID) VALUES (\'%s\', \'%s\', %s, %s)'; $fquery = sprintf($query, $v['Symbol'], $v['Prize'], $v['Odds'], $gameID); $insert = mysql_query($fquery); if (!$insert) { //die("There's little problem: ".mysql_error()); $MyError = "An error occured while saving data. Please try again later." + mysql_error(); echo "<sqlError>".$MyError."</sqlError>"; } else { echo "<success>prizes added</success>"; } } I do not suggest just printing the xml like: echo '<success>..'; use dom instead: http://be.php.net/book.dom Quote Link to comment https://forums.phpfreaks.com/topic/170429-solved-need-help-fixing-this-code/#findComment-899060 Share on other sites More sharing options...
blue-genie Posted August 15, 2009 Author Share Posted August 15, 2009 hey ignace, thanks for getting back to me. I'm going to need more help then that, I'm clueless. where does that code snippet go in? my problem at the moment is that i'm putting bits and pieces together and stuffing it up. I'm a flash developer, and this is beyond my comfort zone, big time! if i stick that code in where I'm assuming it goes, i end up with the number 2 and my gameID being inserted into the db for each column. I'd really appreciate if you could dig me out of this massive hole i've created. Quote Link to comment https://forums.phpfreaks.com/topic/170429-solved-need-help-fixing-this-code/#findComment-899070 Share on other sites More sharing options...
ignace Posted August 16, 2009 Share Posted August 16, 2009 if i stick that code in where I'm assuming it goes, i end up with the number 2 and my gameID being inserted into the db for each column. $query = 'INSERT INTO gameprizes (name, prize, odds, gameID) VALUES (\'%s\', \'%s\', %s, NULL)'; $fquery = sprintf($query, $v['Symbol'], $v['Prize'], $v['Odds']); Here I assume that gameID is a primary key defined as: NOT NULL with the option AUTO_INCREMENT I'd really appreciate if you could dig me out of this massive hole i've created. A shovel and rope comes with the package Quote Link to comment https://forums.phpfreaks.com/topic/170429-solved-need-help-fixing-this-code/#findComment-899335 Share on other sites More sharing options...
blue-genie Posted August 16, 2009 Author Share Posted August 16, 2009 no the gameID is another variable that is parsed from the flash which i'm currently just hardcoding to test it. there is a prizeID that is not null auto increment. Quote Link to comment https://forums.phpfreaks.com/topic/170429-solved-need-help-fixing-this-code/#findComment-899349 Share on other sites More sharing options...
blue-genie Posted August 16, 2009 Author Share Posted August 16, 2009 this is the closest i've got so far and it's inserting each time in the correct column but in different rows. //let's exclude the gameID for now as that works. so instead of inserting into (name, prize, odds) values(shark, sharkprize, 2); its creating 3 rows first: shark , 0, 0 second: 0, sharkprize, 0 third:0, 0, 2 <?php //session_start(); $debugMe=FALSE; include 'config.php'; include 'opendb.php'; //$gameID = $_REQUEST['gameID']; $gameID = 3; //this will set $arr to something like "Symbol=Shark,Odds=1,Prize=sharkprize" //$arr = $_REQUEST['arr']; $arr = "Symbol=Shark,Odds=1,Prize=sharkprize"; if( $debugMe ) { echo "<br><br>'arr' before explode"; var_dump($arr); } //break the string at the commas, giving you an array of three elements //after explode //$data[0]="Symbol=Shark" //$data[1]="Odds=1" //$data[2]="Prize=sharkprize" $data = explode(",", $arr); if( $debugMe ) { echo "<br><br>'data' after 'arr' explode"; var_dump($data); } //now iterate over each of the elements foreach($data as $k=>$v) { if( $debugMe ) { echo "<br>value of 'v' in foreach "; var_dump($v); } //break the value of each array at the "=" symbol. //If you have never used the "list()" construct, refer to: //http://www.php.net/manual/en/function.list.php list($key,$value)=explode("=",$v); //delete the "old" $data[$k] unset($data[$k]); //now append the $key and $value to $data[$k] $data[$k][$key]=mysql_real_escape_string($value); } if( $debugMe ) { echo "<br><br>'data' after foreach"; var_dump($data); exit; } echo '<?xml version="1.0"?>'; echo '<dataxml>'; if ( is_array( $data ) ) { foreach($data as $key=>$v) { echo '<forLoop>insidefor loop'.$data[$key]['Symbol'].'</forLoop>'; echo '<forLoop>insidefor loop'.$data[$key]['Prize'].'</forLoop>'; echo '<forLoop>insidefor loop'.$data[$key]['Odds'].'</forLoop>'; $insert = mysql_query("INSERT INTO gameprizes(name, prize, odds, gameID) VALUES('{$data[$key]['Symbol']}','{$data[$key]['Prize']}','{$data[$key]['Odds']}','{$gameID}')"); } if (!$insert) { echo "<sqlError>An error occured while saving data. Please try again later.". mysql_error() ."</sqlError>"; } else { echo "<success>prizes added</success>"; } } echo '</dataxml>'; exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/170429-solved-need-help-fixing-this-code/#findComment-899387 Share on other sites More sharing options...
blue-genie Posted August 16, 2009 Author Share Posted August 16, 2009 I managed to get assistance from another forum. If you're encountering the same problem PM me for the result. Quote Link to comment https://forums.phpfreaks.com/topic/170429-solved-need-help-fixing-this-code/#findComment-899520 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.