dongre6073 2016-09-18 14:31
浏览 53

使用在单独函数中创建的字符串插入表

I'm trying to insert using a string that is created in a separate function but it does not appear to be working.

I know I should be using a switch statement or something for this but it is purely for testing at the moment.

<?php
function newItem($link){
    $itemname = mysqli_real_escape_string($link, $_POST['itemname']);
    if (strpos($itemname,'<') || strpos($itemname,'>') || strpos($itemname,'?') || strpos($itemname,'/') || strpos($itemname,'=')) 
    {
        echo "<script type='text/javascript'>alert('yeah....no');</script>";
    } else { 
        $sql = "SELECT * FROM items WHERE itemname = '$itemname'";
        $result = mysqli_query($link,$sql);
        if(mysqli_num_rows($result) !== 1){
            $sql = "INSERT INTO items(itemname,itemtype,attack,defence,energy)VALUES('$itemname'".RandomStats($link).")";
            mysqli_query($link,$sql);
            header("location: index.php");
        }else{
            echo "<script type='text/javascript'>alert('Item name already exists');</script>";
        }
    } 
}
function RandomStats($link){
    $rand = rand ( 1 , 100 );
    $itemtype = rand (1,3);
    $itemtypeString;

    if($itemtype = 1){
        $itemtypeString = 'Armour';
    }
    if($itemtype = 2){
        $itemtypeString = 'Sword';
    }
    if($itemtype = 3){
        $itemtypeString = 'Shield';
    }

    if($rand <= 50){
        //common
        $attack = rand (1 , 50);
        $defence = rand (1 , 50);
        $energy = rand (1 , 50);
        $ItemStats = "','".$itemtypeString."','".$attack."','".$defence."','".$energy."'";
    }
    if ($rand >50 and $rand <= 80){
        //rare
        $attack = rand (1 , 100);
        $defence = rand (1 , 100);
        $energy = rand (1 , 100);
        $ItemStats = "','".$itemtypeString."','".$attack."','".$defence."','".$energy."'";
    }
    if ($rand >80 and $rand <= 98){
        //exotic
        $attack = rand (1 , 150);
        $defence = rand (1 , 150);
        $energy = rand (1 , 150);
        $ItemStats = "','".$itemtypeString."','".$attack."','".$defence."','".$energy."'";
    }
    if ($rand >98 and $rand <= 100){
        //ledgendary
        $attack = rand (1 , 200);
        $defence = rand (1 , 200);
        $energy = rand (1 , 200);
        $ItemStats = "','".$itemtypeString."','".$attack."','".$defence."','".$energy."'";
    }

    return $ItemStats;
}
?>

is it just that the way I'm creating the string I'm making the statement incorrect or is it something else?

  • 写回答

1条回答 默认 最新

  • dongshi4773 2016-09-18 14:43
    关注

    i had an extra ' at the start that was not needed after doing the debugging that u_mulder helped me with

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么