mysql - Saving Class to Database in Object Oriented PHP -
i'm new object oriented programming , don't know if i'm doing right. want take data form submitted , add database. database credentials correct.
<?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 `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>
you don't have pass params, because you're declaring them in __construct
well. remove them __construct
. without these params can this:
if(!empty($_post['submit'])) { new student(); }
you can add piece below class declaration , catch post.
your class (in opinion) become like:
<?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 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>"; } } }
Comments
Post a Comment