donglun2024 2012-08-02 05:27
浏览 34
已采纳

PHP游戏Sql问题

I need help with this code, i need it to add an array in Gangs under members and seperate them with -

I need every member in the Gang to be listed under Members and separated with - so i can explode them below.

This is the line that adds the members to gangs but its without - and i think it erases members that are already there.

$result = mysql_query("UPDATE Gangs SET members='".$name."'WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'")
or die(mysql_error());

full code

if(isset($_POST['creategang'])){

if(empty($_POST['gang_name'])){ 
echo "Enter a Gang Name.";
} else {

if (strlen($_POST['gang_name']) > "20"){
echo "The username may not consist out of more then 20 characters.";
}else{

if (ereg('[^A-Za-z0-9]', $_POST['gang_name'])) {
echo "Invalid Name only A-Z,a-z and 0-9 is allowed.";
}else{
$sql = "SELECT name FROM Gangs WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'";
$query = mysql_query($sql) or die(mysql_error());
$m_count = mysql_num_rows($query);

if($m_count >= "1"){
echo 'This name has already been used.!';
}else{
$sql = "INSERT INTO Gangs SET name = '".$_POST['gang_name']."' , owner= '$name'";

$res = mysql_query($sql);

$result = mysql_query("UPDATE users SET gang='".mysql_real_escape_string($_POST['gang_name'])."' WHERE id='" .mysql_real_escape_string($_SESSION['user_id']). "'") 
or die(mysql_error());

$result = mysql_query("UPDATE Gangs SET members='".$name."'WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'")
or die(mysql_error());

echo 'Gang successfully created!';
}
}
}
}
}
?>

this is the code i will use to separate the array

$Gang_array = explode("-", $Gang_members);

Thanks for viewing my question and thanks in advance for helping me

new code to add to gang

<? include_once("connect.php"); ?>

<?
if(isset($_SESSION['user_id'])) {
 // Login OK, update last active

 $sql = "UPDATE users SET lastactive=NOW() WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'";
 mysql_query($sql);

}else{
   header("Location: index.php");
     exit();
 }
$sql = "SELECT * FROM users WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$id = htmlspecialchars($row->id);
$userip = htmlspecialchars($row->userip);
$name = htmlspecialchars($row->name);
$sitestate = htmlspecialchars($row->sitestate);
$password = htmlspecialchars($row->password);
$mail = htmlspecialchars($row->mail);
$money = htmlspecialchars($row->money);
$exp = htmlspecialchars($row->exp);
$rank = htmlspecialchars($row->rank);
$health = htmlspecialchars($row->health);
$points = htmlspecialchars($row->points);
$profile = htmlspecialchars($row->profile);
$gang = htmlspecialchars($row->gang);
?>

<?php 
$sql = "SELECT * FROM Gangs WHERE name='".mysql_real_escape_string($_GET['name'])."'";
$query = mysql_query($sql)  or die(mysql_error());
$row = mysql_fetch_object($query);
$Gang_name = htmlspecialchars($row->name);
$Gang_owner = htmlspecialchars($row->owner);
$Gang_money = htmlspecialchars($row->money);
$Gang_exp = htmlspecialchars($row->exp);
$Gang_level = htmlspecialchars($row->level);
$Gang_members = htmlspecialchars($row->members);
$Gang_array = explode("-", $Gang_members);



$Gang_profile = htmlspecialchars($row->profile);

?>
<div id="content" class="profile">
    <h2>Gang Profile</h2>
<form method="post" >
<input type="submit" name="Petition" id="Petition" value="Petition">
</form>
<center>
<h1><?php echo $Gang_name; ?></h1>
Owner: <?php echo $Gang_owner; ?><br>
Gang Cash: $<?php echo $Gang_money; ?><br>
Gang Exp: <?php echo $Gang_exp; ?><br>
Gang Level: <?php echo $Gang_level; ?><br>
Gang Members: <?php echo $Gang_array; ?><br>
</center><br>
<p>Gang Quote</p>
<div id="UserText">
      <?php
    $Gang_profile = htmlentities($Gang_profile);
    $Gang_profile = nl2br($Gang_profile); 
    $Gang_profile = stripslashes($Gang_profile);
    echo $Gang_profile; ?>
</div>
</div>

<? 
if (isset($_POST['Petition'])) {
$result = mysql_query("SELECT members FROM Gangs 
      WHERE name='".$Gang_name."'");
if ($result) {
      while($row = mysql_fetch_assoc($result)) {
           $members = $row['members'];
      }
}
if ($members != '') $members .= '-'.$name;
else $members = $name;
$result = mysql_query("UPDATE Gangs SET members='".$members."' WHERE name='".$Gang_name."'");

}
  • 写回答

2条回答 默认 最新

  • douzhonglong3789 2012-08-02 10:06
    关注

    If I understand your question correctly. You can try this :

    Get the members first :

    $result = mysql_query("SELECT members FROM Gangs 
          WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'");
    if ($result) {
          while($row = mysql_fetch_assoc($result)) {
               $members = $row['members'];
          }
    }
    

    Then add new member and do update :

    //This is to check whether $name is already in the gangs
    if (strpos($members,$name) !== false) {
        if ($members != '') $members .= '-'.$name;
        else $members = $name;
    
        //Update to gangs
        $result = mysql_query("UPDATE Gangs SET members='".$members."' WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'");
    }
    else sprintf("%s is in the gangs already",$name);
    

    Hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM