duanhunlou7051 2017-08-14 04:52
浏览 60

mysqli数据插入工作正常但没有数据插入数据库

I'm quite new to php although i understand the foundation of php. Most of it atleast.

there are 2 problems with my php script.

One: Even though i typed-in the information inside the text box it always directs me to the ?insert=empty im not sure why it's always telling me that one of the boxes are empty even though it isn't. is it the input type="date"?.

Two: I tried removing the if(empty) method leaving the mysqli query. And it works just fine although no data was inserted into the database.

PHP

session_start();



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

include 'includes/dbh.inc.php';
$id = $_GET['id'];
$c_id = $_REQUEST['u_uid'];
$e_name = mysqli_real_escape_string($conn, $_POST['e_name']) ;
$d_event = mysqli_real_escape_string($conn, $_POST['d_event']) ;
$t_event = mysqli_real_escape_string($conn, $_POST['t_event']) ;
$e_t_event = mysqli_real_escape_string($conn, $_POST['e_t_event']) ;
$theme = mysqli_real_escape_string($conn, $_POST['theme']) ;
$date = date('Y-m-d H:i:s');


    if (empty($e_name) || empty($d_event) || empty($t_event) || empty($e_t_event) || empty($theme)){

        header("Location: ../package.all.php?insert=empty");
        exit();
    }else{




$sql = "INSERT INTO event_table (event_name, event_date, event_time_start, event_time_end, cusact_id, theme, reserve_date_time, package_id) VALUES ('$e_name', '$d_event', '$t_event', '$e_t_event', '$theme', '$date', '$id');";
mysqli_query($conn, $sql);

header("Location: ../package.all.php?insert=success");

}
}
 else {

    echo 'isset was false'; 
    header("Location: ../inquire-packages.inc.php?insert=failed");

}

HTML

<form action="includes/inquire-packages.inc.php" method="POST">
<label>Event Name</label>
<input type="text" name="e_name" placeholder="Enter Event Name*"/>

<label for="job">Day of the Event</label>
<input type="date" name="d_event" placeholder="Enter Date of Event*"/>


<label for="job">Time of the Event</label>
<input type="Time" name="t_event" placeholder="Enter Time of Event*"/>

<label for="job">End Time of the Event</label>
<input type="Time" name="e_t_event" placeholder="Enter End Time of Event*"/>

<label>Theme</label>
<input type="text" name="theme" placeholder="Enter Theme*"/>
<input name="submit" type="submit" value="Submit"/>
</form>

I'm sorry if this is a stupid question but i really want to learn php code.

Thank you, Omar

  • 写回答

2条回答 默认 最新

  • down2323 2017-08-14 05:01
    关注

    1st : Column count doesn't match with value count . your column count is 8 but value count 7 . so you missed one value . try to debug it .

    2nd : I guess you missed $c_id.

    3rd : Try to use prepared statement to avoid sql injection .

    4th : Header() function doesn't terminate the code execution . so use exit() after header function .

    5th : Add error handler mysqli_query($conn, $sql) or die(mysqli_error($conn));

    6th : And where from you get these two values $id = $_GET['id'];$c_id = $_REQUEST['u_uid'];

    PHP :

    $sql = "INSERT INTO event_table (event_name, 
                                     event_date, 
                                     event_time_start, 
                                     event_time_end, 
                                     cusact_id, 
                                     theme, 
                                     reserve_date_time, 
                                     package_id) 
                            VALUES ('$e_name', 
                                    '$d_event', 
                                    '$t_event', 
                                    '$e_t_event', 
                                    '$c_id',
                                    '$theme', 
                                    '$date', 
                                    '$id');";
    
      mysqli_query($conn, $sql) or die(mysqli_error($conn));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?