我有从表单获取数据的查询,当按下提交按钮时,数据应存储在数据库中。当表单的操作是 action ="#" 时,数据被输入到数据库中。但是当操作是 action =" otherPage.php" 时,数据不会插入到数据库中。有帮助吗?
备注:我知道需要将查询更改为反对SQL注入,这仅用于测试
代码:
if(isset($_POST['submit']))
{
$name = $_POST['fullName'];
$idNumber = $_POST['idNo'];
$cardNo = $_POST['cardNo'];
$_SESSION['fullName'] = $name;
$_SESSION['id'] = $idNumber;
$checkExists = "SELECT * FROM system.table WHERE idNumber = '$idNumber' ";
$resExists = mysqli_query($connection,$checkExists)
or die("Error in query: ". mysqli_error($connection));
if(mysqli_fetch_assoc($resExists) > 0)
{
$updateCard = "UPDATE system.table SET cardNo = '$cardNo' WHERE idNumber=$idNumber";
$resUpdate= mysqli_query($connection,$updateCard)
or die("Error in query: ". mysqli_error($connection));
}
if(mysqli_fetch_assoc($resExists) < 1)
{
$company = $_POST['company'];
$name = trim($name);
$last_name = (strpos($name, ' ') === false) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name);
$first_name = trim(preg_replace('#'.$last_name.'#', '', $name));
$insert = "INSERT INTO system.table (idNumber,name,surname,company,cardNo) VALUES
('$idNumber','$first_name','$last_name','$company','$cardNo')";
$resInsert = mysqli_query($connection,$insert)
or die("Error in query: ". mysqli_error($connection));
}
$connection->close();
}
答案 0 :(得分:0)
我不知道这是否是解决问题的方法,但它确实有效。我在代码的开头添加了ob_start();
,将操作保留为
<form role="form" method="POST" action="#">
然后包括
header('Location:otherPage.php');
以便页面自动重定向到 otherPage.php
答案 1 :(得分:0)
如果您在同一文件夹中有两个文件,它应该正常工作:
MyFolder文件 - testForm.php - testUpload.php
testForm.php:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post" action="testUpload.php">
<fieldset>
<legend>Form</legend>
<label>Name: </label>
<input type="text" name="name">
<input type="submit">
</fieldset>
</form>
</body>
</html>
testUpload.php:
<?php
print($_POST['name']);
exit;
您的Web服务器配置中是否有任何重定向语句(例如Apache httpd.conf)?