简体   繁体   中英

MYSQL, Insert into works but it wont connect

So, I am making a kalender sorta thing for school, and im trying to insert multiple rows thingys in the table, The delete function works good but when i try to do the add function it wont work correctly. This is my code =

<?php
$connection = new mysqli('localhost','root','','calendar');

$Days = $_POST['Day'];
$Months = $_POST['Month'];
$Years = $_POST['Year'];
$Names = $_POST['Name'];

$sql = "INSERT INTO birthdays (day), (month), (year), (person) VALUES     ('$Days'), ('$Months'), ('$Years'). ('$Names')";

echo $sql;

$connection->query($sql);

//header('location:index.php');
?>

The outcome:

$sql = "INSERT INTO birthdays (day,month,year,person) VALUES('$Days'), ('$Months'), ('$Years'). ('$Names')";

So it should work fine, But it doesnt save to the table. Im also not that much known with MYSQL yet, I think i did something wrong with the inserting part where i did multiple rows at once, but not sure how to fix it.

(Well, The dates , Names etc he gets on the last page, but that works fine)

The correct SQL syntax is this one:

$sql = "INSERT INTO birthdays (day, month, year, person) VALUES ($Days, $Months, $Years, '$Names')";

but you should use prepared statements with placeholders, something like this:

$pst = $mysqli->prepare("INSERT INTO birthdays (day, month, year, person) VALUES (?,?,?,?)");

$pst->bind_param('iiis', $day, $month, $year,  $name);
$pst->execute();

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