【发布时间】:2021-08-19 03:20:44
【问题描述】:
我创建了两个类:一个名为 index.php 的类,供用户输入数据,例如:姓名、电子邮件、电话号码和地址。 另一个名为 model.php 的类必须将用户键入的信息发送到 MySQL 数据库中。 但是,当用户在图形界面中输入信息,然后单击提交按钮时,本地 MySQL 数据库没有接收到数据。 数据库的名字叫“crud”,crud里面的数据库表的名字叫:“gravacoes”。
请问,谁能帮帮我?
index.php 代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<div class = "container">
<div class = "row">
<div class = "col-md-12 mt-5">
<h1 class = "text-center">PHP OOP CRUD TUTORIAL</h1>
<hr style = "height: 1px; color: black; background-color:black;">
</div>
</div>
<div class = "row">
<div class = "col-md-5 mx-auto">
<?php
include 'model.php';
$model = new Model();
$insert = $model->insert();
?>
<form action = "" method = "post">
<div class = "form-group">
<label for = "">Name</label>
<input type = "text" name = "name" class = "form-control">
</div>
<div class = "form-group">
<label for = "">Email</label>
<input type = "email" name = "email" class = "form-control">
</div>
<div class = "form-group">
<label for = "">Mobile No.</label>
<input type = "text" name = "mobile" class = "form-control">
</div>
<div class = "form-group">
<label for = "">Address</label>
<textarea name ="address" id = "" cols = "" rows = "3" class = "form-control"></textarea>
<br />
</div>
<div class = "form-group">
<button type = "submit" name = "submit" class = "btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
</body>
</html>
model.php的代码:
<?php
class Model {
private $server = "localhost";
private $username ="root";
private $password;
private $db = "crud";
private $conn;
public function __construct(){
try {
$this->conn = new mysqli($this->server, $this->username, $this->password, $this->db);
} catch (Exception $e){
echo "Connection failed". $e->getMessage();
}
}
public function insert() {
if(isset($_POST['submit'])) {
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['mobile']) && isset($_POST['address'])) {
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['mobile']) && !empty($_POST['address'])) {
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$address= $_POST['address'];
$query = "INSERT INTO gravacoes (name, email, mobile, address) VALUES ('$name', '$email', '$mobile', '$address')";
if ($sql = $this->conn->query($query)) {
echo "<script>alert('Success');</script>";
echo "<script>window, location.href = 'index.php';</script>";
} else {
echo "<script>alert('Failed');</script>";
echo "<script>window.location.href='index.php';</script>";
}
} else {
echo "<script>alert('Empty');</script>";
echo "<script>window.location.href = 'index.php';</script>";
}
}
}
}
}
?>
【问题讨论】:
-
我在
index.php中没有看到课程。 -
您在
index.php中没有在提交表单时调用Model类的代码。 -
您的代码对 SQL 注入开放。您应该使用带参数的预处理语句,而不是直接将变量替换到 SQL 字符串中。
-
Barmar,请检查 index.php 中的第 19-23 行: insert(); ?>
-
谢谢,我错过了。将代码混合到 HTML 的中间是令人困惑的。
标签: php html mysql database forms