使用php mysqli插入失败

时间:2016-12-09 08:43:21

标签: php html mysqli

我创建了一个基于表单的寄存器oop。但是当我按下按钮时,数据不会被存储。并且根本没有错误信息。



<div class="form-group">
                        <?php echo $message; ?>
                    </div>
                    
                    <form action="register.php" method="post">
                        <div class="form-group control-group">
                            <div class="controls">
                                <label>Username:</label>
                                <input type="text" class="form-control" name="username" required data-validation-required-message="Please enter your Username.">
                                <p class="help-block">Ex. oimtrust</p>
                            </div>
                        </div>

                        <div class="control-group form-group">
                            <div class="controls">
                                <label>Password:</label>
                                <input type="password" class="form-control" name="password" required data-validation-required-message="Please enter your Password.">
                                <p class="help-block"></p>
                            </div>
                        </div>

                        <div class="control-group form-group">
                            <div class="controls">
                                <label>Nama Lengkap:</label>
                                <input type="text" class="form-control" name="fullname" required data-validation-required-message="Please enter your Fullname.">
                                <p class="help-block">Ex. Fathur Rohim</p>
                            </div>
                        </div>

                        <div class="control-group form-group">
                            <div class="controls">
                                <label>Jenis Kelamin:</label>
                                <select name="gender" class="form-control">
                                    <option value="">Pilih Gender</option>
                                    <option value="Pria">Pria</option>
                                    <option value="Wanita">Wanita</option>
                                </select>
                            </div>
                        </div>

                        <div class="control-group form-group">
                            <div class="controls">
                                <label>Nomor Telepon:</label>
                                <input type="number" class="form-control" name="phone" required data-validation-required-message="Please enter your Phone.">
                                <p class="help-block">Ex. 08133304****</p>
                            </div>
                        </div>

                        <div class="control-group form-group">
                            <div class="controls">
                                <label>Alamat:</label>
                                <textarea name="address" rows="3" class="form-control" required data-validation-required-message="Please enter your Address.">                                   
                                </textarea>
                                <p class="help-block">Ex. Jl. Supriadi RT:03, RW:04</p>
                            </div>
                        </div>

                        <div class="control-group form-group">
                            <input type="hidden" name="id_role" value="3">
                        </div>

                        <div class="control-group form-group">
                            <div class="controls">
                                <button type="submit" class="btn btn-info form-control">Register</button>
                                <p class="help-block">Already have an account? <a href="login.php">Sign in here</a></p>
                            </div>
                        </div>
                    </form>
&#13;
&#13;
&#13;

上面的代码是html代码。以下代码用于插入数据。

&#13;
&#13;
<?php
	$message = "";

	session_start();
	//Jika sudah login
	if (isset($_SESSION['username'])) {
		header('location:index-owner.php');
		exit();
	}

	//Jika belum login
	if (!empty($POST['username']) && !empty($_POST['password']) && !empty($_POST['fullname'])) {
		$username	= $_POST['username'];
		$password	= $_POST['password'];
		$fullname 	= $_POST['fullname'];
		$gender		= $_POST['gender'];
		$phone		= $_POST['phone'];
		$address 	= $_POST['address'];
		$id_role	= $_POST['id_role'];

		//Enkripsi password
		$encrypt_pass	= md5($password);

		include_once("app/model/crud.php");
		$crud 	= new Crud();

		//Cek user
		$cek 	= $crud->execute("SELECT * FROM tbl_owner WHERE username = '{$username}'");
		if ($cek->num_rows != 0) {
			$message	= "<div class='alert alert-danger alert-dismissable' align='center'>
				Username sudah terdaftar!
			</div>";
		} else {
			//Menyimpan user baru
			$register = $crud->execute("INSERT INTO tbl_owner VALUES (NULL, '{$username}', '{$encrypt_pass}', '{$fullname}', '{$gender}', '{$phone}', '{$address}', '{$id_role}')");

			if ($register) {
				$message	= "<div class='alert alert-success alert-dismissable' align='center'>
					Pendaftaran Sukses! Silahkan <a href='login.php'>Login</a>
				</div>";
			} else {
				$message	= "<div class='alert alert-warning alert-dismissable' align='center'>
					Pendaftaran Gagal!
				</div>";
			}
		}
	}
	
	//Menampilkan Tampilan UI
	include 'app/view/header.php';
	include 'app/view/menu.php';
	include 'app/view/auth/register.php';
	include 'app/view/footer.php'; 
 ?>
&#13;
&#13;
&#13;

结束这是crud类

&#13;
&#13;
<?php 
	/**
	 * crud class
	 */
	class Crud 
	{
	    public function __construct()
	    {
	        include('app/config/database.php');
	        $this->db = new mysqli($hostname, $username, $password, $database);
	    }

	    function execute($query)
	    {
	    	$result = $this->db->query($query);
	    	return $result;
	    }
	}
 ?>
&#13;
&#13;
&#13;

请。帮帮我?

2 个答案:

答案 0 :(得分:-1)

请检查您的IF条件,if (isset($POST['username'])出错。将其更改为

if (isset($_POST['username'])) {}

否则,在View中打印Query并将该查询复制到Database Query manager,然后尝试。然后,您可以清楚地找到此查询的错误。

答案 1 :(得分:-1)

试试吧......

if (isset($_POST['username']) && !empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['fullname'])) {

而不是

if (!empty($POST['username']) && !empty($_POST['password']) && !empty($_POST['fullname'])) {