|  | 
	                
  
    | 
	 
        ความซ้ำซ้อนของข้อมูล คำสั่่งตรวจสอบชื่อผู้ใช้ไม่ยอมตรวจสอบและยังบึนทึกลงซ้ำแก้ตรงไหนครับ     |  
    |  |  
 
	
		|  |  |  |  |  
		|  |  | 
          
            | คือผมต้องการทำระบบสมาชิก แต่อยากให้ชื่อผู้ใช้งาน username ไม่ซ้ำกัน เมื่อบันทึกแล้วก็ยังมีการซ้ำกันและเก็บลงฐานข้อมูลอยู่ ผมควรแก้ไขตรวสวนไหมครับ อ๋อ มีแจ้ง ดังภาพ 
  
 Code (PHP)
 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?
if($Username=="" || $Password=="" || $Password2=="" || $Name=="" || $Tel==""){
echo"<center><br><br><br>ข้อมูลไม่ถูกต้อง";
exit();
}elseif($Password != $Password2){
echo"<center><br><br><br><br>รหัสผ่าน ไม่ตรงกัน";
exit();
}
?>
<?
$strSQL = "SELECT * FROM log_admin WHERE Username = '$Username' ";
$result = mysql_db_query($dbname,$sql) ;
$num=mysql_num_rows ($result) ;
if($num>0)
{
		echo "<h3> ชื่อผู้ใช้นี้มีอยู่แล้ว</h3>"; exit();
}
else
?>
<title>ระบบแจ้งซ้อมคอมพิวเตอร์ออนไลน์</title>
<style type="text/css">
<!--
.style1 {	font-size: 24px;
	font-weight: bold;
}
.style2 {color: #006600}
.style4 {	color: #666666;
	font-size: 14px;
}
body {
	background-image: url(pic/cute-wallpaper1-1024-768.jpg);
	background-color: #F7F6F5;
	background-repeat: repeat-x;
}
-->
</style>
<script type="text/JavaScript">
<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//-->
</script>
</head>
<body>
<?
if(strcmp($_POST['code_input'],$_POST['code_hidden'])!=0){ /*ตรวจสอบว่า code ที่ซ่อนมาในฟอร์มกับที่ user กรอกเข้าไปเหมือนกันหรือไม่
***การตรวจสอบความเท่ากันของสตริงนั้นมีอยู่หลายฟังก์ชัน ในที่นี้ผมใช้ strcmp ตรวจสอบโดยไม่สนใจ case แต่ไม่แนะนำให้ใช้รูปแบบ if($str1==$str2) นะครับ เพราะลักษณะการใช้หน่วยความจำของสตริงนั้นไม่ได้คงที่เหมือนตัวเลข ฉะนั้นความผิดพลาดมันเกิดขึ้นได้ครับ*/
echo"Coder not";
}
else{
?>
<form id="form4" name="form4" method="post" action="view_student.php">
  <table width="1312" border="0" align="center">
    <tr>
      <td align="center"><a href="m_admin.php"></a>
        <p> </p>
        <p> </p>
        <table width="365" border="0">
          <tr>
            <td width="355" align="center"><label><span class="style1"><br>
            </span>
            <? 
//ติดต่อโปรแกรมฐานข้อมูล MySQL
              $link=mysql_connect("localhost","root","1234");
              if (!$link)
              {
                         print("ERROR");
              }
                 else
              {
//ติดต่อฐานข้อมูล MySQL ชื่อ test
              mysql_select_db("datahelps",$link);
//เปิด ตารางชื่อ testcom และ เขตข้อมูล ชื่อ name,class,school,today,time,score
$sql="insert into log_admin (Username,Password,Name,Tel,Department,Status) values ('$Username','$Password','$Name','$Tel','$Department','$Status')";
//เพิ่มข้อมูลลง Database
               $res = mysql_query( $sql , $link );
               if ($res==1){
			    ?>
            <img src="pic/Download Database.png" width="64" height="64" />
            <?
                   print("<font color=#ffffff><br>Complate<br>บันทึกข้อมูล  $name เรียบร้อย</font>");
				   ?>
            <meta http-equiv=refresh content=0;URL=addmember.php><?
				  
               }else{
			    ?><img src="pic/Misc-Delete-Database-icon.png" width="64" height="64" border="0" /><?
                  echo"<center><font color=#ffffff><br>มีรหัส $id ในระบบแล้ว</font>";
				  ?><meta http-equiv=refresh content=0;URL=addmember.php><?
				 exit();
				  
				  				  }
//เปิดดูข้อมูล
?></label></td>
          </tr>
          <tr>
            <td align="center"><label><br />
            </label></td>
          </tr>
        </table>
        <p>
          <label></label>
      </p>        <p class="style2"> </p>      <p> </p></td>
    </tr>
  </table>
<? }} ?>
</form>
</body>
</html>
 
 
 Tag : PHP, MySQL, CakePHP
 
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2012-10-08 14:30:20 | By :
                          blackberryone | View :
                          1344 | Reply :
                          2 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |  
		|  |  |  |  |  
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ได้รับค่า $_GET $_POST มารึเปล่าครับ เท่าที่เห็นผมไม่เห็นค่า Stock นะ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-10-08 14:33:32 | By :
                            Ex-[S]i[L]e[N]t |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 |  |