无法将数据输入mysql

时间:2012-08-16 10:58:22

标签: php mysql

用于将数据输入数据库的php脚本正在有选择地工作。 5年级和10年级的数据进入,但所有其他年级的数据都不会出现。我检查了if else块,逻辑工作正常,它执行内部块,因为它应该不会将任何数据输入数据库。对于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();
}


}
?>

表的结构(期望表“十”)

列类型排序规则属性空默认ExtraAction id int(11)否无AUTO_INCREMENT
roll varchar(3)latin1_swedish_ci否无
marks smallint(6)否无std varchar(2)latin1_swedish_ci否无
sec varchar(1)latin1_swedish_ci否无

表十的结构

列类型排序规则属性空默认ExtraAction id int(11)否无AUTO_INCREMENT
roll varchar(3)latin1_swedish_ci否无
marks smallint(6)否无std varchar(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表格中输入这些值时,它回应“roll-12 marks-454 std-7 section-B done”。我不明白为什么它不会只在数据库中输入这些值

2 个答案:

答案 0 :(得分:1)

如果此代码中的任何内容不是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();
}

答案 1 :(得分:1)

您忘记在查询中在$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')";