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

这是代码,让我知道你们是否也需要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();
}


}
?>


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

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

表十的结构

列类型排序规则属性Null默认ExtraAction
id int(11)否无AUTO_INCREMENT
roll varchar(3)latin1_swedish_ci否无
标记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标记-454 std- 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')";

关于php - 无法将数据输入mysql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11985696/

10-14 12:52
查看更多