gpt4 book ai didi

php - php如何获取用户输入

转载 作者:行者123 更新时间:2023-11-29 10:36:48 26 4
gpt4 key购买 nike

我正在制作一个事件管理系统。我不知道出了什么问题,在我点击提交按钮后,它只是不断重新加载。

这是我的 HTML 代码

<?php 
session_start();

if (!isset($_SESSION['username'])) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
}

if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header("location: login.php");
}

<div class="header">
<h2>Create Event</h2>
</div>

<form method="post" action="create_event.php">
<div class="input-group">
<label>Event Title</label>
<input type="text" name="event_title" >
</div>
<div class="input-group">
<label>Pick Location</label>
<select name="location">
<option>Alabang</option>
<option>South City</option>
<option>Batangas</option>
<option>Cavite</option>
<option>Laguna</option>
</select>
<div class="input-group">
<label>Address</label>
<textarea rows="4" cols="50" name="address"></textarea>
</div>
<div class="date">
<label>Pick date and time</label>
<input type="date" name="date"></br>
<input type="time" name="time">
</div>
<div class="event_info">
<label>Event Information</label>
<textarea rows="4" cols="50" name="event_info"></textarea>
</div>
<div class="input-group">
<button type="submit" class="btn" name="create_event">Create Event</button>
</div>
</form>

PHP代码:

    // Create Event
if (isset($_POST['create_event'])) {
// receive all input values from the form
$event_title = mysql_real_escape_string($_POST['event_title']);
$location = mysql_real_escape_string($_POST['location']);
$address = mysql_real_escape_string($_POST['address']);
$date = mysql_real_escape_string($_POST['date']);
$time = mysql_real_escape_string($_POST['time']);
$event_info = mysql_real_escape_string($_POST['event_info']);
$query="select * from `event` where event_title='$_POST[event_title]' or time='$_POST[time]' or date='$_POST[date]'";
$result=mysql_query($query);
$count=mysql_num_rows($result);
if($count>0)
{
array_push($errors, "Already used");
}


// form validation: ensure that the form is correctly filled
if (empty($event_title)) { array_push($errors, "Please input title"); }
if (empty($location)) { array_push($errors, "Location is required"); }
if (empty($address)) { array_push($errors, "Location is required"); }
if (empty($time)) { array_push($errors, "Time is required"); }
if (empty($date)) { array_push($errors, "Date is required"); }
if (empty($event_info)) { array_push($errors, "Date is required"); }


// register user if there are no errors in the form
if (count($errors) == 0) {
$query = "INSERT INTO `event` (event_title,location,address,time,date,event_info) VALUES ('$event_title', '$location','$address','$time','$date','$event_info')";
$result = mysql_query($query);
echo("success");
}

}

我认为没有任何问题,因为如果我单击提交 按钮,则不会弹出错误。它只是重新加载,但没有数据发送到我的数据库。

最佳答案

在表单中修改您的提交按钮,如下所示:

  <input  type = "submit"  name = "create_event" value="create event"/>

还在操作中使用$_SERVER['PHP_SELF']变量。像这样...

     <form action ="<?php echo 
htmlspecialchars($_SERVER['PHP_SELF']);
?>" method = "post">

数据库查询时请使用PDO语句,简单又安全。并且在使用用户输入之前对其进行清理。

关于php - php如何获取用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46260498/

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