我是PHP的新手,我正在尝试制作一个在我的数据库中的表中插入名称,电子邮件和ID的小表单,但它不起作用 index.php:
<form action="getData.php" method="post">
Name
<input type="text" name="name" maxlength="40" value="<?=$name;?>" />
Email
<input type="text" name="email" maxlength="50" value="<?=$email;?>" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
访问getdata.php:
<?php
if($_POST['formSubmit'] == "Submit"){
$name = $_POST['name'];
$email = $_POST['email'];
$servername = "localhost";
$username = "tj_testdb";
$password = "tjtorin04";
$dbname = "tj_testdb";
$conn = new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error){
echo "Connections failed: " . $conn->connect_error;
}
$sql = "INSERT INTO data(id,name,email) VALUES(0,$name,$email)";
if($conn->query($sql)===TRUE){
echo "inserted data";
}
else{
echo "failed";
}
$conn->close();
}
?>
答案 0 :(得分:-1)
如果您的ID自动增加,则无需将其放在SQL行中,HTML代码中的变量(名称和电子邮件)也未定义