gpt4 book ai didi

php - 如何只在输入不为空时才向数据库中插入数据?

转载 作者:行者123 更新时间:2023-11-29 06:27:15 25 4
gpt4 key购买 nike

由于我是 PHP 新手,我遇到了一个让我抓狂的问题。问题是:下面的时间表提交表格在 Chrome 上运行良好(每次我未填写电子邮件时,都无法处理提交)。但是,在 safari 上使用时,您始终可以将空白表单提交到数据库中。

这是 html 表单。

<script type="text/javascript" src="/membership/my-form/js/jquery-2.1.1.js"></script>
<script type="text/javascript" src="/membership/my-form/js/main.js"></script>

<form class="mf-form floating-labels" method="post" action="timetablesubmit.php">

<div>
<h1 style="text-align:center">Availability form</h1>
</div>
<div>
<p class="mf-select icon">
<select name="timetable-staff" class="user" required>
<option value="">Select Person</option>
<option value="AMY">Amy</option>
<option value="TOM">Tom</option>
</select>
</p>

<div>
<input name="location1" value="A" type="hidden">
<input name="location2" value="B" type="hidden">
</div>
<div class="AMY box">You work in A.</div>
<div class="TOM box">You work in B.</div>
</div>

<div class="icon">
<label class="mf-label" for="mf-email">Email Address</label>
<input class="email" type="email" name="timetable-email" id="mf-email" required>
</div>

</form>


<script type="text/javascript">
$(document).ready(function(){
$("select").change(function(){
$( "select option:selected").each(function(){
if($(this).attr("value")=="Amy"){
$(".box").hide();
$(".AMY").show();
}
if($(this).attr("value")=="Tom"){
$(".box").hide();
$(".TOM").show();
}
});
}).change();
});
</script>


<style type="text/css">
.box{
padding: 10px;
display: none;
margin-top: 20px;
border: 1px solid #000;
font-size:1.6em;
text-align:center;
background-color: #f1f1f1;
}
</style>

这是 timetablesubmit.php:

<?php
header("content-type:text/html;charset=utf-8");

session_start();

$timesubmit=date('Y-m-d H:i:s');
$staff=$_POST['timetable-staff'];
$email=$_POST['timetable-email'];


$con=mysql_connect("localhost","database","password");
if (!$con) {
die ('Could not connect:' . mysql_error());
}

mysql_select_db("database", $con);

mysql_query("set names utf8");
mysql_query("INSERT INTO timetable(staff,email)
VALUES('$staff','$email')");

mysql_close($con);

sleep(2);
?>

<html>
<? require 'header.php'; ?>
<div class="tk-reg">
<p>Thak you <?php echo $_POST['timetable-staff']; ?><p>
<p> Time availability submitted successfully.<p>
Your email address is: <?php echo $_POST["timetable-email"]; ?>
</div>

<div class="tk-regfollow">
<ul style="text-align:center">
<a href="/membership/index.php">Back to home page.</a>
</ul>
</div>
</html>

然后我在互联网上搜索并更改为以下,仍然无法在 Safari 上工作(出现警告,但是数据仍然被插入到数据库中。

<?php
require 'header.php';
$nameErr = $emailErr = "";
$name = $email = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["timetable-staff"])) {
$nameErr = "Please select a staff.";
} else {
$staff = test_input($_POST["timetable-staff"]);
}
if (empty($_POST["timetable-email"])) {
$emailErr = "Email address is required";
} else {
$email = test_input($_POST["timetable-email"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

$con=mysql_connect("localhost","database_name","database_password");
if (!$con) {
die ('Could not connect:' . mysql_error());
}

mysql_select_db("database_name", $con);

mysql_query("set names utf8");
mysql_query("INSERT INTO timetable(staff,email)
VALUES('$staff','$email')");

mysql_close($con);
sleep(2);
?>


<html>
<style>
.error {color: #FF0000;}
</style>

<h1 style="text-align:center">Availability form for
<?php
$d=strtotime("+1 Months");
echo date("M", $d) . " 2015 <br>";
?>
</h1>

<form class="mf-form floating-labels" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div>
<p class="mf-select icon">
<select name="timetable-staff" class="user" required>
<option value="">Select Person</option>
<option value="AMY">Amy</option>
<option value="TOM">Tom</option>
</select>
<span class="error">* <?php echo $nameErr;?></span>
</p>
</div>
<div class="icon">
<label class="mf-label" for="mf-email">Email Address</label>
<input class="email" type="email" name="timetable-email" id="mf-email" required>
<span class="error">* <?php echo $emailErr;?></span>
</div>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</html>

希望有人能帮我指出我的错误。谢谢。

============================================= =======================

我尝试了很多方法来解决这个问题,但仍然有“这个”或“那个”的问题。终于用了一个确实管用的方法,不知道是不是不推荐。

这是我在 timetablesubmit.php 中修改的代码(我的流程是:timetable.php ==>timetablesubmit.php==>welcome.php)

$email = trim($_POST['timetable-email']);
if($email !='' and $email !=null) {
$sql1;
}
else {
echo '<html><head>
<link rel="stylesheet" type="text/css" href="/membership/style.css"/>
<head>
<div class="check-error-message">
<p>Error: Email address is required.</p><br>
<a href="javascript:history.back(-1);">Return</a>
</div>';
exit;
};

最佳答案

你可以在插入操作前检查条件

if($nameErr =="" && $emailErr == "")
{
mysql_query("INSERT INTO timetable(staff,email)
VALUES('$staff','$email')");
}

关于php - 如何只在输入不为空时才向数据库中插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30183530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com