我有 2 个文件,一个名为“index.html”,另一个名为“student.php”。 index.html代码如下所示:
<!DOCTYPE html>
<html>
<head>
<title>insert data in database using mysqli</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="main">
<h1>Insert data into database using mysqli</h1>
<div id="login">
<h2>Student's Form</h2>
<hr/>
<Form Name ="form1" Method ="POST" ACTION = "student.php">
<!--<form action="" method="post"> -->
<label>Student Name :</label>
<input type="text" name="stu_name" id="name" required="required" placeholder="Please Enter Name"/><br /><br />
<label>Student Email :</label>
<input type="email" name="stu_email" id="email" required="required" placeholder="john123@gmail.com"/><br/><br />
<label>Student City :</label>
<input type="text" name="stu_city" id="city" required="required" placeholder="Please Enter Your City"/><br/><br />
<input type="submit" value=" Submit " name="submit"/><br />
</form>
</div>
<!-- Right side div -->
</div>
</div>
</body>
</html>
学生.php:
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "college";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO students (student_name, student_email, student_city)
VALUES ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";
if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('New record created successfully');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}
$conn->close();
}
?>
输入学生姓名、电子邮件和地址后,我单击“提交”按钮。下一页上出现以下错误:无法 POST/http-services/emulator-webserver/ripple/userapp/x/C/xampp/htdocs/xampp/please/www/student.php。我之前测试过数据库连接,应用程序能够连接到数据库,因此这不是问题。我的代码有问题吗?
最佳答案
您需要通过 AJAX 将 html 代码连接到 PHP 脚本。使用 JSON 来传输数据。
本视频介绍了如何使其发挥作用。
关于javascript - intel xdk-将 html 连接到 php 以使用 mysqli 将值保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36103784/