无法将数据输入到 mysql 中


Unable to enter data into mysql

旨在将数据输入数据库的 php 脚本正在有选择地工作。5年级和10年级的数据进入,但所有其他年级的数据不会显示。我已经检查了ifelse块,并且逻辑运行良好,可以执行内部块,因为它应该不会将任何数据输入数据库。对于 5 年级或 10 年级以外的成绩,执行第 2 个块,我已经检查过它是否回显了它应该进入数据库的东西的正确值。在sql()查询之后使用echo,我也发现它也可以工作,所以有点卡不了不知道出了什么问题,请帮忙。

这是代码,让我知道你们是否也需要 HTML

<?php
$roll=$_POST['roll'];
$marks=$_POST['marks'];
$dbase=$_POST['std'];
$sec=$_POST['sec'];
$sec1=$_POST['sec1'];
$expire= time()+60;
if (empty($roll) || empty($marks) || ($dbase==0))
{
echo '<p align="center">You did not enter all data. Please go back and enter them.</p>';
echo '<FORM><p align="center"><INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1);return true;"></p></FORM>';
die();
}
else
{
    if($dbase==5)
    {
        $temp="five";
    }
    if($dbase==6)
    {
        $temp="six";
    }
    if($dbase==7)
    {
        $temp="seven";
    }
    if($dbase==8)
    {
        $temp="eight";
    }
    if($dbase==9)
    {
        $temp="nine";
    }
    if($dbase==10)
    {
        $temp="ten";
    }
    if($dbase==11)
    {
        $temp="eleven";
    }
    if($dbase==12)
    {
        $temp="twelve";
    }
if(($dbase==5)&&($sec=="0"))
{
    echo '<p align="center">You did not enter all data. Please go back and enter them. 2</p>';
    echo '<FORM><p align="center"><INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1);return true;"></p></FORM>';
    die();
}
else
{
    if($dbase<6)
    {
    $sect=$sec;
    setcookie("tab", $temp, $expire, "/","skc-hs.com");
    $host="127.0.0.1"; // Host name
    $username="root"; // Mysql username
    $password=""; // Mysql password
    $db_name="data"; // Database name
    $tbl_name=$temp; // Table name
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    // To protect MySQL injection (more detail about MySQL injection)
    $roll = stripslashes($roll);
    $marks = stripslashes($marks);
    $roll = mysql_real_escape_string($roll);
    $marks = mysql_real_escape_string($marks);
    $sql="insert into $tbl_name (roll , marks, std, sec) values ('$roll' , '$marks' , '$dbase' , '$sect')";
    mysql_query($sql);
    header( 'Location: dcreate.html');
    die();
    }
}
if(($dbase!=10)&&($dbase!=5)&&($sec1=="0"))
{
    echo '<p align="center">You did not enter all data. Please go back and enter them. 3</p>';
    echo '<FORM><p align="center"><INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1);return true;"></p></FORM>';
    die();
}
else
{
    if(($dbase!=5)&&($dbase!=10))
    {
    $sect=$sec1;
    setcookie("tab", $temp, $expire, "/","skc-hs.com");
    $host="127.0.0.1"; // Host name
    $username="root"; // Mysql username
    $password=""; // Mysql password
    $db_name="data"; // Database name
    $tbl_name=$temp; // Table name
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    // To protect MySQL injection (more detail about MySQL injection)
    $roll = stripslashes($roll);
    $marks = stripslashes($marks);
    $roll = mysql_real_escape_string($roll);
    $marks = mysql_real_escape_string($marks);
    $sql="insert into $tbl_name (roll , marks, std, sec) values ('$roll' , '$marks' , '$dbase' '$sect')";
    mysql_query($sql);
    header( 'Location: dcreate.html');
    die();
    }
}
if($dbase==10)
{
    setcookie("tab", $temp, $expire, "/","skc-hs.com");
    $host="127.0.0.1"; // Host name
    $username="root"; // Mysql username
    $password=""; // Mysql password
    $db_name="data"; // Database name
    $tbl_name=$temp; // Table name
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    // To protect MySQL injection (more detail about MySQL injection)
    $roll = stripslashes($roll);
    $marks = stripslashes($marks);
    $roll = mysql_real_escape_string($roll);
    $marks = mysql_real_escape_string($marks);
    $sql="insert into $tbl_name (roll , marks, std) values ('$roll' , '$marks' , '$dbase')";
    mysql_query($sql);
    header( 'Location: dcreate.html');
    die();
}

}
?>

表的结构(预期为表"十")

列类型排序规则属性空默认额外操作id int(11) 否 无 AUTO_INCREMENT
卷曲瓦尔查(3) latin1_swedish_ci 无
标记小(6) 否 无 标准变量(2) latin1_swedish_ci 无
秒 瓦尔查尔(1) latin1_swedish_ci 无

表十的结构

列类型排序规则属性空默认额外操作id int(11) 否 无 AUTO_INCREMENT
卷曲瓦尔查(3) latin1_swedish_ci 无
标记小(6) 否 无 标准变量(2) latin1_swedish_ci 无

当我像这样修改第二个 if 块时

if(($dbase!=5)&&($dbase!=10))
    {
    $sect=$sec1;
            echo "roll- ";
            echo $roll;
            echo "  marks- ";
            echo $marks;
            echo " std- ";
            echo $dbase;
            echo " section- ";
            echo $sect;
            setcookie("tab", $temp, $expire, "/","skc-hs.com");
    $host="127.0.0.1"; // Host name
    $username="root"; // Mysql username
    $password=""; // Mysql password
    $db_name="data"; // Database name
    $tbl_name=$temp; // Table name
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    // To protect MySQL injection (more detail about MySQL injection)
    $roll = stripslashes($roll);
    $marks = stripslashes($marks);
    $roll = mysql_real_escape_string($roll);
    $marks = mysql_real_escape_string($marks);
    $sql="insert into $tbl_name (roll , marks, std, sec) values ('$roll' , '$marks' , '$dbase' '$sect')";
    mysql_query($sql);
    echo " done";
    die();
    }

当我在 html 表单中输入这些值时,它会回显"滚动 - 12 标记 - 454 标准 - 7 部分 - B 完成"。我不明白为什么它不会只在数据库中输入这些值

您忘记在查询中在 $dbase$sect 之间放置逗号。

将查询从

   $sql="insert into $tbl_name (roll , marks, std, sec) 
          values ('$roll' , '$marks' , '$dbase' '$sect')";

   $sql="insert into $tbl_name (roll , marks, std, sec)
          values ('$roll' , '$marks' , '$dbase' , '$sect')";
如果

有任何内容不是 10 或 5,您似乎exit()脚本:

if(($dbase!=10)&&($dbase!=5)&&($sec1=="0"))
{
    echo '<p align="center">You did not enter all data. Please go back and enter them. 3</p>';
    echo '<FORM><p align="center"><INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1);return true;"></p></FORM>';
    die();
}