我是面向对象编程的新手,我不知道我是否做得对。我想从提交的表单中获取数据并将其添加到数据库中。数据库凭据是正确的。
<?php
require_once('../layout.php');
require_once('../dbconnect.php');
class student{
public $f_name;
public $l_name;
public $email;
public $state;
public $phone;
public $birthday;
public function __construct($f_name,$l_name,$email,$state,$phone,$birthday) {
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];
$email = $_POST['email'];
$state = $_POST['state'];
$phone = $_POST['phone'];
$birthday = $_POST['birthday'];
$query = 'INSERT INTO `student`(`first_name`, `last_name`, `email`, `state`, `phone`, `birthday`) VALUES ($f_name,$l_name,$email,$state,$phone,$birthday)';
$resultquery = mysqli_query($dbconnect,$query);
if($resultquery) {
echo "<div class='alert alert-success'>dsf</div>";
}
}
}
?>
<form method="post" class="col-md-6 col-md-offset-3">
<input name="f_name" class="form-control">
<input name="l_name" class="form-control">
<input name="email" class="form-control">
<input name="state" class="form-control">
<input name="phone" class="form-control">
<input name="birthday" class="form-control" type="date">
<input name="submit" type="submit" class="btn btn-default">
</form>
答案 0 :(得分:0)
您不必传递所有参数,因为您也在__construct
中声明它们。我会从__construct
删除它们。所以如果没有这些参数,你可以这样做:
if(!empty($_POST['submit'])) {
new student();
}
你可以在类声明下面添加这个片段,它会捕获帖子。
你的课程(在我看来)会变成:
<?php
class student{
public function __construct() {
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];
$email = $_POST['email'];
$state = $_POST['state'];
$phone = $_POST['phone'];
$birthday = $_POST['birthday'];
$query = 'INSERT INTO student(`first_name`, `last_name`, `email`, `state`, `phone`, `birthday`) VALUES ($f_name,$l_name,$email,$state,$phone,$birthday)';
$resultquery = mysqli_query($dbconnect,$query);
if($resultquery) {
echo "<div class='alert alert-success'>dsf</div>";
}
}
}