2017-02-27 242 views
0

我知道这个问题已经被多次询问过了,我已经尝试了给定的解决方案,但是没有一个似乎不适用于我的脚本。 D:任何人都会善待启发我,我做错了什么?在MySQL列中没有插入多个复选框值

这里的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

在此先感谢!

+0

表中的“category”列的类型是什么?如果您打印它,“$ cat”的价值是多少? –

+0

'category'列的数据类型必须是varchar,如果它是'int' –

+0

它的varchar,而不是int @PathikVejani – Aki

回答

0

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

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

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

1

你能试试这条线改变这种basicly添加(阵列)..

$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... 
    ?> 
+0

嗨,我也尝试了基本的数组,并没有发生任何事情。至于连接,它已经有了,否则它不会在mysql中插入其他的字段。谢谢:) – Aki

+0

因此,您的表单中可能存在问题,您可以分享您的表单吗? – marlonjd

1

按IMBS”的建议,我已经把它在注释中(备查),因为它不影响对类别阵列通过移动SQL数据删除原来的foreach循环,并做了一些调整在它下面,它现在工作正常。 :)

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.'; 
    } 
} 

谢谢你的所有建议! :)