PHP MYSQL多个如果多个更新的语句

时间:2016-01-07 21:55:20

标签: php mysql

有人能告诉我这些IF语句有什么问题吗?

if(isset($_POST['submit']))
{
    $dropship = $unitid['id'];
    $jumpship = $_POST['jumpship'];
    $dsdest = $_POST['planet'];
    $dslz = $_POST['landingzone'];
    $dsmission = $_POST['mission'];
    $ds1 = mysql_query("SELECT id, ds1 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");
    $ds2 = mysql_query("SELECT id, ds2 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");
    $ds3 = mysql_query("SELECT id, ds3 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");

    $dist_loc_get2 = mysql_query("SELECT * FROM gc3025_dist_game WHERE `planet`='$dsdest' AND `districtid`='$dslz'");
    $distloc2 = mysql_fetch_assoc($dist_loc_get2);
    $newdist = $distloc2['g_district'];
    $ds_name_get = mysql_query("SELECT * FROM gc3025_game_dropships WHERE `id`='$dropship'");
    $ds_name = mysql_fetch_assoc($ds_name_get);
    $dsname = $ds_name['unit_name'];

    $dest_name_get = mysql_query("SELECT gc3025_planets_game.Game, gc3025_planets_game.owners, gc3025_planets_game.g_planet, gc3025_planets_game.Planet_id, gc3025_planets_id.planet_name  FROM gc3025_planets_id JOIN gc3025_planets_game ON gc3025_planets_id.id = gc3025_planets_game.Planet_id WHERE `g_planet`='$dsdest'");
    $dest_name = mysql_fetch_assoc($dest_name_get);
    $destname = $dest_name['planet_name'];

    $dsdz_name_get = mysql_query("Select gc3025_dist_game.districtid, gc3025_dist_game.g_district, gc3025_dist_labels.id, gc3025_dist_labels.dist_name FROM gc3025_dist_game JOIN gc3025_dist_labels ON gc3025_dist_game.districtid = gc3025_dist_labels.id WHERE `g_district`='$newdist'");
    $dsdz_name = mysql_fetch_assoc($dsdz_name_get);
    $dsdzname = $dsdz_name['dist_name'];

    $dsmission_name_get = mysql_query("SELECT * FROM gc3025_movement_dropdowns WHERE `id`='$dsmission'");
    $dsmission_name = mysql_fetch_assoc($dsmission_name_get);
    $dsmissionname = $dsmission_name['mission_type'];

    if ($ds1 == 0){
    mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds1` = '$dsname', `ds1dest` = '$destname', `ds1dz` = '$dsdzname', `ds1mission` = '$dsmissionname' WHERE `id`='$jumpship'");
    }
    if ($ds1 == 1){  
    mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds2` = '$dsname', `ds2dest` = '$destname', `ds2dz` = '$dsdzname', `ds2mission` = '$dsmissionname' WHERE `id`='$jumpship'"); 
    } 
    if ($ds2 == 1){
        mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds3` = '$dsname', `ds3dest` = '$destname', `ds3dz` = '$dsdzname', `ds3mission` = '$dsmissionname' WHERE `id`='$jumpship'");
        }
    if ($ds3 == 1){
        echo "This Jumpship is Full!";
      }

    echo "<p>$dsname Loaded on $jumpship going to $destname and to complete $dsmissionname In District $dsdzname!</p>";

希望这已经足够了。

基本上,这张桌子适用于载有3个飞船的跳伞。我基本上需要if语句如果ds1中有一个dropship,那么dropship将进入ds2列。如果ds1和ds2中有一个dropship,那么它将更新ds3列。如果所有三个人都有飞船,那么回声&#34;这个跳跃是满的&#34;将发布。

1 个答案:

答案 0 :(得分:0)

函数mysql_query返回resource,您必须使用mysql_fetch_assoc进行数据提取。此外,查询中的数据应为properly escaped

$ds_query = mysql_query(sprintf(
  "SELECT ds1, ds2, ds3 FROM gc3025_game_jumpships WHERE `id`='%s'",
  mysql_real_escape_string($jumpship)
));

$ds_result = mysql_fetch_assoc($ds_query);

if (!$ds_result['ds1'])
{
  mysql_query(sprintf(
    "UPDATE `gc3025_game_jumpships` " .
    "SET `ds1` = '%s', `ds1dest` = '%s', `ds1dz` = '%s', `ds1mission` = '%s' " .
    "WHERE `id`='%s'",
    mysql_real_escape_string($dsname),
    mysql_real_escape_string($destname),
    mysql_real_escape_string($dsdzname),
    mysql_real_escape_string($dsmissionname),
    mysql_real_escape_string($jumpship)
  ));
}
elseif ($ds_result['ds1'])
{
  // and so on  
}
elseif ($ds_result['ds2'])
{
  // and so on
}