  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                ผมกำลังทำระบบสมัครสมาชิก โดยเมื่อสมาชิกทำการกรอกข้อมูลเข้ามาเเล้ว พบว่ามีบางข้อมูลผิดพลาด ก็ให้มีการเก็บค่าข้อมูลที่ถูกต้องไว้ก่อน ซึ่ง code ที่ผมได้เขียนไว้ มันไม่เก็บข้อมูลที่ถูกต้องไว้ครับ เรากวนเเนะนำวิธีเเก้ไขหน่อยครับ 
 
code หน้า Register 
 
Code (PHP) 
<?php
session_start();
require_once 'connect.php';
$errorMessage = (isset($_GET['error']) && $_GET['error'] != '') ? $_GET['error'] : '';
$cus_perfix = (isset($_SESSION['cus_perfix'])) ? $_SESSION['cus_perfix'] : '';
$cus_username = (isset($_SESSION['cus_username'])) ? $_SESSION['cus_username'] : '';
$cus_name = (isset($_SESSION['cus_name'])) ? $_SESSION['cus_name'] : '';
$cus_surname = (isset($_SESSION['cus_surname'])) ? $_SESSION['cus_surname'] : '';
$cus_email = (isset($_SESSION['cus_email'])) ? $_SESSION['cus_email'] : '';
$cus_number = (isset($_SESSION['cus_number'])) ? $_SESSION['cus_number'] : '';
$cus_address = (isset($_SESSION['cus_address'])) ? $_SESSION['cus_address'] : '';
$cus_district = (isset($_SESSION['cus_district'])) ? $_SESSION['cus_district'] : '';
$cus_prefecture = (isset($_SESSION['cus_prefecture'])) ? $_SESSION['cus_prefecture'] : '';
$cus_province = (isset($_SESSION['cus_province'])) ? $_SESSION['cus_province'] : '';
$cus_zip_code = (isset($_SESSION['cus_zip_code'])) ? $_SESSION['cus_zip_code'] : '';
?>
<html>
    <head>
        <meta charset="utf8">
        <title>หน้าเเรก</title>
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
        <script type="text/javascript" src="checkform.js"></script>
    </head>
    <body>     
        <div class="row">
            <div class="col-md-12"> <?php require_once 'headMenu_cus.php'; ?></div>
        </div>
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
            <h1>Siam Stanless</h1>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item ">
                        <a class="nav-link" href="Home.php">หน้าเเรก </a>
                    </li> 
                    <li class="nav-item ">
                        <a class="nav-link" href="Login.php">เข้าสู่ระบบ </a>
                    </li>    
                </ul>
            </div>
        </nav><br><br>
        <div class="container">
            <div class="row">
                <div class="col-md-3"></div>
                <div class="col-md-6">
                    <?php
