始终从行获取最后一个值

时间:2015-05-09 21:54:01

标签: php mysql

我试图在#34;和#34;中尝试从php文件到另一个变量,但是我的问题是:

我有一个商店页面,我得到了一个' id'根据你点击的按钮,然后按钮会把你带到另一个页面(buy.php),我需要这个项目ID来从db获取正确的项目(对不起我的sh **解释)。

项目php:

$conn = new mysqli($web_host, $web_user, $web_pw, $web_tbl);
    $sql = "SELECT id, vnum, name, imagine, pret, descriere FROM itemshop_items";
    $result = $conn->query($sql);
    $user_name = mysql_real_escape_string($_SESSION['user_name']);
    if ($result->num_rows > 0){
    while($row = $result->fetch_assoc()){
        $id = $row['id'];
        echo "<tr><div class='is_item'";
        echo "<td><font color='red'>". $row['name'] ."</font></p>";
        echo "<div class='border_img_is'><img src='img/item/". $row['imagine'] ."' width='32px' height='64px' alt='espada'></img></div></p>";
        echo "<div class='preco'>Preço: " . $row['pret'] . "MD's</div>";
        echo "<textarea readonly type='text' id='texto' cols='40' rows='5' name='texto' >".$row['descriere']."</textarea>";
        echo "<a class='comprar_btn' href='index.php?s=buy&id=".$id."'>comprar</a>";
        echo "</tr></div>";
        $preco = $row['pret'];
        $vnum = $row['vnum'];
    }

buy.php:

$sql= mysql_query("SELECT vnum, pret, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, socket0, socket1, socket2 FROM player.itemshop_items WHERE id='$id'") or die(mysql_error());
    if(mysql_num_rows($sql)==1) {
    $conn = mysql_connect($db_host, $db_username, $db_pass);
    mysql_select_db("$db_name") or die("no database by that name");
    $mdsquery = mysql_query("SELECT coins FROM account.account WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."' LIMIT 1");
    $mds = mysql_fetch_object($mdsquery);
    //verificar se user tem mds suficientes, dar return das coins em vez de obj
    if($mds->coins>=$preco){
        //remover coins do user como preco do item
        $nCoins = $mds->coins-$preco;
        $tirar = mysql_query("UPDATE account.account SET coins='".mysql_real_escape_string($nCoins)."' WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."' LIMIT 1");
        //coins removidas, vamos inserir item na conta
        //inserir o item na conta em MALL
        $inserir = mysql_query("INSERT INTO player.item_award (pid,login,vnum,count, given_time, item_id, why, mall) VALUES
         ('".mysql_real_escape_string($_SESSION['user_id'])."','".mysql_real_escape_string($_SESSION['user_name'])."','".$vnum."','1','0000-00-00 00:00:00', '".$vnum."', 'idk', '1')");
        //get local time
        $datetime = $_POST['date'] . ' ' . $_POST['time'];
        $datetime = mysql_real_escape_string($datetime);
        $datetime = strtotime($datetime);
        $datetime = date('Y-m-d H:i:s',$datetime);
        //inserir dados da compra nos logs
        $inserir_logs =  mysql_query("INSERT INTO player.itemshop_logs (account,item,id_item,data) VALUES
         ('".mysql_real_escape_string($_SESSION['user_id'])."','".$vnum."','".$id."','".$datetime."')");
        echo "<div class='top_text'><center><font color='green'>Compraste o item com sucesso! </p></font></center></div>";
    }else{
        echo "<div class='top_text'><center><font color='red'>Não tens moedas suficientes!</font></center></div>";
    }

继承我的项目表:

enter image description here

结果talbe: enter image description here

你可以看到&#34; vnum&#34;在这个桌子上应该是19或189取决于按钮,但它总是返回39! (项目的最后一个)

1 个答案:

答案 0 :(得分:1)

您在item.php文件中多次声明vnum,并始终保留最后一个39

这是我正在讨论的查询

SELECT id, vnum, name, imagine, pret, descriere FROM itemshop_items