6 Replies - 1467 Views - Last Post: 06 February 2012 - 10:55 AM Rate Topic: -----

#1 rpgmaker   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Mysql insert is not working on a trade script.

Posted 06 February 2012 - 10:13 AM

Hello guys and girls i own a rpg ( a online role playing game ) Were users can catch monsters and train them up and stuff. But now i have come around to building a trade script...

I show all there monsters and the other users monsters on the 1 page but when they pick what they want and what there offering for the monsters it will not add the info into the db...

So i have a page were they have to enter the users username they want to trade with.

   <form name="input" action="tradedo.php" method="get">
Username: <input type="text" name="username_trade" />
<input type="submit" value="Submit" />
</form> 


Then on tradedo.php there monsters are shown and the others users monsters are grab and they can pick what they want and what there offering for them...
 session_start();

mysql_connect("localhost", "blah", "");
mysql_select_db("");



$username_trade = $_POST['username_trade'];



$_SESSION['username_trade'] = $username_trade ;

echo "You put in id  ". $username_trade . ".<br />";



?>
</p>
<p>&nbsp;</p>
<p><span class="mid_box">
  <?php
                        // get and display userbox
                        $q = "SELECT id,pokemon,exp,level FROM user_pokemon WHERE belongsto='". $_SESSION['username_trade']."'";
                        $r = mysql_query($q);

                        if (mysql_num_rows($r) <= 0) {
                            echo "You have no current pokemon stored";

                        }
                    ?>

</span></p>
<p>&nbsp;</p>

<p>
  <?php
 echo "<form action='tradestore.php' method='POST'>";
while ( $v = mysql_fetch_object( $r ) )
{
    echo "<label><input type='checkbox' name='pokemon[]' value='$v->dbid'/> They have a $v->pokemon    </label><br/>";
        echo "<label> Level $v->level     </label><br/>";
}
echo "<input type='hidden' name='user' value='$username_trade'/>";
echo "<input type='submit' value='Check!!'/>";
?>
</p>
<p><strong>Pick what you want two offer for the pokemon </strong></p>
<p>
  <?php
                        // get and display userbox
                        $q = "SELECT id,pokemon,exp,level FROM user_pokemon WHERE belongsto='". $_SESSION['username']."'";
                        $t = mysql_query($q);

                        if (mysql_num_rows($t) <= 0) {
                            echo "You have no current pokemon stored";

                        }
                    ?>
</p>
<p>


  <?php
 echo "<form action='test_process.php' method='POST'>";
while ( $v = mysql_fetch_object( $t ) )
{
    echo "<label><input type='checkbox' name='pokemonin[]' value='$v->dbid'/> I have a $v->pokemon</label><br/>";
    echo "<label> Level $v->level     </label><br/>";
}
echo "<input type='hidden' name='userin' value='$username'/>";
echo "</form>";


It works great .... It shows all the monsters and everything is great but now i need to insert them and here is the insert page


$pokemon = $_POST['pokemon'];
$pokemonin = $_POST['pokemonin'];
$meid = $_SESSION['username'];
$toid = $_POST['user'];
$dbid = array();
$dbid2 = array();

foreach ( $pokemon as $poke )
{   $dbid['pokemon'][] = $poke;

}

foreach ( $pokemonin as $poke2 )
{   $dbid2['pokemonin'][] = $poke2;


}




