猿问

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

在这里有点恼火,尝试了很多东西,但没有任何效果。我想为我的 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>


如果复选框被选中,我希望它在列中放置一个 R,如果没有,则什么都不做。没有任何东西进入专栏。


茅侃侃
浏览 136回答 3
3回答

慕哥6287543

In HTML&nbsp; &nbsp; <label> Language</label>&nbsp; &nbsp; <input type="checkbox" name="lan[]" value="eng">English&nbsp; &nbsp; <input type="checkbox" name="lan[]" value="tam">Tamil&nbsp; &nbsp; <input type="checkbox" name="lan[]" value="tel">Telugu&nbsp; &nbsp; <input type="checkbox" name="lan[]" value="hin">Hindi&nbsp; &nbsp;&nbsp;In php&nbsp; &nbsp; $lan=$_POST["lan"];&nbsp; &nbsp; $slan=implode[","$lan];
随时随地看视频慕课网APP
我要回答