Using Mysqli bind_param with date and time columns?

前端 未结 8 909
清歌不尽
清歌不尽 2020-12-08 06:14

How do you insert data into a MySQL date or time column using PHP mysqli and bind_param?

相关标签:
8条回答
  • 2020-12-08 06:41

    $q = "insert into order_details (order_id, product_id, product_name, product_quantity, product_price, created_at, updated_at) values (?, ?, ?, ?, ?, NOW(), NOW())";

                $stmt = mysqli_prepare($dbc, $q);
    

    mysqli_stmt_bind_param($stmt, 'iisid',$ord, $pid, $_SESSION['product_name'], $qty, $price);

    0 讨论(0)
  • 2020-12-08 06:44

    For the current date/time you can use the MySQL standard routine. You do not have to prepare that.

    $query  = "INSERT INTO tablename ";
    $query .= "VALUES(?,?,?,NOW()) ";
    $preparedquery = $dbaselink->prepare($query);
    $preparedquery->bind_param("iii",$val1,$val2,$val3);
    
    0 讨论(0)
  • 2020-12-08 06:46

    Like any other string

    $stmt = $mysqli->prepare('insert into foo (dt) values (?)');
    $dt = '2009-04-30 10:09:00';
    $stmt->bind_param('s', $dt);
    $stmt->execute();
    
    0 讨论(0)
  • 2020-12-08 06:47

    Use the mySQL function now() for timestamp fields.

    0 讨论(0)
  • 2020-12-08 06:55

    first set the date & time using date() function-

    $date=date("d/m/y");
    $time=date("h:i:sa");
    

    then pass it into the prepared statement, just like any other string variable-

    $stmt = $mysqli->prepare("INSERT INTO FOO (dateColumn, timeColumn) VALUES (?,?)");
    $stmt->bind_param("ss", $date , $time);
    $stmt->execute();
    
    0 讨论(0)
  • 2020-12-08 07:01

    Timestamps in PHP are integers (the number of seconds from the UNIX epoch). An alternative to the above is to use an integer type date/time parameter, and the MySQL functions FROM_UNIXTIME and UNIX_TIMESTAMP

    $stmt = $mysqli->prepare("INSERT INTO FOO (dateColumn) VALUES (FROM_UNIXTIME(?))");
    $stmt->bind_param("i", $your_date_parameter);
    $stmt->execute();
    
    0 讨论(0)
提交回复
热议问题