如何使用php读取/发送帖子数据并在其中保存变量

时间:2012-08-05 20:24:41

标签: php sql

我在代码的循环中有这个代码,循环为找到的每个成员创建一个提交按钮。我需要每个按钮都有成员名称存储在其中,以便在单击该按钮时可以通过post发送。我不确定这是否可以发布,但我正在尝试用URLS做的方式。有谁知道怎么做?

<input type="submit" value="Attack" name="Attack?name=<?php echo $Member_name; ?>" />

<?php 
if(isset($_POST['Attack'])){
$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_GET['name'])."'";
$query = mysql_query($sql)  or die(mysql_error());
$row = mysql_fetch_object($query);
}

这是我试图以隐藏的形式存储它的整个代码,但它只抓取找到的最后一个成员而不会得到其他成员。

<?php

$sql = "SELECT name, rank FROM users ORDER BY rank DESC"; //  Searches the database for every one who has being last active in the last 5 minute
$query = mysql_query($sql) or die(mysql_error());
$count = mysql_num_rows($query);
$i = 1;
while($row = mysql_fetch_object($query)) {
 $Member_name = htmlspecialchars($row->name);
$Member_level = htmlspecialchars($row->rank);
?>
<td><?php echo $i; ?></td>
<td><a href="View_Profile.php?name=<?php echo $Member_name; ?>"><?php echo $Member_name; ?></a></td><td><?php echo $Member_level; ?></td><td>
<input type="hidden" name="thename" value="<?php echo $Member_name; ?>">
<input type="submit" value="Attack" name="Attack" />
</td>
<?

 if($i != $count) { // this counts the amount of people that are online and display the results.
  echo "</tr><tr>";
 }
 $i++;
}
?>

<?php 
if(isset($_POST['Attack'])){
$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_POST['thename'])."'";
$query = mysql_query($sql)  or die(mysql_error());
$row = mysql_fetch_object($query);
$profile_id = htmlspecialchars($row->id);
$profile_userip = htmlspecialchars($row->userip);
$profile_name = htmlspecialchars($row->name);
$profile_money = htmlspecialchars($row->money);
$profile_gang = htmlspecialchars($row->gang);
$profile_exp = htmlspecialchars($row->exp);
$profile_profile = htmlspecialchars($row->profile);
$profile_rank = htmlspecialchars($row->rank);
$profile_health = htmlspecialchars($row->health);
$profile_defence = htmlspecialchars($row->defence);
$profile_stanima = htmlspecialchars($row->stanima);
?>

1 个答案:

答案 0 :(得分:2)

好的,假设其他一切正常,你正在检索数据。

改变这个:

<input type="hidden" name="thename" value="<?php echo $Member_name; ?>">
<input type="submit" value="Attack" name="Attack" />

对此:

<form method="POST" action="">
<input type="hidden" name="name" value="<?php echo $Member_name; ?>">
<input type="submit" value="Attack" name="Attack" />
</form>

并且在PHP中,更改此行:

$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_GET['name'])."'";

要:

$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_POST ['name'])."'";

这不是执行此操作的最佳方式, 根据您拥有的用户数量,您将生成大量HTML元素,但它应该可以解决您的问题(提供其他一切工作和接收数据。)

HTML 5&amp; Javascript对此非常完美,是您应该关注的内容。