duanmu1736 2013-12-27 17:49
浏览 66
已采纳

我正在尝试根据帖子时间戳向mysql添加注册日期

This works for me but it adds the datetime in mysql as 1970 01 01 00:00 00

include("mysql_connect.php");
$phpdate = strtotime( $mysqldate );
$mysqldate = date( 'Y-m-d H:i:s', $phpdate );

//check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "INSERT INTO people (username, email, sign_up_date) VALUES ('$_POST[username]','$_POST[usermail]', '" . $mysqldate . "'  )";

if (!mysqli_query($mysqli,$sql)) {
    die('Error: ' . mysqli_error($mysqli));
}
echo "1 record added";

mysqli_close($mysqli);

Here's my html form:

<section class="loginform tmr">
<form name="login" action="regi.php" method="post" accept-charset="utf-8">
    <label for="username">Username: </label><br />
        <input type="username" name="username" placeholder="Handle" required><br />
        <input type="hidden" name="sign_up_date" value="<?php echo $_POST['sign_up_date'] ?>">
    <label for="usermail">Email: </label><br />
        <input type="email" name="usermail" placeholder="yourname@email.com" required><br />
    <label for="password">Password: </label><br />
        <input type="password" name="password" placeholder="password" required><br />
        <input type="submit" value="Login">
</form>
</section>

I'm using a hidden input and requesting the post value.

I tried using timesteamp() and date()

sign_up_date type in mysql is datetime..

should i use something like this?

$insertdate = date('Y-m-d', strtotime($_POST['Date']));

Additionally, I have a field called account_permissions enum('a', 'b', 'c') and I'm wondering how I insert that into my form (should i use another hidden input)... I need it to default to 'a' all the time.

Any help would be greatly appreciated. Thank you.

  • 写回答

4条回答 默认 最新

  • doulun0651 2013-12-27 18:02
    关注

    If your sign_up_date column data type is DATE, you can just use the MySQL CURDATE() function:

    INSERT INTO people (username, email, sign_up_date)
      VALUES ('name', 'email', CURDATE())
    

    Remember that a DATE column will store just the date, without the time of day. If you want to include the time of day accurate to seconds, make it a DATETIME column and populate it with NOW() instead of CURDATE(). If you want to include the time of day accurate to fractions of a second, make it a TIMESTAMP column and populate it with CURRENT_TIMESTAMP.

    IMPORTANT: note that the point behind mysqli is to avoid using expressions like ... VALUES ('$_POST[username]', ..., which leave you open to SQL Injection attacks. See here for more information, and please follow their guidelines - SQL Injection is a very real threat.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分