使用复选框php将数据插入mysql数据库

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (75)

在这里有点恼怒,尝试了一堆东西,没有工作。我想为我的Magic the Gathering粉丝最终项目制作一个表单,您可以在其中选中复选框以使用字母或根本不填充数据库中的列。

<!Doctype html>
<html>

<head>
</head>

<body>
    <?php

    if (!empty($_POST)) {
        //connect to the database
        $mysqli = new mysqli('localhost', 'root', '', 'magic');
        //check the connection
        if ($mysqli->connect_error) {
            die('Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error);
        }
        //insert the data
        $sql = "INSERT INTO decks ( DeckName, Author, CardList ) VALUES ( '{$mysqli->real_escape_string($_POST['DeckName'])}', '{$mysqli->real_escape_string($_POST['Author'])}', '{$mysqli->real_escape_string($_POST['CardList'])}' )";
        $insert = $mysqli->query($sql);
        //insert the checkbox data
        if (isset($_POST['Red'])) {
            $sql = "INSERT INTO decks ( Red ) VALUES ( '{$mysqli->real_escape_string('R')}' ) ";
        } else {
            $sql = "INSERT INTO decks ( Red ) VALUES (' ')";
        }

        //response from the database
        if ($insert) {
            echo "Success!";
        } else {
            die("Error, please try again!");
        }

        //kill the connection
        $mysqli->close();
    }
    ?>

    <form method="post" action="">
        <input name="DeckName" type="text">
        <br>
        <br>
        <input name="Author" type="text">
        <br>
        <br>
        <input type="checkbox" id="Red" name="Red"> <img src="red.png" alt="Red">
        <input type="checkbox" id="Blue" name="Blue"> <img src="blue.png" alt="Blue">
        <input type="checkbox" id="Green" name="Green"> <img src="green.png" alt="Green">
        <input type="checkbox" id="White" name="White"> <img src="white.png" alt="White">
        <input type="checkbox" id="Black" name="Black"> <img src="black.png" alt="Black">
        <br>
        <br>
        <input name="CardList" type="text">
        <br>
        <br>
        <input type="submit" value="Submit Form">
    </form>
</body>

</html>

如果选中该复选框,我希望它在列中放置一个R,如果不是则没有。什么都没有进入专栏。

提问于
用户回答回答于
if (!empty($_POST)) {
    //connect to the database
    $mysqli = new mysqli('localhost', 'root', '', 'magic');
    //check the connection
    if ($mysqli->connect_error) {
        die('Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error);
    }
    //Do NOT insert the data HERE 

    // the checkbox data
    if (isset($_POST['Red'])) {
        $red = 'R';
    } else {
        $red = ' ';
    }
    $sql = "INSERT INTO decks ( DeckName, Author, CardList, Red ) VALUES ( '{$mysqli->real_escape_string($_POST['DeckName'])}', '{$mysqli->real_escape_string($_POST['Author'])}', '{$mysqli->real_escape_string($_POST['CardList'])}' , '{$mysqli->real_escape_string($red)}' )";
    $insert = $mysqli->query($sql);
    //response from the database
    if ($insert) {
        echo "Success!";
    } else {
        die("Error, please try again!");
    }
    // I put the value of (red) in the insert sql before executing it so must be done with all check boxes ! ALL MUST BE IN ONE INSERT SENTENCE 
    //kill the connection
    $mysqli->close();
}
?>

<form method="post" action="">
    <input name="DeckName" type="text">
    <br>
    <br>
    <input name="Author" type="text">
    <br>
    <br>
    <input type="checkbox" id="Red" name="Red"> <img src="red.png" alt="Red">
    <input type="checkbox" id="Blue" name="Blue"> <img src="blue.png" alt="Blue">
    <input type="checkbox" id="Green" name="Green"> <img src="green.png" alt="Green">
    <input type="checkbox" id="White" name="White"> <img src="white.png" alt="White">
    <input type="checkbox" id="Black" name="Black"> <img src="black.png" alt="Black">
    <br>
    <br>
    <input name="CardList" type="text">
    <br>
    <br>
    <input type="submit" value="Submit Form">
</form>

在你的代码中你只是设置sql语句插入红色但没有执行它,即使你这样做,它将在新行中,其他列的空值,

我写的代码适用于Red复选框

用户回答回答于

尝试使用以下代码插入数据:

<?php

if ( !empty( $_POST) ) {
    //connect to the database
    $mysqli = new mysqli( 'localhost', 'root', '', 'magic' );

    //check the connection
    if ( $mysqli->connect_error ) {
        die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
    }

    //insert the data
    $sql = "INSERT INTO decks ( DeckName, Author, CardList ) VALUES ( '{$mysqli->real_escape_string($_POST['DeckName'])}', '{$mysqli->real_escape_string($_POST['Author'])}', '{$mysqli->real_escape_string($_POST['CardList'])}' )";

    //insert the checkbox data
    if ( isset($_POST['Red']) ) {
        $sql = "INSERT INTO decks ( Red ) VALUES ( '{$mysqli->real_escape_string('R')}' ) ";
    } else { 
        $sql = "INSERT INTO decks ( Red ) VALUES (' ')";
    }

    //response from the database
    if ($mysqli->query($sql) === TRUE) {
        echo "Success!";
    } else {
        die("Error, please try again!");
    }

    //kill the connection
    $mysqli->close();
}
?>

$insert = $mysqli-> query($sql);从代码中删除此行,并将其替换if ($mysqli->query($sql) === TRUE) {为游览代码if ( $insert ) {

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励