由于我正在使用PHP连接到MySQL数据库。我尝试连接到数据库,并且同样地,每当我单击表单上的登录按钮时,我想将数据插入到数据库中。但是,当我尝试此代码时,数据库中没有更改显示出来,因此请帮助我。谢谢。
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$conn = new PDO("mysql:host=$servername;dbname=Akalpa", $username, $password); // connection to my sql database
if(isset($_POST['Log In'])){
$username=$_POST['username'];
$query = "Insert into User(username) values('$username')"; //query to insert to db
if (mysql_query($query)) //checking the query
{
echo"<h3>Inserted Successfully</h3>"; //value inserted to db successfully
}
}
?>
mysql_query($query)
替换为$conn->query( $query )
,并查看 Jack M 的答案,因为您应该采取更多安全措施。 - Brian Gottier