dqkkrt8860 2013-04-21 12:26
浏览 38
已采纳

使用jQuery获取日期并使用PHP表单将其保存到MySQL

I need a small example on sending date to mysql db from a php form. Tried lots of examples but I seems I am missing o step. I have prepared a form for 3 fields, name last name and birth date. When I fill the form and submit date filed gets crazy :) 0000-00-00 00:00:00

I tried to format the date but this time I could not understand how to pass it to mysql. Here is my funny code;

<?php include "header.php" ?>
<?php include "conn.php" ?>
<script>
    $(function() {
        $( "#bdate" ).datepicker();
    });
</script>
<?php
    if(isset($_POST['submit'])){
        $PlayerName = $_POST['PlayerName'];
        $PlayerLname = $_POST['PlayerLname'];
        $PlayerBdate = $_POST['PlayerBdate'];
        $query = "INSERT INTO tblPlayer (
                PlayerName, PlayerLname, PlayerBdate
    ) VALUES (
        '{$PlayerName}', '{$PlayerLname}', '{$PlayerBdate}'
    )";
        if(mysql_query($query,$connection)){
            //Sucess
            header("Location: players_list.php");
        } else{
            echo "<p>something is wrong</p>";
            echo "<p>" . mysql_error() . "</p>";
        }

    }
?>
<form action="add_player.php" method="post">
<table>
<tr>
   <td>Name: </td>
   <td><input type="text" name="PlayerName" /></td>
   </tr>
   <tr>
   <td>Lastname:</td>
   <td><input type="text" name="PlayerLastName" /></td>
   </tr>
   <tr>
   <td>Birthdate:</td>
   <td><input id="bdate" type="text" name="PlayerBdate" /></td>
   </tr>
   <tr>
   <td><input type="submit" name="submit" value="Save" /></td>
   <td align="right">&nbsp;</td>
   </tr>
   </table>
<?php ob_end_flush() ?>
<?php include ("footer.php") ?>

header.php contains

<?php ob_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<title>Untitled Document</title>
</head>
<body>

and conn.php is the standard connection sting file.Footer.php is just closing the connection:) Also I could not make my code work without the ob_start(); and end functions:S

So can you point out how can I get the date as day/month/year format and save it to mysql that way?

  • 写回答

2条回答 默认 最新

  • doufangyan6862 2013-04-21 12:30
    关注

    since your date in mysql is in Y-m-d H:i:s format.. use php date() function to convert the date into required format.

    replace

    $PlayerBdate = $_POST['PlayerBdate'];
    

    with

     $PlayerBdate = date('Y-m-d H:i:s',strtotime($_POST['PlayerBdate']));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料