gpt4 book ai didi

PHP MySQL 如果日期和时间不可用添加到警报消息

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

我对 PHP 相当陌生,也是一名新学生,因此,如果我犯了对有经验的人来说显而易见的错误,我会提前请求原谅我。

我正在开发一个页面提交表单,该表单连接到 mysql 数据库以检查现有值,如果不存在则记录日期时间选择器的字符串值,并附加一个 ID 号以生成唯一值每当有人按下提交键并在接受表单验证后,都会将其发送到表中。如果记录不存在则提交可以正常工作。

但是,每当记录已经存在时,整个页面就会变成空白,而不是在错误消息中显示时间不可用。

最初,我有一个按钮可以检查记录是否存在于 mysql 表中,但后来我决定在完成表单验证时尝试包含 mysql 表的日期验证检查会更有效,这是我遇到麻烦的地方当然是分页并且没有显示任何内容。

如果有人可以指导我,我将不胜感激。

看来我无法添加更多代码,但我会添加错误的内容。

我确实需要初始化脚本顶部的$error='';以及$DateTime$id,我它位于下面,但忘记将其放在上面,这导致了我的问题。

    <?php

$userid = "1";
$strid = strval($id);
$DateTimeCheck = $DateTime . $strid;
$con = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
$con->exec("SET CHARACTER SET utf8");
$sql = "SELECT booked FROM user_booking WHERE `booked` = :booked";
$sqlprep = $con->prepare($sql);
$ar_val = array(':booked' => $DateTimeCheck);

if (isset($_POST['datetime'])){

if (!$_POST['name']) {
$error = "<br/>- Please enter your name";
}
if (!$_POST['email']) {
$error .= "<br/>- Please enter your email";
}
if (!$_POST['message']) {
$error .= "<br/>- Please enter a message";
}
if (!$_POST['check']) {
$error .= "<br/>- Please confirm you are human";
}
if ($sqlprep->execute($ar_val)) {
while ($row = $sqlprep->fetch(PDO::FETCH_OBJ)) {
$DateTimeExists = $row->booked;
}
}
if (isset($DateTimeExists) && $DateTimeExists != ''){
$error .= "<br/>- The time you have requested is unavailable";
}

if ($error) {
$result = '<div class="alert alert-danger" role="alert"><strong>Whoops, there is an error</strong>. Please correct the following: ' . $error . '</div>';
} else {
mail("@gmail.com", "Contact message", "Name: " . $_POST['name'] . "
Email: " . $_POST['email'] . "
When: " . $_POST['datetime'] . "
Message: " . $_POST['message']);
{
$result = '<div class="alert alert-success" role="alert">Thank you, someone will be in touch soon to confirm your appointment. </div>';

$id = "$userid";
$strid = strval($id);
$DateTime = $_POST['datetime'];
$DateTimeCheck = $DateTime . $strid;
$strid = strval($id);
$con = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO user_booking ( user_id, booked ) VALUES ( :id, :booked )";
$q = $con->prepare($sql);
$q->execute(array(':booked' => $DateTime . $strid,
':id' => $id));
$con = null;
}
}
}

?>

这是表单的标记。

                <form method="post" role="form">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Your name"
value="<?php echo $_POST['name']; ?>">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Your email"
value="<?php echo $_POST['email']; ?>">
</div>

<div class="form-group" align="left">
<label class="control-label">Date/Time</label>
<div class='input-group date' id='datetimepicker1'>
<input type='text' name="datetime" class="form-control" placeholder="desired time"
value="<?php echo $_POST['datetime']; ?>">
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<div class="form-group">
<textarea name="message" rows="5" class="form-control"
placeholder="message..."><?php echo $_POST['message']; ?></textarea>
</div>
<div class="checkbox" align="left">
<label class="readable" align="left">
<input type="checkbox" name="check"> I am human
</label>
</div>
<div align="left">
<input type="submit" name="submit" class="btn btn-success" value="Book Appointment!"/>
</div>
</form>

最佳答案

如果您使用 .=,则需要先初始化该变量,然后 .= 对变量起作用。

您尝试在此 IF block 中执行此操作

if (!$_POST['name']) {
$error = "<br/>- Please enter your name";
}

但如果该错误不存在,您将永远不会真正初始化 $error 变量。

所以简单的解决方案是在进入这部分代码之前初始化$error

$error = '';
if (isset($_POST['datetime'])){

// So now you can change this test to use .=
if (!$_POST['name']) {
$error .= "<br/>- Please enter your name";
}

现在该变量可以在代码中进行测试

If this is in fact the problem here, you should have been getting errors reported. If you didnt see any then try adding these lines of code to any problem script while you test it

ini_set('display_errors', 1); 
ini_set('log_errors',1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

关于PHP MySQL 如果日期和时间不可用添加到警报消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59646771/

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