PHP添加日期 - mysqli_stmt :: bind_param()

时间:2015-03-24 15:48:52

标签: php mysql google-maps

我正在尝试将日期字段添加到from并保存到PHP / MySQL数据库。我正在使用phpMyAdmin并将'edate'字段设置为'DATE'类型。

我保存数据的PHP文件是:

$statement = $db->stmt_init();

date_default_timezone_set('UTC');

$edate =  $_POST['edate'];
$timestamp = date('dd-mm-yyyy', strtotime($edate));  

//database insert statement
$statement->prepare("INSERT INTO events (lat, lon, name, description,    category, edate) VALUES (?, ?, ?, ?, ?, '$timestamp')");

//grab values from the url and add to database
$statement->bind_param("ddsssi", $_POST['lat'], $_POST['lon'], $_POST['name'],     $_POST['description'], $_POST['category'], $_POST['edate']);
$status = $statement->execute();

但是当我在浏览器中运行PHP文件时。它给出了以下错误:

Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in /Users/Stephen/Sites/tidy_map/adddata.php on line 18

第18行:

$statement->bind_param("ddsssi", $_POST['lat'], $_POST['lon'], $_POST['name'], $_POST['description'], $_POST['category'], $_POST['edate']);

表单是使用Google Maps API V3的Google地图的一部分 - 当向地图添加新标记时,它会打开一个InfoWindow,其中显示带有一些Javascript的表单:

// Add Foem to New Info Window
google.maps.event.addListener(drawingManager, 'overlaycomplete', function (point)
{
    //"clone" the event-form to put in the infowindow
    var form = $(".event-form").clone().show();
    var infowindow_content = form[0];
    var infowindow = new google.maps.InfoWindow({
        content: infowindow_content,
    });

2 个答案:

答案 0 :(得分:0)

您应该尝试这样做,将时间戳绑定为字符串并告诉postgres将其转换为时间戳

//database insert statement
$statement->prepare("INSERT INTO events (lat, lon, name, description,    category, edate) VALUES (?, ?, ?, ?, ?, STR_TO_DATE(?,'%d-%m-%Y'))");

//grab values from the url and add to database
$statement->bind_param("ddssss", $_POST['lat'], $_POST['lon'], $_POST['name'],     $_POST['description'], $_POST['category'], $_POST['edate']);

答案 1 :(得分:0)

好的感谢所有帮助人员,我实际上发现了这个问题。 php工作正常:

//database insert statement
$statement->prepare("INSERT INTO events (lat, lon, name, description, category, edate) VALUES (?, ?, ?, ?, ?, ?)");

//grab values from the url and add to database
$statement->bind_param("ddssss", $_POST['lat'], $_POST['lon'], $_POST['name'], $_POST['description'], $_POST['category'], $_POST['edate']);

但我从未添加过要从我的JS代码发送的Date字段。

edate: $("input[name=edate]",this).val(),

这是我的全面疏忽。感谢您的帮助,抱歉浪费您的时间。