<!DOCTYPE html>
<html>
<head>
<title>Data</title>
</head>
<body>
/* POST it here */
<form action="this.php" method="post">
<input type='text' name='SpouseName' required/>
<input type='text' name='SpouseAge' />
<input type="submit" name="submit" value="SUBMIT"/>
</form>
</body>
</html>
/* Here is the PHP CODE */
<?php
include 'dbh2.php';
$SpouseName = $_POST['SpouseName'];
$SpouseAge = $_POST['SpouseAge'];
$sql = "INSERT INTO test (SpouseName, SpouseAge) VALUES ('$SpouseName', '$SpouseAge')";
$result = $conn->query($sql);
?>
我想忽略像spouseage这样的空输入怎么做?
答案 0 :(得分:1)
您可以检查是否已使用 isset()
设置了值,并使用 !empty()
检查它们是否为空:
$_POST
虽然请注意,在分配变量之前,最好检查if (isset($_POST['SpouseName']) && !empty($_POST['SpouseName'])) {
$SpouseName = $_POST['SpouseName']
}
if (isset($_POST['SpouseAge']) && !empty($_POST['SpouseAge'])) {
$SpouseAge = $_POST['SpouseAge'];
}
if ($SpouseName && $SposeAge) {
$sql = "INSERT INTO test (SpouseName,SpouseAge) VALUES ('$SpouseName','$SpouseAge')";
} else {
echo "The values weren't set or were empty!";
}
值是否已设置且不为空:
public void drawPolygons(int numberOfPolygons){
for(int i=0; i<numberOfPolygons; i++){
drawPolygon(numberOfSides, lengthOfSide, penColor);
}
}
希望这会有所帮助:)
答案 1 :(得分:0)
我将查询分为两部分。现在,您可以对每个单独的输入进行表单验证并测试数据。我还为提交设置了isset,以检查是否已被点击。然后我们测试它是否为空,如果不插入,我们为每个都做。
if(isset($_POST['submit'])){
$SpouseName = $_POST['SpouseName'];
$SpouseAge = $_POST['SpouseAge'];
if(!$SpouseName==''){$sql = "INSERT INTO test (SpouseName) VALUES ('$SpouseName')";}
if(!$SpouseAge ==''){$sqlb = "INSERT INTO test (SpouseAge) VALUES ('$SpouseAge')";
}
}