这是我的PHP编码。我从我的模板表单中得到了这个。
最奇怪的是,当我尝试从我的模板运行此编码时,我已经获得了输出并检索了数据,但是当我尝试将此编码运行到我的项目任务中时,它会显示空白页。
<?php
include"conn.php";
$conn = connect();
$db = connectdb();
$wardID =$_REQUEST["wardID"];
$RequestName = $_REQUEST["RequestName"];
$Department =$_REQUEST["Department"];
$Position = $_REQUEST["Position"];
$Date = $_REQUEST["Date"];
$TypeOfRequest = $_REQUEST["TypeOfRequest"];
$PleaseSpecify = $_REQUEST["PleaseSpecify"];
$DateRequire = $_REQUEST["DateRequire"];
$DateReturn = $_REQUEST["DateReturn"];
mysqli_select_db($conn,"misadmin") or die (mysqli_connect_error()."\n");
//select from the table student
$query = "select * from misform" ;
//to return the query that has been request from the database
$result = $conn ->query($query);
//Fetch a result row as an associative array
$row=mysqli_fetch_assoc($result);
//Select a MySQL database
mysqli_select_db($conn,"misadmin")or die (mysqli_connect_error()."\n");
$insert ="insert into misform(WardID,RequestName,Department,Postion,Date,TypeOfRequest,PleaseSpecify,DateRequire,DateReturn) values ('$wardID','$RequestName','$Department','$Position','$Date','$TypeOfRequest','$PleaseSpecify','$DateRequire','$DateReturn')";
$rowinsert =$conn ->multi_query($insert) or die (mysqli_connect_error()."\n");
header("Location:requestform3.php");
?>
这是我的连接
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$host_user="localhost";
$user_user="root";
$password_user="";
function connect (){
$conn =mysqli_connect("localhost","root","") or die (mysqli_connect_error()."/n");
return $conn;
}
function connectdb(){
$conndb="misadmin";
return $conndb;
}
?>
</body>
</html>
这是我的HTML
<body>
<form id="requestform" action="requestform2.php" method="post" >
<div><center><table border = "0" cellspacing="0" cellpadding"4"><tr><td>
<fieldset>
<legend>Request Form</legend>
<div id="errorDiv"></div>
<table><tr>
<td><label for="wardID">Ward ID:*</label></td>
<td><input type="text" id="wardID" name="wardID">
<span class="errorFeedback errorSpan" id="WardIDError"> Ward ID is required</span></td>
</tr>
<tr>
<td><label for="RequestName">Request Name:</label></td>
<td><input type="text" id="RequestName" name="RequestName"></td>
</tr>
<tr>
<td><label for="Department">Department:</label></td>
<td><input type="text" id="Department" name="Department"></td>
</tr>
<tr>
<td><label for="Position">Position:</label></td>
<td><input type="text"id="Position" name="Position"></td>
</tr>
<tr>
<td><label for="Date">Date:</label></td>
<td><input type="date" id="Date" name="Date"></td>
</tr>
<tr>
<td><label for="request">Type of request:* </label></td>
<td><select name="TypeOfRequest" id="request">
<option></option>
<option>Hardware</option>
<option>Software</option>
<option>Network</option>
<option>Others</option></select>
<span class="errorFeedback errorSpan" id="requestError">Please choose one</span>
</td></tr>
<tr>
<td></td>
<td><textarea name="PleaseSpecify" id="specify"> Please Specify </textarea>
<span class="errorFeedback errorSpan" id="specifyError">you not specify yet!</span>
</td></tr>
<tr>
<td><label for="DateRequire">Date Require:</label></td>
<td><input type="date" id="DateRequire" name="DateRequire">
</td></tr>
<tr>
<td><label for="DateReturn">Date Return:</label></td>
<td><input type="date" id="DateReturn" name="DateReturn">
</td></tr>
答案 0 :(得分:0)
1)Postion
sql语句中的INSERT
似乎不正确。
未知列&#39;位置&#39;在&#39;字段列表&#39;
我通过使用:
找到了这个$rowinsert = $conn->multi_query($insert) or die($conn->error . "\n");
而不是:
$rowinsert = $conn->multi_query($insert) or die(mysqli_connect_error() . "\n");
2)当要处理header(Location:...)
时,会引发以下警告:
(!)警告:无法修改标头信息 - 已发送的标头 在... / requestform2.php上的(输出从... / conn.php:9开始)在线上 32
问题是:当处理header(Location:...)
时,PHP引擎会发现某些内容已经发送到客户端,例如到浏览器,以便显示。这是不允许的。在你的代码中出现这种情况,因为你有HTML代码&#34; conn.php&#34;。这被识别为客户端的输出,因此不被允许。所以,&#34; conn.php&#34;应该只是:
<?php
$host_user="localhost";
$user_user="root";
$password_user="";
function connect (){
$conn =mysqli_connect("localhost","root","") or die (mysqli_connect_error()."/n");
return $conn;
}
function connectdb(){
$conndb="misadmin";
return $conndb;
}
?>
我是怎么发现的?我启用了错误报告和显示。所以我使用了这些陈述:
error_reporting(E_ALL);
ini_set('display_errors', 1);
将它们放入文件&#34; errorReporting.php&#34;并在&#34; conn.php&#34;之前加入当你发展。如果您的网站正在制作中,请不要包含它!如果您没有选择显示错误,那么您只是收到一个空白页面,而不知道为什么没有重定向到第三页的重定向。
<强>推荐强>:
错误+异常处理是应用程序中最重要的部分之一,应该非常正确地实现。随着用户输入的过滤和消毒。所以:
请参阅我发布的完整示例(OOP&amp; procedural)并尝试应用我展示的步骤:
但我建议你改用PDO而不是MySQLi。为此我也发布了一个相应的例子:
祝你好运!