2014-03-06 6 views
-1

우리는 PHP를 사용하여 WAMP 서버에 액세스하는 하나의 안드로이드 응용 프로그램을 보유하고 있습니다. Select * 등의 모든 쿼리는 정상적으로 작동합니다. 그러나 특정 조건에서 제대로 작동하지 않는 쿼리 삽입 WAMP에 이미 데이터베이스에 데이터가 있으면 데이터베이스에 새 데이터를 삽입하지 못했습니다. 그러나 WAMP 서버에 기존 데이터가 없으면 쿼리 작업을 성공적으로 삽입하십시오. 다음 PHP 코드는 우리가삽입 쿼리가 WAMP 서버에서 작동하지 않습니다.

<?php 

/* 
* Following code will create a new product row 
* All product details are read from HTTP Post Request 
*/ 

// array for JSON response 
$response = array(); 

// check for required fields 
if (isset($_POST['UserName']) && isset($_POST['UserID']) && isset($_POST['Password']) && isset($_POST['Age'])&& isset($_POST['ContactNumber']) && isset($_POST['expiryDate'])) { 

    $UserName = $_POST['UserName']; 
    $UserID = $_POST['UserID']; 
    $Password = $_POST['Password']; 
    $Age = $_POST['Age']; 
    $ContactNumber = $_POST['ContactNumber']; 
    $expiryDate = $_POST['expiryDate']; 

    // include db connect class 
    require_once __DIR__ . '/db_connect.php'; 

    // connecting to db 
    $db = new DB_CONNECT(); 

    // mysql inserting a new row 
    $result = mysql_query("INSERT INTO normaluser(UserName, UserID, Password,Age,ContactNumber,expiryDate) VALUES('$UserName', '$UserID', '$Password','$Age','$ContactNumber','$expiryDate')"); 

    // check if row inserted or not 
    if ($result) { 
     // successfully inserted into database 
     $response["success"] = 1; 
     $response["message"] = "New user successfully created."; 

     // echoing JSON response 
     echo json_encode($response); 
    } else { 
     // failed to insert row 
     $response["success"] = 0; 
     $response["message"] = "Oops! error is there."; 

     // echoing JSON response 
     echo json_encode($response); 
    } 
} else { 
    // required field is missing 
    $response["success"] = 0; 
    $response["message"] = "Required fields is missing"; 

    // echoing JSON response 
    echo json_encode($response); 
} 
?> 

너희들은 해결이 문제에 대한 사용을 도와주세요 수, 데이터를 삽입하기 위해 사용하고 있습니까?

+2

당신은 오류 검사를하지 않는다 : 당신은 아마 사용자에게 다시 전송 이러한 오류를 싶지 않는 있지만 정확한 오류는 MySQL의에서 반환되는 당신을 말할 것이다 이것은 조금 단순하다. 오류가 무엇인지 보려면'mysql_error()'를 사용하십시오. –

+1

mysql_real_escape_string()을 사용하여 입력 값을 살균하는 것이 좋습니다. 그렇지 않으면 SQL 인젝션이 가능합니다. – frank1fr

+0

[누군가가 'SQL Injection'이라고 했습니까?] (http://xkcd.com/327/) – MisterBla

답변

1

더 이상 사용되지 않는 mysql_ * 사용을 무시하고 데이터의 위생 처리가 부족합니다.

이 사용자가 이미 데이터베이스에 있는지 확인하지 마십시오. 적어도 하나의 열이 고유 또는 기본 키라고 가정하면 동일한 사용자를 두 번 시도하고 만들면이 쿼리가 실패 할 가능성이 높습니다.

하지만 가능한 문제에 상관없이 유용한 오류 처리를 코드에 추가하면 문제의 원인을 정확히 알 수 있습니다.

$result = mysql_query("INSERT INTO normaluser(UserName, UserID, Password,Age,ContactNumber,expiryDate) 
         VALUES('$UserName', '$UserID', '$Password','$Age','$ContactNumber','$expiryDate')"); 

// check if row inserted or not 
if ($result) { 
    // successfully inserted into database 
    $response["success"] = 1; 
    $response["message"] = "New user successfully created."; 

    // echoing JSON response 
    echo json_encode($response); 
} else { 
    // failed to insert row 
    $response["success"] = 0; 
    $response["message"] = 'Database error ' . mysql_errno() . ' ' . mysql_error(); 

    // echoing JSON response 
    echo json_encode($response); 
} 
관련 문제