我想通過PHP在數據庫中插入HTML表單數據。 我無法插入HTML表單數據到數據庫中,我不知道,我在做什麼我的代碼錯了?我想通過PHP在數據庫中插入HTML表單數據,但它沒有發生
這裏是我的代碼:
<?php
$host = 'localhost';
$username = 'root';
$password = '';
$dbname = 'mydb';
$con=mysqli_connect($host, $username, $password,$dbname);
if (!$con)
{
echo "connection failed";
}
if(isset($_POST['submit']))
{
$name = $_POST['username'];
$pass = $_POST['password'];
$sql="insert into mytable1 (name,password) VALUES ('$name', '$pass')";
if(mysqli_query($con,$sql))
{
echo "DATA IS INSERTED INTO DATABASE";
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>SignUpform.com</title>
</head>
<body>
<div id="div1">
<div id="div2">
<h1>Sign up Form</h1>
</div>
<form action="" method="post">
<fieldset>
<legend>Signup form ceredentials</legend>
<p id="p1">Name:</p>
<input id="text1" type="text" placeholder="Enter username" name="username">
<p id="p2">Password:</p>
<input id="pass1" type="password" placeholder="Enter Password" name="password">
<p id="email">Email:</p>
<input id="emailtextfld" type="text" placeholder="Enter Email" name="email">
<p id="company">Company Name:</p>
<input id="companytextfld" type="text" placeholder="Company name" name="company">
<input id="donebtn" type="submit" value="submit" name="submit">
</fieldset>
</form>
</div>
</body>
</html>
你會得到'數據被插入數據庫'嗎?您可以接受SQL注入,參數化您的查詢。你的密碼應該被散列。 – chris85
不,我沒有收到此消息「數據插入數據庫」@ chris85 – Mcolo
那麼會發生什麼,什麼都不顯示?你能否在你的條件上加上'else'來看看是否有失敗? – chris85