我是PHP的新手。我在isset命令中添加了,表单正在使用数据库,但每次调出此页面时,它都会自动将空白记录提交到我的数据库。我如何才能使它只在输入输入并且用户点击提交时才通过PHP命令运行?
<?php
$handle = isset($_POST['handle']);
$address = isset($_POST['address']);
$code = isset($_POST['code']);
$rrr=mysqli_connect("localhost","knock_knock","whosthere","1");
if (mysqli_connect_errno()) {
echo "Connection to DB failed" . mysqli_connect_error();
}
$handle2 = mysqli_real_escape_string($rrr, $handle);
$address2 = mysqli_real_escape_string($rrr, $address);
$code2 = mysqli_real_escape_string($rrr, $code);
if(empty($handle)){
echo "Handle can not be empty";
}
$sql="INSERT INTO players (Handle, Address, Code)
VALUES ('$handle2', '$address2', '$code2')";
if (!mysqli_query($rrr,$sql)) {
die ('Error: ' . mysqli_error($rrr));
}
echo "Record added";
mysqli_close($rrr);
?>
答案 0 :(得分:4)
$handle = isset($_POST['handle']);
$address = isset($_POST['address']);
$code = isset($_POST['code']);
如果set.it应该是 -
,上面的代码将在所有变量中存储true$handle = isset($_POST['handle']) ? $_POST['handle'] : false;
$address = isset($_POST['address']) ? $_POST['address'] : false;
$code = isset($_POST['code']) ? $_POST['code'] : false;
并且表单提交后应该执行的所有代码都应放在 -
中if (isset($_POST['submit'])) {
//code to be executed
}
submit
或任何字段用于提交按钮。然后应为empty
值添加检查。
你必须停止执行或重定向。
if(empty($handle)){
header('location:page.php?msg=yourmessage');
exit;
}
或
if(empty($handle)){
echo "Handle can not be empty";
exit;
}
答案 1 :(得分:0)
在顶部放置一个简单的if条件
$handle =$_POST['handle'];
if(empty(isset($handle)) || $handle=='')
{
echo "Handle can not be empty";
}
else
{
//Your stuff
}