doulan9419 2017-09-17 03:58
浏览 37
已采纳

如何用户输入php

I'm making an event management system. I dont know what's wrong, it just keeps reloading after I click submit button.

Here's my HTML codes

<?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 code:

    // 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");
    }

}

I don't think there is anything wrong because if I click the submit button, no errors popup. It just reloads but no data is sent to my database.

  • 写回答

1条回答 默认 最新

  • douhuanbai6729 2017-09-17 04:33
    关注

    Amend in form your submit button like this:

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

    Also in action use $_SERVER['PHP_SELF'] variable. Like this...

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

    In database query please use PDO statement it's easy and safe. And also sanitize user input before using it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址