简体   繁体   English

MYSQL,插入到工程中但无法连接

[英]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. 因此,我正在为学校制作kalender sorta物件,而我试图在表中插入多行物件,delete函数的效果很好,但是当我尝试执行add函数时,它将无法正常工作。 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. 我还不太了解MYSQL,我认为插入部分做错了,我一次执行多行,但是不确定如何解决。

(Well, The dates , Names etc he gets on the last page, but that works fine) (好吧,他在最后一页上看到了datesNames etc ,但这很好用)

The correct SQL syntax is this one: 正确的SQL语法是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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