3
正如我使用PHP連接到MySQL數據庫。我嘗試連接到數據庫,同樣我想在我的表單上單擊登錄按鈕時將數據插入數據庫。但是當我嘗試這段代碼時沒有顯示任何更改,所以請給予幫助。謝謝你..連接並插入到Mysql數據庫使用php
<?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
}
}
?>
只需用'$ conn-> query($ query)'替換'mysql_query($ query)''看看Jack M的答案,因爲你應該做的更安全。 –