srand ((double) microtime( )*1000000);
$random_number = rand( );
echo "$random_number";


    mysql_query("INSERT INTO trade (trade_id, trade_to, trade_from, trade_pokeid, trade_mypokeid)
                            VALUES ('$random_number','".$toid."', '".$meid."', '".$dbid['pokemon']."', '".$dbid2['pokemonin']."');") or die("Error: ". mysql_error());






In my db im getting

trade_id = 1977949793(works)
trade_to = 
trade_from = admin  (works)
trade_pokeid = Array ( no monster ids ???)
trade_mypokeid = Array( no monster ids ???)


Be for the script use to insert the monster id has 1,4,5,7 then id grab them and show them on a trade accept page.. but not its just adding Array and not even grabing the trade_to i have tried to explain the best i can.

And the db connect i have edited out .....

I have also tried this for the update page
session_start();

mysql_connect("localhost", "blahhhhhhhhhhh", "");
mysql_select_db("");

$pokemon = $_POST['pokemon'];
$pokemonin = $_POST['pokemonin'];
$meid = $_SESSION['username'];
$toid = $_POST['user'];
$dbid = array();
$dbid2 = array();

foreach ( $pokemon as $poke )
{
    $pokemoninArray = each($pokemonin);
    $pokemonin = $pokemoninArray[0];

    srand ((double) microtime( )*1000000);
    $random_number = rand( );
    echo "$random_number";

    mysql_query("INSERT INTO trade (trade_id, trade_to, trade_from, trade_pokeid, trade_mypokeid)
VALUES ('$random_number','".$toid."', '".$meid."', '".$poke."', '".$pokemonin."');") or die("Error: ". mysql_error());

}

echo"Done";





But still the insert does not work..

This post has been edited by rpgmaker: 06 February 2012 - 10:19 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Mysql insert is not working on a trade script.

#2 Jstall   User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Mysql insert is not working on a trade script.

Posted 06 February 2012 - 10:33 AM

Hello,

$dbid['pokemon']



Is an array that is populated in your foreach loop here:
foreach ( $pokemon as $poke )
{  
   // here you are adding elements to your $dbid['pokemon'] array 
   $dbid['pokemon'][] = $poke;
}



You do the same thing with $dbid2['pokemonin'], that is why you are seeing Array in your database.
Was This Post Helpful? 0
  • +
  • -

#3 rpgmaker   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: Mysql insert is not working on a trade script.

Posted 06 February 2012 - 10:38 AM

I do that anyway's
$pokemon = $_POST['pokemon'];
$pokemonin = $_POST['pokemonin'];
$meid = $_SESSION['username'];
$toid = $_POST['user'];
$dbid = array();
$dbid2 = array();

foreach ( $pokemon as $poke )
{   $dbid['pokemon'][] = $poke;

}

foreach ( $pokemonin as $poke2 )
{   $dbid2['pokemonin'][] = $poke2;


}




srand ((double) microtime( )*1000000);
$random_number = rand( );
echo "$random_number";


    mysql_query("INSERT INTO trade (trade_id, trade_to, trade_from, trade_pokeid, trade_mypokeid)
                            VALUES ('$random_number','".$toid."', '".$meid."', '".$dbid['pokemon']."', '".$dbid2['pokemonin']."');") or die("Error: ". mysql_error());




But still does not work... im getting 0 has the result...

This post has been edited by rpgmaker: 06 February 2012 - 10:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: Mysql insert is not working on a trade script.

Posted 06 February 2012 - 10:41 AM

View Postrpgmaker, on 06 February 2012 - 06:13 PM, said:

 session_start();

mysql_connect("localhost", "blah", "");
mysql_select_db("");



$username_trade = $_POST['username_trade'];



$_SESSION['username_trade'] = $username_trade ;

echo "You put in id  ". $username_trade . ".<br />";



?>
</p>
<p>&nbsp;</p>
<p><span class="mid_box">
  <?php
                        // get and display userbox
                        $q = "SELECT id,pokemon,exp,level FROM user_pokemon WHERE belongsto='". $_SESSION['username_trade']."'";
                        $r = mysql_query($q);

                        if (mysql_num_rows($r) <= 0) {
                            echo "You have no current pokemon stored";

                        }
                    ?>

</span></p>
<p>&nbsp;</p>



It works great .... It shows all the monsters and everything is great but now i need to insert them and here is the insert page

really? that should give you a "cannot send session cookie" warning (unless you have error display disabled) and thus not start the session.


View Postrpgmaker, on 06 February 2012 - 06:13 PM, said:

In my db im getting

trade_id = 1977949793(works)
trade_to = 
trade_from = admin  (works)
trade_pokeid = Array ( no monster ids ???)
trade_mypokeid = Array( no monster ids ???)


Be for the script use to insert the monster id has 1,4,5,7 then id grab them and show them on a trade accept page.. but not its just adding Array and not even grabing the trade_to i have tried to explain the best i can.

that (regarding the last 2 lines) is expected behaviour. you pass in an array and convert it to a string. and every array in PHP is converted to the string "Array". (simply try echo array();). for the empty trade_to check what you have in $_POST.
Was This Post Helpful? 0
  • +
  • -

#5 Jstall   User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Mysql insert is not working on a trade script.

Posted 06 February 2012 - 10:42 AM

Yes I understand that is what you are doing, but I'm saying there is a bit of a flaw in your logic somewhere.

In your query you are trying to insert $dbid['pokemon'] as a value. But in this context $dbid['pokemon'] is an array. You are not trying to insert values stored in the array you are using the array itself. You would want to use the values that are stored in the array would you not?
Was This Post Helpful? 0
  • +
  • -

#6 rpgmaker   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: Mysql insert is not working on a trade script.

Posted 06 February 2012 - 10:45 AM

Ok well what is the best way to do it ? If i sat down and re did the whole script ?
I guess the select page is ok ? Because its doing what it ment to be doing ?
And i guess instead of storing all the monsters in one column it would be better to have a row for each monster ?
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: Mysql insert is not working on a trade script.

Posted 06 February 2012 - 10:55 AM

View Postrpgmaker, on 06 February 2012 - 06:45 PM, said:

And i guess instead of storing all the monsters in one column it would be better to have a row for each monster ?

nope, you donít do that either. you rather make (distinct) entries for each monster that was traded.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1