简体   繁体   中英

PHP adding Date - mysqli_stmt::bind_param()

I'm trying to add a date field to a from and save to PHP/MySQL database. I'm using phpMyAdmin and have set the 'edate' field to 'DATE' type.

My PHP file to save the data is:

$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();

But when i run the PHP file in the browser. it gives teh following error:

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

with line 18 being:

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

The Form is part of a Google Map using Google Maps API V3 - when a new marker is added to the map, it opens an InfoWindow which displays the form with some 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,
    });

You should try this, bind your timestamp as a string and tell postgres to cast it to a timestamp

//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']);

Ok thanks for all the help guys, i actually discovered the problem. The php works ok:

//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']);

But I had never added the Date field to be sent from my JS code.

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

It was a total oversight on my part. Thanks for the help and sorry for wasting your time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM