你好我和女孩我拥有一个RPG(在线角色扮演游戏) 用户可以捕捉怪物并训练他们的东西。但是现在我来建立一个交易脚本......
我在1页上显示所有怪物和其他用户怪物,但是当他们选择他们想要的东西以及为怪物提供的东西时,它不会将信息添加到数据库......
所以我有一个页面,他们必须输入他们想要交易的用户名。
<form name="input" action="tradedo.php" method="get">
Username: <input type="text" name="username_trade" />
<input type="submit" value="Submit" />
</form>
然后在tradedo.php上显示怪物,其他用户怪物被抓住,他们可以选择他们想要的东西以及为他们提供的东西......
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> </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> </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>";
这很有效....它显示了所有的怪物,一切都很棒,但现在我需要插入它们,这里是插入页面
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 )
{ $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());
echo"Done";
在我的db中获取
trade_id = 1977949793(works)
trade_to =
trade_from = admin (works)
trade_pokeid = Array ( no monster ids ???)
trade_mypokeid = Array( no monster ids ???)
为脚本使用插入怪物ID有1,4,5,7然后id抓住它们并在交易接受页面上显示它们..但不是它只是添加数组甚至没有抓住trade_to我已经试过解释我能做的最好的事情。
我已经编辑了数据库连接.....
答案 0 :(得分:1)
在INSERT语句中:
... '".$dbid['pokemon']."'
这是一个连接到字符串的数组,不起作用。
您可以序列化或循环并创建一些表示,但最好重新设计数据库方案,这样就不会在一个字段中存储多个值...
答案 1 :(得分:1)
$ dbid ['pokemon']和$ dbid2 ['pokemonin']是数组,因此当你连接它们时,它们会变成一个值为“Array”的字符串。
不考虑安全性或性能,这应该有效:
session_start();
mysql_connect("localhost", "blahhhhhhhhhhh", "");
mysql_select_db("");
$pokemon = $_POST['pokemon'];
$pokemonin = $_POST['pokemonin'];
$meid = $_SESSION['username'];
$toid = $_POST['user'];
foreach ( $pokemon as $poke )
{
foreach ( $pokemonin as $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."', '".$poke."', '".$poke2."');") or die("Error: ". mysql_error());
}
}
echo "Done";