mysql - My OOP PHP method not execute -
i have file 'class_reg.php' contains class 'reg', , methon 'registration' in it, recive input value , insert database via pdo. , have 'dbconfig.php' file, connect database, include 'class_reg.php' file , create object '$user'. , have 'registration.php' file include 'dbconfig.php' file , try run method 'registration' not work. here files:
class_reg.php
<?php class reg{ public $db; function __construct($con){ $this->db = $con; //$con содержит подключение к базе данных } public function registration($username, $pass){ try{ $sql = $this->db->prepare("insert websiteusers ( username, pass, fullname, email ) values ( :username, :pass )"); $sql->bindparam(":username", $username); $sql->bindparam(":pass", $pass); $sql->execute(); return $sql; } catch(pdoexception $e){ echo "error data :". $e->getmessage(); } } public function redirect($url){ header("location: $url"); } } ?>
dbconfig.php
<?php $user = 'root'; $pass = '8169x5it'; try{ $con = new pdo('mysql:host=localhost; dbname=reg_form', $user, $pass); }catch (pdoexception $e){ echo "pod error :".$e->getmessage(); } include_once 'class_reg.php'; $user = new reg($con); ?>
registration.php
<?php require_once 'dbconfig.php'; if(isset($_post['username'])){ $form = $_post; //var_dump($_post); $username = $form[ 'username' ]; $pass = md5($form[ 'pass' ]); if($user->registration($username, $pass)){ $user->redirect("all_users.php"); }else{ echo "0"; } } ?>
so, redirect me file 'all_users.php' display users insert database data not inserting database. tell me i'm doing wrong in code. thank in advance!
most of problems have been answered in comments, try summarize , note few additional things.
the main problem invalid sql noted in comment above @danronmoon have mismatch between number of columns specified , number of values provided. if not entering full name , email values, remove these column specifications insert.
you have typo when trying use bindparam()
noted in comments.
the reason see "proper" redirect behavior two-fold:
i don't see have configured pdo object instance utilize exception error mode, no exception thrown when there error. exception handler not being triggered
your return method should be:
return $sql->execute();
not
return $sql;
as $sql
pdostatement object (assuming fix other problem above , prepare
works properly).
Comments
Post a Comment