2014-03-06 2 views
-1

데이터베이스의 테이블에 정보를 보내는 코드가 있습니다. 코드는 다음과 같습니다.내 코드가 데이터베이스에 두 번 입력합니다.

<?php 
//session_start(); 
include("config.php"); 

$alertcityname = $_POST['city']; 
$alerareaname = $_POST['area']; 
$email = $_POST['emailid']; 
$phone = $_POST['phone']; 
$pref = $_POST['alert']; 


    $insertQuery = "insert into fir_forward (username,town,area,email,phone,pref) 
        values ('".$_SESSION['username']."','".$alertcityname."','".$alerareaname."', 
        '".$email."','".$phone."','".$pref."')"; 


    $msg = base64_encode("Registered Successfully"); 


    mysql_query($insertQuery) or die("error in insert record"); 

    if(mysql_query($insertQuery)) 
    { 
      header("location:updatemess.php?msg=$msg");  
    } 
    else 
    { 
     header("location:noupdatemess.php?msg=Not Registered"); 
    } 


?> 

내 문제는 데이터가 두 번 입력되거나 테이블이 두꺼워지는 것입니다.

F_id   username  townarea  phone   pref email 
0000000001  0000000001 [->] 0000000009 [->] 0000725954499 sms [email protected] 
0000000002  0000000001 [->] 0000000009 [->] 0000725954499 sms [email protected] 
0000000003  0000000003 [->] 0000000002 [->] 0000734566778 email [email protected] 
0000000004  0000000003 [->] 0000000002 [->] 0000734566778 email [email protected] 

친절하게 도와 주시면 오류를 표시 할 수 있습니까?

+1

이 질문의 재미있는 부분은 질문을 읽을 때 나는 함수의 두 배를 찾아야한다는 것을 알았습니다 : mysql_query. 그것에 대해 생각해라;) – Pakspul

+0

@ Pakkul - Lolz .. 명확하게 그것은 –

답변

2

mysql_query()을 두 번 실행하고 있기 때문입니다. 하나의 인스턴스를 제거하십시오.

//mysql_query($insertQuery) or die("error in insert record"); //<---- Comment this 

    if(mysql_query($insertQuery)) 
    { 
      header("location:updatemess.php?msg=$msg");  
    } 
    else 

Sidenote

이 (mysql_*) 확장은 PHP 5.5.0 추천되지 않습니다, 그리고 미래에 제거됩니다

. 대신 MySQLi 또는 PDO_MySQL 확장자를 사용해야합니다. PreparedStatements으로 전환하면 SQL 주입 공격을 차단하는 것이 더 좋습니다!

0

exit(); 데이터를 삽입 한 후

if(mysql_query($insertQuery)) 
    { 
     header("location:updatemess.php?msg=$msg"); 
     exit();   
    } 

또한 수정할 수 있으며 변수가 설정되어 있는지 확인할 수 있습니다.

if(isset($alertcityname) && isset($alerareaname) && isset($email) && isset($alertcityname) && isset($phone)&& isset($pref)) 
    { 

      if(mysql_query($insertQuery)) 
       { 

         header("location:updatemess.php?msg=$msg"); 
        exit();  
       } 

     } 
+1

내가 Shankar에 의해 제안되는 것에 따라 1 개의 인스턴스를 삭제했다. – hezi

관련 문제