我做了一个管理员可以登录并创建一个投票的系统,问题是即使没有数据被输入投票仍在创建,我想防止这一点。 我已经尝试使用如果(空,但似乎没有工作,任何帮助将被感激。我很抱歉,如果这是不正确的格式化。
<?php include 'functions.php'; $pdo = pdo_connect_mysql(); $msg = '';
// Check if POST data is not empty
if (!empty($_POST)) {
// Post data not empty insert a new record
// Check if POST variable “title” exists, if not default the value to blank, basically the same for all variables
$title = isset($_POST[‘title’]) ? $_POST[‘title’] : ‘’;
$desc = isset($_POST[‘desc’]) ? $_POST[‘desc’] : ‘’;
// Insert new record into the “polls” table
$stmt = $pdo->prepare(‘INSERT INTO polls VALUES (NULL, ?, ?)’);
$stmt->execute([$title, $desc]);
// Below will get the last insert ID, this will be the poll id
$poll_id = $pdo->lastInsertId();
// Get the answers and convert the multiline string to an array, so we can add each answer to the "poll_answers" table
$answers = isset($_POST['answers']) ? explode(PHP_EOL, $_POST['answers']) : '';
foreach ($answers as $answer) {
// If the answer is empty there is no need to insert
if (empty($answer)) {
echo 'no entry';
} else {
echo 'continue';
}
// Add answer to the "poll_answers" table
$stmt = $pdo->prepare('INSERT INTO poll_answers VALUES (NULL, ?, ?, 0)');
$stmt->execute([$poll_id, $answer]);
}
// Output message
$msg = 'Created Successfully!';
}
?>
if (!empty($_POST))
是不够的-当您提交表单时,数组本身很可能不是空的,即使其中的字段是空的。
例如,您可能会将以下内容作为$_post
的内容发送给您:
array('title'=> '','des'=>'')
您需要验证所有需要填充的特定字段,并且只有当它们都完成得令您满意时才执行插入。 这是一个标准的验证过程。
例如。
if (!empty($_POST["desc"]) && !empty($_POST["title"]))
{
//run your query here
}
完整样品:
<?php
include 'functions.php';
$pdo = pdo_connect_mysql();
$msg = '';
// Check if POST data is valid
if (!empty($_POST["desc"]) && !empty($_POST["title"]))
// Post data not empty insert a new record
// Check if POST variable “title” exists, if not default the value to blank, basically the same for all variables
$title = $_POST["title"];
$desc = $_POST["desc"];
// Insert new record into the “polls” table
$stmt = $pdo->prepare("INSERT INTO polls VALUES (NULL, ?, ?)");
$stmt->execute([$title, $desc]);
// Below will get the last insert ID, this will be the poll id
$poll_id = $pdo->lastInsertId();
// Get the answers and convert the multiline string to an array, so we can add each answer to the "poll_answers" table
$answers = isset($_POST['answers']) ? explode(PHP_EOL, $_POST['answers']) : '';
foreach ($answers as $answer) {
// If the answer is empty there is no need to insert
if (empty($answer)) {
echo 'no entry';
} else {
echo 'continue';
}
// Add answer to the "poll_answers" table
$stmt = $pdo->prepare('INSERT INTO poll_answers VALUES (NULL, ?, ?, 0)');
$stmt->execute([$poll_id, $answer]);
}
// Output message
$msg = 'Created Successfully!';
}
?>
您的代码的问题是
if (!empty($_POST))
以上条件将不起作用,因为当您提交的表单帖子有如下数组
array('title'=> '','des'=>'')
所以上面数组似乎不是空的,然后在你写了下面这样的东西之后得到标题和描述
$title = isset($_POST[‘title’]) ? $_POST[‘title’] : ‘’;
$desc = isset($_POST[‘desc’]) ? $_POST[‘desc’] : ‘’;
如果未设置,则将获得空条目。 所以您需要检查标题和desc是否为空,如果它不是空的,那么您只需要插入到数据库中。
您可以在这里学习PHP:http://thecodetutorial.com/
我建议您在用户发送表单之前进行表单验证。
例如,防止发送空表单或在特定字段为空时防止发送空表单。
您可以使用html或JavaScript来实现这一点:
>
HTML-Tags required属性
<form action="/action_page.php">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<input type="submit">
</form>
JS-有很多框架(Bootstrap),或者您可以只在jquery中完成
文档中的引导验证
jsfiddle演示