创建数组后,使用逗号将数组添加到mysql varchar行中

时间:2014-08-25 13:38:23

标签: php mysql sql arrays

我有一张带有一些物品的大桌子。我想根据玩家的游戏级别从该表中检索特定项目。代码提取了我想要将它们添加到数组中的所有特定项目(在本例中为" $cart")。我想将一个mysql行更新为varchar字段,数组用逗号分隔",";

我尝试了将它们添加到数据库中的不同方法,但有些东西不起作用。

继承我的代码:

$sql = mysql_query("select * from iteme where tip != 'Minereu' AND tip != 'Reteta' AND   tip != 'Cooking' AND tip != 'Altele' AND tip != 'Potiuni' AND tip != 'Fragment' AND tip != 'Tools' AND tip != 'Medicina' ");

$numaratoare = 0;
$cart = array();
$nivel_player = mysql_query("select * from membri where id !=0 ");

while($informatie_player = mysql_fetch_array($nivel_player))
{
    $nivel_actual_player = lvl($informatie_player['experienta']);
    $shop_arme_plus_5 = $nivel_actual_player + 5 ;
    if ($nivel_actual_player - 5 == 0 )
        $shop_arme_minus_5 = $nivel_actual_player - 5 ;
    else
        $shop_arme_minus_5 = 0 ;


    $shop_arme = mysql_query("select * from iteme where tip = 'Arme' AND level_minim < ".$shop_arme_plus_5." AND level_minim > ".$shop_arme_minus_5." AND vandabil = 1 ");

    while($informatie = mysql_fetch_array($shop_arme)) {
        $numaratoare ++;
        //echo " ".$informatie['nume_ro']." , ";
        array_push($cart, $informatie['obiect']);
    }
    mysql_query("INSERT INTO membri_shop_iteme set iteme_arme = ".$informatie["obiect"]." where id_jucator = ".$informatie_player['id']." "); // Here is the problem.

}

问题出在哪里?我尝试添加implode(', ', $cart);,但仍然无法将其添加到数据库中。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

可能是它的字符串问题$ informatie [&#34; obiect&#34;] to $ informatie [&#39; obiect&#39;]

  mysql_query("INSERT INTO membri_shop_iteme set iteme_arme = ".$informatie['obiect']." where id_jucator = ".$informatie_player['id']." "); // Here is the problem.

  $newObject=join(",",$cart);
  mysql_query("UPDATE membri_shop_iteme SET iteme_arme = '".$newObject."' where id_jucator = ".$informatie_player['id']." "); // Here is the problem.

答案 1 :(得分:0)

您在查询中使用$informatie["obiect"],但必须使用$cart

while($informatie = mysql_fetch_array($shop_arme)) {
    $numaratoare ++;
    array_push($cart, $informatie['obiect']);
}

mysql_query("UPDATE membri_shop_iteme
             SET iteme_arme = '".implode("','", $cart)."'
             WHERE id_jucator = '".$informatie_player['id']."'");