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 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里