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.

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

报告相同问题?

悬赏问题

  • ¥50 R语言读取nc按月均值转为tif
  • ¥30 智能车串级pid调参
  • ¥15 visual studio code翻译老是错误
  • ¥20 卫星测高数据的高程转换
  • ¥15 爬取招聘网站数据信息
  • ¥15 安装完tensorflow,import tensorflow as tf后报错,如何解决?
  • ¥15 ultralytics库导出onnx模型,模型失去预测能力
  • ¥15 linux下点对点协议连接2个USB串口的硬件流量控制问题
  • ¥15 SQL数据自动生成问题
  • ¥15 设密码后无法访问他人的共享文件