你好,朋友,我想将HTML表单中的数据插入MySQL数据库,但在日期格式方面遇到一些错误
<?php
include_once 'Configuration.php';
$Dateee = $_POST['form_fields[date]'];
$am113d = $_POST['form_fields[am113d]'];
$am112d = $_POST['form_fields[am112d]'];
$am111d = $_POST['form_fields[am111d]'];
$pm13d = $_POST['form_fields[pm13d]'];
$pm12d = $_POST['form_fields[pm12d]'];
$pm11d = $_POST['form_fields[pm11d]'];
$pm53d = $_POST['form_fields[pm53d]'];
$pm52d = $_POST['form_fields[pm52d]'];
$pm51d = $_POST['form_fields[pm51d]'];
$pm63d = $_POST['form_fields[pm63d]'];
$pm62d = $_POST['form_fields[pm62d]'];
$pm61d = $_POST['form_fields[pm61d]'];
$pm73d = $_POST['form_fields[pm73d]'];
$pm72d = $_POST['form_fields[pm72d]'];
$pm71d = $_POST['form_fields[pm71d]'];
$sql = "INSERT INTO `Draw_tabel` (`Date`, `A113d`, `A112d`, `A111d`, `A133d`, `A132d`, `A131d`, `A173d`, `A172d`, `A171d`, `A183d`, `A182d`, `A181d`, `A193d`, `A192d`, `A191d`) VALUES ('STR_TO_DATE(`date`, '%d-%m-%Y')', 'am113d', 'am112d', 'am111d', 'pm13d', 'pm12d', 'pm11d', 'pm53d', 'pm52d', 'pm51d', 'pm63d', 'pm62d', 'pm61d', 'pm73d', 'pm72d', 'pm71d');" ;
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
使用PDO https://www.w3schools.com/php/php_mysql_prepared_statements.asp
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email)
VALUES (:firstname, :lastname, :email)");
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':email', $email);
// insert a row
$firstname = "John";
$lastname = "Doe";
$email = "[email protected]";
$stmt->execute();
// insert another row
$firstname = "Mary";
$lastname = "Moe";
$email = "[email protected]";
$stmt->execute();
// insert another row
$firstname = "Julie";
$lastname = "Dooley";
$email = "[email protected]";
$stmt->execute();
echo "New records created successfully";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
?>