使用PHP将日期和时间表单字段发送到MySQL [英] Sending date and time form fields to MySQL with PHP

查看:90
本文介绍了使用PHP将日期和时间表单字段发送到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在帖子中搜索了答案,最接近的答案来自主题尝试通过php-with-mysql-need-help发布信息"

I've searched through the posts for an answer and the closest answer came from the topic "trying-to-post-date-via-php-with-mysql-need-help"

我有一个带有2个输入字段和的表单.用户从html5弹出式日历选择器中选择日期,输入时间,然后将表单提交到mysql数据库.我在获取日期和时间字段来验证/上载到数据库时遇到了麻烦.

I've got a form with 2 input fields and .The user selects the date from an html5 popup calendar picker, types in the time, and submits the form to a mysql db. I'm having trouble getting the date and time fields to validate/upload to the db.

在我的MySQL数据库中,我有一个称为约会的表,该表的一个名为curdate的列的类型为DATE;另一列称为curtime,类型为TIME.

In my MySQL db I have a table called appointments with a column called curdate that has a type of DATE; another column is called curtime with a type of TIME.

我的PHP文件的顶部是PHP验证和SQL操作,底部是粘性html表单.表单用于发送表单

My PHP file has the PHP validation and SQL actions at the top of the file and the sticky html form at the bottom. The form uses to send the form

这是代码:

$td = date( 'Y-m-d H:i:s', $phpdate );
$phpdate = strtotime( $td );
if(isset($_POST['submitted'])) {
    require_once('mysqli_connect.php');
    $errors=array();
if(empty($_POST['curdate'])) {
            $errors[]='Your date did not match';
        } else {
            $td=mysqli_real_escape_string($dbc,trim($_POST['curdate']));
    }

    if(empty($_POST['curtime'])) {
            $errors[]='Your time is empty';
    } else {
        $tt=$_POST['curtime'];
    }
if(empty($errors)) {
    $q="INSERT INTO users (first_name,last_name,email,curdate,curtime,physician,reason) VALUES ('$fn','$ln','$e','$td','$tt','$phys','$reason')";
    $r=@mysqli_query($dbc,$q);

    if($r) {
        echo '<h1>Thanks</h1>
        <p>You are now registered</p><p><br/></p>';
    } else {
        echo '<h1>System Error</h1>
        <p class="error">You could not be registered</p>';
        echo '<p>'.mysqli_error($dbc).'<br/><br/>Query:'.$q.'</p>';
    }

    mysqli_close();
    include('includes/footer.html');
    exit();
    } else {
    echo '<h1>Error</h1>
    <p class="error">The following errors occurred:<br/>';
    foreach ($errors as $msg) {
        echo "-$msg<br/>\n";
    }
    echo '</p><p> Please try again</p><p><br/></p>';
    }
    mysqli_close($dbc);
}

推荐答案

Mysql或Mysqli支持日期格式为'yyyy-mm-dd',因此在插入数据库之前,您需要以正确的格式转换日期和时间

Mysql Or Mysqli Supports date format as 'yyyy-mm-dd' so u need to convert date and time in proper format before inserting in DB

如果您要在用户提交表单时插入当前日期和时间,请使用以下查询

If you want to insert current date and time when the user submits the form then Use following Query


    INSERT INTO users (first_name,last_name,email,curdate,curtime,physician,reason) 
               VALUES ('$fn','$ln','$e',CURDATE(),CURTIME(),'$phys','$reason')

但是,如果您想在表单字段中插入日期,请​​执行以下操作

But if you want to insert date from form field then do following


    $td=$_POST['curdate'];
    $tt=$_POST['curtime'];

    $td=date('Y-m-d',strtotime($td));    //Converts date to 'yyyy-mm-dd' acceptable to mysql
    $tt=date('H:i:s',strtotime($tt));    //converts time to HH:mm:ss

    //Now apply Your Query as
     INSERT INTO users (first_name,last_name,email,curdate,curtime,physician,reason) 
               VALUES ('$fn','$ln','$e','$td','$tt','$phys','$reason')

这篇关于使用PHP将日期和时间表单字段发送到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