//ตรวจสอบว่ามี error ส่งมาหรือไม่ ถ้ามีแสดง error ดังกล่าวออกมา
                    if (isset($_GET['error']) && $_GET['error'] != '') {
                        ?>
                        <div class="alert alert-danger" align="center"><?php echo $errorMessage; ?></div>
                        <?php
                    }
                    ?>
                    <div class="alert alert-primary" role="alert" >
                        สมัครสมาชิก
                    </div>
                    <form action="Check_Register.php" method="POST" onsubmit="return checkformregister(this);">
                        <table align="center" cellpadding="0" cellspacing="1" class="table table-hover">
                            <tr> 
                                <td width="200" >คำนำหน้า <span class="label label-warning">*</span></td>
                                <td class=""> <input name="cus_perfix" type="text" class="box" id="cus_perfix"  value="<?php echo $cus_perfix; ?>" size="32" maxlength="32"></td>
                            </tr>
                            <tr> 
                                <td width="200" >Username <span class="label label-warning">*</span></td>
                                <td class=""> <input name="cus_username" type="text" class="box" id="cus_username"  value="<?php echo $cus_username; ?>" size="32" maxlength="32"></td>
                            </tr>
                            <tr> 
                                <td width="200" >ชื่อ <span class="label label-warning">*</span></td>
                                <td class=""> <input name="cus_name" type="text" class="box" id="cus_name"  value="<?php echo $cus_name; ?>" size="32" maxlength="32"></td>
                            </tr>
                            <tr> 
                                <td width="200">นามสกุล <span class="label label-warning">*</span></td>
                                <td class=""> <input name="cus_surname" type="text" class="box" id="cus_surname"  value="<?php echo $cus_surname; ?>" size="32" maxlength="32"></td>
                            </tr>
                            <tr> 
                                <td width="150">รหัสผ่าน <span class="label label-warning">*</span></td>
                                <td><input name="cus_password" type="password" class="box" id="cus_password" size="32" maxlength="32">
                                    <label class="label label-default" style="font-size:0.9em;">* อักษรผสมกับตัวเลข 8 ตัวอักษรขึ้นไป</label>
                                </td>
                            </tr>
                            <tr> 
                                <td width="150">ยืนยันรหัสผ่าน <span class="label label-warning">*</span></td>
                                <td> <input name="cus_confirm_passord" type="password" class="box" id="cus_confirm_passord" value="" size="32" maxlength="32"></td>
                            </tr>
                            <tr> 
                                <td width="150">อีเมล <span class="label label-warning">*</span></td>
                                <td> <input name="cus_email" type="email" class="box" id="cus_email" size="32"  value="<?php echo $cus_email; ?>" maxlength="32" placeholder="[email protected]"></td>
                            </tr> 
                            <tr> 
                                <td width="150">เบอร์โทรศัพท์ <span class="label label-warning">*</span></td>
                                <td> <input name="cus_number" type="tel" class="box" id="cus_number"  value="<?php echo $cus_number; ?>" size="32" maxlength="32"></td>
                            </tr>
                            <tr> 
                                <td width="150">ที่อยู่<span class="label label-warning">*</span></td>
                                <td class=""> <input name="cus_address" type="text" class="box" id="cus_address"  value="<?php echo $cus_address; ?>" size="32" maxlength="50" ></td>
                            </tr>
                            <tr> 
                                <td width="150">ตำบล<span class="label label-warning">*</span></td>
                                <td class=""> <input name="cus_district" type="text" class="box" id="cus_district"  value="<?php echo $cus_district; ?>" size="32" maxlength="32" ></td>
                            </tr>
                            <tr> 
                                <td width="150">อำเภอ<span class="label label-warning">*</span></td>
                                <td class=""> <input name="cus_prefecture" type="text" class="box" id="cus_prefecture"  value="<?php echo $cus_prefecture; ?>" size="32" maxlength="32" ></td>
                            </tr>
                            <tr> 
                                <td width="150">จังหวัด<span class="label label-warning">*</span></td>
                                <td class=""> <input name="cus_province" type="text" class="box" id="cus_province"  value="<?php echo $cus_province; ?>" size="32" maxlength="32" ></td>
                            </tr>
                            <tr> 
                                <td width="150">รหัสไปรษณีย์ <span class="label label-warning">*</span></td>
                                <td> <input name="cus_zip_code" type="text" class="box" id="cus_zip_code" value="<?php echo $cus_zip_code; ?>" size="32" maxlength="32"></td>
                            </tr>
                        </table>
                        <p align="center"> 
                            <input type="submit" value="สมัครสมาชิก" class="btn btn-primary">
                            <input type="button" value="ยกเลิก" onclick="window.location.href = 'Home.php'" class="btn btn-primary">
                        </p>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>
 
 
code หน้า Check_Register 
 
