1

我需要有关此代码的帮助,我需要它在成员下的 Gangs 中添加一个数组并将它们分开 -

我需要将帮派中的每个成员都列在“成员”下并与之分开——这样我就可以在下面分解它们。

这是将成员添加到帮派但没有加入的行 - 我认为它会删除已经存在的成员。

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

完整代码

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!';
}
}
}
}
}
?>

这是我将用来分隔数组的代码

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

感谢您查看我的问题,并提前感谢您帮助我

添加到帮派的新代码

<? 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."'");

}
4

2 回答 2

1

伪代码,但这应该可以工作:

$result = mysql_query("UPDATE Gangs SET members = members + '-' + '".$name."'WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'")
or die(mysql_error());
于 2012-08-02T07:51:22.687 回答
1

如果我正确理解你的问题。你可以试试这个:

首先获取成员:

$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'];
      }
}

然后添加新成员并更新:

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

希望能帮助到你。

于 2012-08-02T10:06:27.393 回答