Jump to content

[SOLVED] need help fixing this code


blue-genie

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

?>

 

Link to comment
Share on other sites

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.