Code (PHP) 
<meta charset="utf8">
<?php
require_once 'connect.php';
$cus_perfix = $_POST['cus_perfix'];
$cus_username = $_POST['cus_username'];
$cus_name = $_POST['cus_name'];
$cus_surname = $_POST['cus_surname'];
$cus_password = $_POST['cus_password'];
$cus_email = $_POST['cus_email'];
$cus_number = $_POST['cus_number'];
$cus_address = $_POST['cus_address'];
$cus_district = $_POST['cus_district'];
$cus_prefecture = $_POST['cus_prefecture'];
$cus_province = $_POST['cus_province'];
$cus_zip_code = $_POST['cus_zip_code'];
$cus_hash = md5($cus_password); // นำตัวเเปร cus_password มาเข้ารหัส เเบบ md5 
$sql = "SELECT * FROM customer WHERE cus_username = '$cus_username' ";
$result = mysqli_query($con, $sql); // นำมา query ข้อมูลเพื่อให้ mysql server ประมวลผล
$sql2 = "SELECT * FROM customer WHERE cus_email = '$cus_email'  "; //ตรวจสอบว่ามีชื่อ Email นี้อยู่ในระบบแล้วหรือไม่
$result2 = mysqli_query($con, $sql2);
if (mysqli_num_rows($result) > 0) {
    
    echo "<script>alert('username ซ้ำ');window.location='Register.php';</script>";
} else if (mysqli_num_rows($result2) > 0) {
     
    echo "<script>alert('อีเมลล์ มีอยู่แล้ว กรุณาเลือกชื่ออื่น');window.location='Register.php';</script>";
} else if (checkPassword($cus_password) == false) { 
    
    echo "<script>alert('!ผิดพลาด รหัสผ่านต้องมีทั้งอักษรและตัวเลข และยาวอย่างน้อย 8 ตัวอักษร');window.location='Register.php';</script>";
//ตรวจสอบว่าอีเมลถูกต้องหรือไม่
} else if (checkEmail($cus_email) == false) {
    echo "<script>alert('!ผิดพลาด คุณกรอกอีเมล์ไม่ถูกต้อง');window.location='Register.php';</script>";
} else if (checkUsername($cus_username) == false && $cus_username != '') {
    echo "<script>alert('!ผิดพลาด คุณกรอก username ไม่ถูกต้อง');window.location='Register.php';</script>";
} else if (checkNumber($cus_number) == false && $cus_number != '') { 
    echo "<script>alert('!ผิดพลาด คุณกรอกเบอร์โทรศัพท์ไม่ถูกต้อง');window.location='Register.php';</script>";
} else if (checkZipcode($cus_zip_code) == false && $cus_zip_code != '') {
    echo "<script>alert('!ผิดพลาด คุณกรอกรหัสไปรษณีย์ไม่ถูกต้อง');window.location='Register.php';</script>";
} else {
    $sql = "INSERT INTO customer (cus_perfix, cus_username, cus_name, cus_surname, cus_password, cus_email, cus_number, cus_address, cus_district, cus_prefecture, cus_province, cus_zip_code) "
            . "VALUES ('$cus_perfix','$cus_username', '$cus_name', '$cus_surname', '$cus_hash', '$cus_email', '$cus_number', '$cus_address', '$cus_district', '$cus_prefecture', '$cus_province'"
            . ", '$cus_zip_code')";
    $result1 = mysqli_query($con, $sql);
    if ($result1) {
        echo "<script>alert('บันทึกข้อมูลเรียบร้อยแล้ว');window.location='Login.php';</script>";
    } else {
        echo "<script>alert('บันทึกข้อมูลไม่สำเร็จ');window.location='Register.php';</script>";
    }
}
function checkPassword($cus_password) { //ขึ้นต้นด้วยตัวอักษรพิมใหญ่หรือเล็ก 
    if (strlen($cus_password) < 3 || !preg_match('/^[A-Za-z0-9]*(?=[A-Za-z0-9]*[0-9])(?=[A-Za-z0-9]*[a-z])(?=[A-Za-z0-9]*[A-Z])[A-Za-z0-9]*$/', $cus_password)) { //ขึ้นต้นด้วยตัวเลขหรือตัวอักษรก็ได้ เเละขึ้นต้นด้วยตัวอักษรจะเป็นตัวเล็กหรือตัวใหญ่ก็ได้
        return false; // ตรวจสอบว่ารหัสผ่านตรงตามเงื่อนไขหรือไม่ หากไม่จะส่งค่า false เเต่ถ้าตรงจะส่งค่า true
    } else {
        return true; 
}
function checkEmail($cus_email) {
    if (preg_match('/\A[a-z0-9]+([-._][a-z0-9]+)*@([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,4}\z/', $cus_email)) {
        return true;
    } else {
        return false;
    }
}
function checkNumber($cus_number) {
    if (strlen($cus_number) != 10 || !preg_match('/(\b[0]{1}?\d{2}|\b[0]{1}?[2]{1})[-.]?(\d{3}[-.]?\d{4}\b|\d{3}[-.]?\d{3}\b)/i', $cus_number)) {
        return false;
    } else {
        return true;
    }
}
function checkZipcode($cus_zip_code) {
    if (!preg_match('/\b[1-9]{1}?\d{3}[0]\b/i', $cus_zip_code)) {
        return false;
    } else {
        return true;
    }
}
?>
 
 
  Tag : PHP, HTML5, JavaScript, Ajax, jQuery               
                        | 
           
          
            | 
			
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2019-02-13 22:48:11 | 
                      By :
                          staff39 | 
                      View :
                          554 | 
                      Reply :
                          1 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  |