douzhanbai9526 2017-01-04 11:11
浏览 30
已采纳

PDO查询在1个变量中有2个参数

I have problem with inserting into database when i want to save 2 variables in 1 row.

Here i got form where i want when i put name, and select all other things to save me a items i choose and enter in specified color to save in database in that color.

<form action="" method="post">
<fieldset>
    <legend> Add user </legend>
    <p><label class="field" for="user">User :</label> <input type="text" name="user" id="user"></label></p>
    <p><label class="field" for="roll">Points :</label> 
    <select name="roll_chance" id="roll">
        <option value=""></option>
        <option value="*">*</option>
        <option value="**">**</option>
        <option value="***">***</option>
    </select>
    </p>
    <p><label class="field" for="class">Class :</label>
    <select name="class" id="class">
        <option value=""></option>
        <option value="Assasin">Assasin</option>
        <option value="Bard">Bard</option>
        <option value="Mage">Mage</option>
        <option value="Marksman">Marksman</option>
        <option value="Protector">Protector</option>
        <option value="Priest">Priest</option>
        <option value="Reaper">Reaper</option>
        <option value="Tormentor">Tormentor</option>
        <option value="Vampire">Vampire</option>
        <option value="Warrior">Warrior</option>
    </select>
    </p>
    <p><label class="field" for="color">Color :</label>
    <select name="color" id="color">
        <option value=""></option>
        <option value="yellow">Yellow</option>
        <option value="red">Red</option>
        <option value="blue">Blue</option>
    </select>
    </p>
    <p><input type="submit" name="submit" value="Save"></p>
</fieldset>
</form>


if (isset($_POST['submit'])) 
{
    if (isset($_POST['user']) && isset($_POST['roll_chance']) && isset($_POST['class']) && isset($_POST['color'])) {
        $user = htmlentities($_POST['user']);
        $roll = htmlentities($_POST['roll_chance']);
        $class = htmlentities($_POST['class']);
        $color = htmlentities($_POST['color']);

        $user = strip_tags($user);
        $roll = strip_tags($roll);
        $class = strip_tags($class);
        $color = strip_tags($color);

        $stmt = $dbh->prepare ("INSERT INTO users (user, roll_chance, class) VALUES (:user, :roll, :class)");
        $stmt->bindParam(':user', '<span style"color:'.$color.';">'.$user.'</span>');
        $stmt->bindParam(':roll', $roll);
        $stmt->bindParam(':class', '<span style"color:'.$color.';">'.$class.'</span>');
        $stmt->execute();
    }   
}
  • 写回答

1条回答 默认 最新

  • dsnhalq37505 2017-01-04 11:17
    关注

    I advise to split data and layout and create a column named color in your table. And change query like:

    $stmt = $dbh->prepare ("INSERT INTO users (user, roll_chance, class, color) VALUES (:user, :roll, :class, :color)");
    $stmt->bindParam(':user', $user);
    $stmt->bindParam(':roll', $roll);
    $stmt->bindParam(':class', $class);
    // insert color
    $stmt->bindParam(':color', $color);
    $stmt->execute();
    

    And use this color value later in your output. This will make your life easier if later you'll need to change layout from span to div or other markup.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器