-2

如何将这些字符串数据从 PHP 插入 MySQL 表,我可以让代码运行但它不插入数据库。

php代码:

<?php

//mysql connection
$connect_error = 'Sorry, the database server is down. Please try again later.';
mysql_connect('xxxxxxxxx.com', 'xxxxxxx', 'xxxxxxxxx') or die($connect_error);
mysql_select_db('checkbox_test') or die($connect_error);

if (isset($_POST['submit'])) {
    if (isset($_POST['subject'])) {
        $strsubject = implode(",", ($_POST['subject']));
    } else {
        $strsubject = "";
    }

    echo $strsubject;
}

mysql_query("INSERT INTO levels (level_1, level_2, level_3, level_4, level_5, level_6) VALUES ($strsubject");
?>

的HTML:

<form action="" method="post" name="form_subject" id="form_subject">

    <input type="checkbox" name="subject[]" value="level_1"><label>Level 1</label><p>    
    <input type="checkbox" name="subject[]" value="level_2"></label>Level 2</label><p>  
    <input type="checkbox" name="subject[]" value="level_3"></label>Level 3</label><p>   
    <input type="checkbox" name="subject[]" value="level_4"><label>Level 4</label><p>    
    <input type="checkbox" name="subject[]" value="level_5"></label>Level 5</label><p>       
    <input type="checkbox" name="subject[]" value="level_6"></label>Level 6</label><p>

    <input type="submit" action= "" name="submit"  value="Submit" />

</form>

数据库表有 7 列,1 列用于 id(设置为 A_I),其余为每个级别命名。我是网络开发新手,谢谢大家

4

2 回答 2

0

您的INSERT语句中缺少右括号,就在:之后$strsubject和之前"

mysql_query("INSERT INTO levels (level_1, level_2, level_3, level_4, level_5, level_6) 
                    VALUES ($strsubject")

这可能不是唯一的问题。

于 2013-07-31T13:04:11.913 回答
-1
  1. 在 PHP 中,就像在 JS、C++ 和 Java 中一样,不是简单地将一个变量放入另一个变量中。使用正确的连接字符.

.

$what      = "lazr!";
$variable1 = "firin " . $what;

$sql = "....SET column1='" . $data1 . "')";

.

  1. 不要使用mysql_*函数。

  2. 在 PDO 中使用准备好的语句:http: //php.net/manual/de/pdo.prepare.php

.

<?php
/* Execute a prepared statement by passing an array of values */
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < ? AND colour = ?');
$sth->execute(array(150, 'red'));
$red = $sth->fetchAll();
$sth->execute(array(175, 'yellow'));
$yellow = $sth->fetchAll();
  1. $strsubject = implode(",", ($_POST['subject']));应该:

.

$strsubject = "'" . implode("', '", ($_POST['subject'])) . "'";
于 2013-07-31T13:43:21.743 回答