提交表单后,它会重定向到操作页面,但不会将数据保存在数据库中

时间:2016-07-09 07:29:51

标签: php html



<?php
session_start();
include("includes/connection.php");
if(isset($_POST["testid"])&&isset($_POST["testtime"])&&isset($_POST["testdate"])&&isset($_POST["year"])&&isset($_POST["branch"]))
{
	$tid = $_POST["testid"];
	$ttime = $_POST["testtime"];
	$tdate = $_POST["testdate"];
	$year = $_POST["year"];
	$branch = $_POST["branch"];
	
	
	$query1 = "SELECT * FROM tbl_create_test";
	$result1 = mysqli_query($conn,$query1);
	
	$bool = true;
	while($row = mysqli_fetch_array($result1))
	{	
		$tidTemp= $row['fld_test_id'];
		if($tidTemp==$tid)
			{
		
			$bool=false;
			echo '<script>alert("This Test id is already Assigned");</script>'	;
			echo '<script>window.location.assign("")</script>';	
			}
		
	}	
	
	if($bool)
	{	
		$query2 = "INSERT INTO tbl_create_test(fld_test_id,fld_test_time,fld_test_date,fld_year,fld_branch) 
						 VALUES('$tid','$ttime','$tdate','$year','$branch')";

		$result2 = mysqli_query($conn,$query2);
		if($result2)
		{
			$_SESSION['test_id']=$tid;
			header("location: enter_questions.php");

		}
	}

}

?>
<!DOCTYPE HTML>
<html>
<head>
<title>Admin Area</title>
</head>
<body>
<h1 style="text-align:center;margin-top:40px;">WELCOME TO ADMIN AREA</h1>
<div style="width:50%; float:left;" >
<h2><u>Test Creation Section</u></h2>
<div style="padding:20px;">
<form method="post" action="enter_questions.php">
<label>Test id:<br><input type="text" name="testid" required></label></br>
<label>Test Time:<br><input type="time" name="testtime" required></label></br>
<label>Test Date:<br><input type="date" name="testdate" required></label></br>
<label>Year:<br><input type="text" name="year" required></label></br>
<label>Branch:<br><input type="text" name="branch" required></label></br>


<br><button type="submit" >Next</button>
</form>


</div>


</body>
</html>
&#13;
&#13;
&#13;

点击下一步后,数据不会保存在数据库中,但会重定向到enter_questions页面。我需要将数据存储在DB中以便继续。我在codeigniter方面经验丰富,但从未在核心PHP上工作过。

3 个答案:

答案 0 :(得分:0)

每个表单都有一个动作,在您的情况下,您要对表单“当我单击按钮时输入enter_questions.php”说。表格会听你指示并转到这个文件,但没有什么可以做的。如果您想在表单所在的同一位置运行代码,可以离开action=''并按照这样的提交按钮。

<input type='submit' name='submit' value='Click Me!'>

知道了问题,你怎么知道什么时候点击按钮并运行你想要的代码?那么你可以这样做。

if(isset($_POST['submit'])){
    //run your code
}

您可以阅读这篇文章how forms work

答案 1 :(得分:0)

<?php
session_start();
include("includes/connection.php");
if(isset($_POST["testid"])&&isset($_POST["testtime"])&&isset($_POST["testdate"])&&isset($_POST["year"])&&isset($_POST["branch"])) {
    $tid = $_POST["testid"];
    $ttime = $_POST["testtime"];
    $tdate = $_POST["testdate"];
    $year = $_POST["year"];
    $branch = $_POST["branch"];
    /// optimized code
    if($result = mysqli_fetch_assoc(mysqli_query($conn, "SELECT `fld_test_id` FROM tbl_create_test WHERE `fld_test_id`='$tid' LIMIT 1"))) {
        if($result = mysqli_query($conn, "INSERT INTO tbl_create_test (fld_test_id,fld_test_time,fld_test_date,fld_year,fld_branch)
                         VALUES ('$tid','$ttime','$tdate','$year','$branch')")) {
            $_SESSION['test_id']=$tid;
            header("location: enter_questions.php");
            die;
        }
    }
    /// id exists or not insert record
    echo '<script>alert("This Test id is already Assigned");</script>'  ;
    echo '<script>window.location.assign("")</script>';
}
?>

答案 2 :(得分:0)

只需从action移除form即可。如果它产生问题,请检查此include("includes/connection.php");

中的连接代码

Edit2:我已尝试使用mysql代码并将数据保存到数据库

<?php
session_start();
 mysql_connect('localhost','root','') or die('error');
 mysql_select_db('dbp');
if(isset($_POST["testid"])&&isset($_POST["testtime"])&&isset($_POST["testdate"])&&isset($_POST["year"])&&isset($_POST["branch"]))
{
    $tid = $_POST["testid"];
    $ttime = $_POST["testtime"];
    $tdate = $_POST["testdate"];
    $year = $_POST["year"];
    $branch = $_POST["branch"];


    $query1 = "SELECT * FROM tbl_create_test";
    $result1 = mysql_query($query1);

    $bool = true;
    while($row = mysql_fetch_array($result1))
    {   
        $tidTemp= $row['fld_test_id'];
        if($tidTemp==$tid)
            {

            $bool=false;
            echo '<script>alert("This Test id is already Assigned");</script>'  ;
            echo '<script>window.location.assign("")</script>'; 
            }

    }   

    if($bool==true)
    {   


        $query2 = "INSERT INTO tbl_create_test(fld_test_id,fld_test_time,fld_test_date,fld_year,fld_branch) VALUES('$tid','$ttime','$tdate','$year','$branch')";

        $result2 = mysql_query($query2)or die('error in insertion');
        if($result2)
        {
            $_SESSION['test_id']=$tid;
            header("location: enter_questions.php");

        }
    }

}

?>
<!DOCTYPE HTML>
<html>
<head>
<title>Admin Area</title>
</head>
<body>
<h1 style="text-align:center;margin-top:40px;">WELCOME TO ADMIN AREA</h1>
<div style="width:50%; float:left;" >
<h2><u>Test Creation Section</u></h2>
<div style="padding:20px;">
<form method="post" >
<label>Test id:<br><input type="text" name="testid" required></label></br>
<label>Test Time:<br><input type="time" name="testtime" required></label></br>
<label>Test Date:<br><input type="date" name="testdate" required></label></br>
<label>Year:<br><input type="text" name="year" required></label></br>
<label>Branch:<br><input type="text" name="branch" required></label></br>


<br><button type="submit" >Next</button>
</form>


</div>


</body>
</html>