MySQL列中未插入多个复选框值

时间:2017-02-27 09:29:06

标签: php mysql

我知道已经多次询问过这个问题,并且我已经尝试过给定的解决方案,但似乎没有任何解决方案可以与我的脚本一起使用。 D:任何人都会善意地开导我,我做错了什么?

这是HTML脚本的一部分:

<td class="tableBody">
    <input type="checkbox" name="category[]" value="Website" /> Website
    <input type="checkbox" name="category[]" value="Members" /> Members
    <input type="checkbox" name="category[]" value="Updates" /> Updates
    <input type="checkbox" name="category[]" value="Cons" /> Cons
    <input type="checkbox" name="category[]" value="Others" /> Others
</td>

这是MySQL脚本的一部分:

$title = htmlspecialchars(strip_tags($_POST['title']));
    $entry = $_POST['entry'];
    $cat = implode(",", $_POST['category']);

    date_default_timezone_set('Asia/Manila');
    $timestamp = date('Y-m-d H:i:s');

    $entry = nl2br(htmlentities($entry, ENT_QUOTES, 'UTF-8'));

    if (!get_magic_quotes_gpc()) {
            $title = addslashes($title);
            $entry = addslashes($entry);
        }

    $insert = "INSERT INTO `blog` (`id`, `username`, `entry_id`, `title`, `entry`, `category`, `timestamp`) VALUES ('$id', '$name', '',  '$title', '$entry', '$cat', '$timestamp');";

表单工作正常,只是类别未插入列中。enter image description here

提前致谢!

3 个答案:

答案 0 :(得分:1)

您可以尝试将此行更改为此基本添加(数组)..

$cat = implode(",", (array)$_POST['category']);    

如果这不再有效,请看这个;我添加一些行添加这些行,看看是否有错误或类似的东西。可能存在数据库连接或数据库结构问题。你可以看到是否有错误。我测试了它,它对我有用。

<?php
        $title = htmlspecialchars(strip_tags($_POST['title']));
        $entry = $_POST['entry'];
        $cat = implode(",", $_POST['category']);
        echo $cat;
        date_default_timezone_set('Asia/Manila');
        $timestamp = date('Y-m-d H:i:s');

        $entry = nl2br(htmlentities($entry, ENT_QUOTES, 'UTF-8'));

        if (!get_magic_quotes_gpc()) {
                $title = addslashes($title);
                $entry = addslashes($entry);
            }

            //I add these lines from here, to...
            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "code";

            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            } 

            $sql = "INSERT INTO `blog` (`id`, `username`, `entry_id`, `title`, `entry`, `category`, `timestamp`) VALUES ('$id', '$name', '',  '$title', '$entry', '$cat', '$timestamp');";

            if ($conn->query($sql) === TRUE) {
                echo "New record inserted successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }

            $conn->close();

            //Here...
    ?>

答案 1 :(得分:1)

根据ImBS的建议,我删除了原始的foreach循环,将其置于注释中(以备将来参考),因为它确实影响了类别的数组,并通过移动它下面的SQL数据进行了一些调整现在工作正常。 :)

if (!isset($_POST['submit']) || $_SERVER['REQUEST_METHOD'] != "POST") {
    exit("<p>You did not press the submit button; this page should not be accessed directly.</p>");
}
else {

$exploits = "/(content-type|bcc:|cc:|document.cookie|onclick|onload|javascript|alert)/i";
$profanity = "/(beastial|bestial|blowjob|clit|cum|cunilingus|cunillingus|cunnilingus|cunt|ejaculate|fag|felatio|fellatio|fuck|fuk|fuks|gangbang|gangbanged|gangbangs|hotsex|jism|jiz|kock|kondum|kum|kunilingus|orgasim|orgasims|orgasm|orgasms|phonesex|phuk|phuq|porn|pussies|pussy|spunk|xxx)/i";
$spamwords = "/(viagra|phentermine|tramadol|adipex|advai|alprazolam|ambien|ambian|amoxicillin|antivert|blackjack|backgammon|texas|holdem|carisoprodol|ciara|ciprofloxacin|debt|dating|porn)/i";
$bots = "/(Indy|Blaiz|Java|libwww-perl|Python|OutfoxBot|User-Agent|PycURL|AlphaServer)/i";

if (preg_match($bots, $_SERVER['HTTP_USER_AGENT'])) {
    exit("<h1>Error</h1>\nKnown spam bots are not allowed.<br /><br />");
}
/* FOREACH LOOP
foreach ($_POST as $key => $value) {
    $value = trim($value);

    if (preg_match($exploits, $value)) {
        exit("<h1>Error</h1>\nExploits/malicious scripting attributes aren't allowed.<br /><br />");
    }
    elseif (preg_match($profanity, $value) || preg_match($spamwords, $value)) {
        exit("<h1>Error</h1>\nThat kind of language is not allowed through our form.<br /><br />");
    }

    $_POST[$key] = stripslashes(strip_tags($value));
}
END FOREACH LOOP */

    $connect = mysqli_connect("$db_server", "$db_user", "$db_password", "$db_database");
    $select=mysqli_query($connect, "SELECT * FROM `$table_members` WHERE username='$_SESSION[logged_in]'");
            while($row=mysqli_fetch_assoc($select)) {
                $id = $row['id'];
                $name = $row['username'];
            }

    $title = htmlspecialchars(strip_tags($_POST['title']));
    $entry = $_POST['entry'];
    $category = $_POST['category'];
    $cat = implode(", ", $_POST['category']);

    date_default_timezone_set('Asia/Manila');
    $timestamp = date('Y-m-d H:i:s');

    $entry = nl2br(htmlentities($entry, ENT_QUOTES, 'UTF-8'));

    if (!get_magic_quotes_gpc()) {
            $title = addslashes($title);
            $entry = addslashes($entry);
    }

    $insert = "INSERT INTO `blog` (`id`, `username`, `entry_id`, `title`, `entry`, `category`, `timestamp`) VALUES ('$id', '$name', '',  '$title', '$entry', '$cat', '$timestamp')";

    if(mysqli_query($connect, $insert)) {
        echo '<h1>Success!</h1>
        You have successfully posted a new blog entry!';
    }
    else {
        echo '<h1>Error</h1>
        It looks like there was an error in processing your submitted form.';   
    }
}

感谢您的所有建议! :)

答案 2 :(得分:0)

除了类别的数据类型外,一切似乎都很好。它可能不是varchar。尝试使用varchar。您的查询可能会显示一些警告,请使用

error_reporting(E_ALL); ini_set('display_errors', 1);

到错误报告然后手动解决查询。