如何将多个数据插入一列

时间:2014-02-02 06:11:05

标签: php mysql

我尝试将数据学生名称'Dhiya'和'Siti'和$ checkbox插入数据库。我首先从数据库中的student_course表中回忆起数据。我检查了Presence的名字。这是代码:

<html>
<div class="container">
<div class="content">
<?php
include ('system2.php');
include('connect_database.php');
$course = $_POST['course'];
$batch = $_POST['batch'];
$group = $_POST['group'];
$student_name = $_POST['Student_Name'];
$checkbox = $_POST['checkbox'];
$query_view1 = mysql_query("SELECT distinct Student_Name FROM student_course where batch='12/01'");
$query_view2 = mysql_query("SELECT distinct Student_Name FROM student_course where batch='12/02'");

echo "<form action ='system23.php' method = 'post'>";
echo "<table border = '1' align='center'>";
echo "<tr>
    <th><font color=#555555>Name</font> </th>
    <th><font color=green>Presence</font> </th>
    <th><font color=red>Absence</font> </th>
    <th><font color=#FFCC00>Permission</font> </th>
    <th><font color=purple>Late</font> </th>
 </tr>";

if ($batch=='12/01')
{
    while($row = mysql_fetch_array($query_view1) )
    {
        echo "<tr>";?>
        <td><input type="text" name="stuname" value="<?php echo $row['Student_Name']?>"/></td>
        <?php
        echo "<td><input type='checkbox' name='checkbox' value='P'/></td>";
        echo "<td><input type='checkbox' name='checkbox' value='A'/></td>";
        echo "<td><input type='checkbox' name='checkbox' value='PR'/></td>";
        echo "<td><input type='checkbox' name='checkbox' value='L'/></td>";
        echo "</tr>";
    }
}
else if ($batch=='12/02')
{
    while($row = mysql_fetch_array($query_view2) )
    {
        echo "<tr>";?>
        <td><input type="text" name="stuname" value="<?php echo $row['Student_Name']?>"/></td>
        <?php
        echo "<td><input type='checkbox' name='checkbox' value='P'/></td>";
        echo "<td><input type='checkbox' name='checkbox' value='A'/></td>";  
        echo "<td><input type='checkbox' name='checkbox' value='PR'/></td>";
        echo "<td><input type='checkbox' name='checkbox' value='L'/></td>";
        echo "</tr>";
    }
}   
echo "<tr>";
echo "<td colspan='6' align='center'><input name='submit' type='submit' value='GO' /></td>";
echo "</tr>";
echo "</table>";
echo "</form>"; 
?>
</div>
</div>
</html>

然后我尝试将数据插入数据库。这是代码:

<html>
<div class="container">
<?php
include ('system22.php');
include('connect_database.php');
$checkbox = $_POST['checkbox'];
$query_view1 = mysql_query("SELECT distinct Student_Name FROM student_course where batch='12/02'");
$query_view2 = mysql_query("SELECT distinct Student_Name FROM student_course where batch='12/02'");
$student_name = $_POST['stuname'];
$batch= $_POST['batch'];
mysql_query ("INSERT INTO result2 VALUES ('$student_name', '$checkbox')");
echo "Success";
?>
</div>
</html>

但是当我检查数据库时,它只放了'Siti'。我不知道还有其他什么方法可以使用

由于我无法发布图片,我会尽量让它尽可能清晰

student_course表

<html>
    <table>
        <tr>
            <th>Name</th>
            <th>1</th>
        </tr>
        <tr>
            <td>Siti</td>
            <td>P</td>
        </tr>
    </table>
</html>

对于表result2结构。

字段:'名称'和'1' 类型:两个varchar(99)。 没有主要和前卫的钥匙。

2 个答案:

答案 0 :(得分:1)

如果您只将所有内容插入一列,则需要在整个字符串周围加上引号,而不是两个变量。

mysql_query ("INSERT INTO result2 VALUES ('$student_name, $checkbox')");

应该有效

或者您可以简单地将两个变量添加到新的变量中,如:

$insert = $student_name.", ".$checkbox;
mysql_query ("INSERT INTO result2 VALUES ('$insert')");

但是他们最好的选择是切换到PDO例如并准备语句以避免SQL一劳永逸地注入;)

答案 1 :(得分:0)

例如,对天气使用if语句或不选中该框。

if (!isset($_POST['checkboxname'])) {
    // checkbox was not checked
} else {
    // checkbox was checked
}