2017-11-09 5 views
-2

$ email, $ password 및 $ address의 세 가지 변수 조회를 처리하는 PHP 코드를 작성하고 있습니다. 사용자는 현재 전자 메일, 암호 및 시스템에서 업데이트하려는 새 주소를 입력합니다. [this is the html form] [1] 쿼리는 if 문의이 부분을 실행합니다. enter code here $ body. = "잘못된 이메일 : $ inputemail";PHP 코드 오류 쿼리

내가 올바른 정보를 입력했지만 어떻게 잘못된 이메일을 말하는 것입니까? 문제가 무엇인지 파악하는 데 어려움을 겪고 있으므로 코딩에 익숙하지 않습니다. 또한, 나는 var_dump($db->errorInfo());를 사용하여 디버깅 그리고 배열을 반환 (크기 = 3)

0 => string '' (length=0) 
    1 => null 
    2 => null 

    <?php 
include_once('support.php'); 
include_once('connect_database.php'); 
//connect_database.php contains your connection/creation of a PDO to connect to your MYSQL db on bmgt406.rhsmith.umd.edu/phpmyadmin 
ini_set("display_errors","1"); 
error_reporting(E_ALL); 


// Initialize $title and $body. 
$title = "Simple Update Request"; 
$body = "<fieldset><legend>" .$title . "</legend>"; 

// Initialize variables with values for the name of the table ($name_of_table) 
// and the 3 fields - email, password, and address using method GET. 
$name_of_table = "requests"; 

$inputemail = $_GET['email']; 
$inputpassword = $_GET['password']; 
$address = $_GET['address']; 
var_dump($db->errorInfo()); 

// Check if the table exists in the db 
if (tableExists($db, $name_of_table)) 
{ 
    // Prepare a SQL select query and bind the 3 variables to email, password, and address fields in your database. 

    $sqlQuery = "SELECT * FROM $name_of_table WHERE email = :email"; 
    $statement1 = $db->prepare($sqlQuery); 

    $statement1->bindValue(':email', $inputemail, PDO::PARAM_STR); 

    // Execute the SQL query and store in $result 
    $result = $statement1->execute(); 

    if (!$result) { 
     // Retrieving records failed. 
     $body .= "Retrieving records from table " . $name_of_table . " failed. <br/>"; 
    } else { 
     // Retrieving records successful. 
     // Retrieve result into $singleRow using fetch(); 
     $singleRow = $statement1->fetch(); 

     if (!$singleRow){ 
      // Invalid email address has been entered. 
      $body .= "Invalid email: $inputemail"; 
     } else { 
      if ($singleRow['password'] != $inputpassword) { 
        // If the password is not the same as $inputpassword, then an "Invalid Password" has been entered. 
       $body .= "Invalid password."; 
        // Get current value from table before UPDATING the user's new address. 
       } else { 
        $address = $singleRow['address']; 

        // Close previous SQl query connection before starting a new query. 
        $statement1->closeCursor(); 

        // Prepare a SQL Query to update the user information and execute the query. 
        $sqlQuery = "UPDATE " . $name_of_table . " SET email= :email, password= :password WHERE address= :address"; 
        $statement1 = $db->prepare($sqlQuery); 
    $statement1->bindValue(':address', $address, PDO::PARAM_STR); 
    $statement1->bindValue(':email', $inputemail, PDO::PARAM_STR); 
    $statement1->bindValue(':password', $inputpassword, PDO::PARAM_STR); 

    // Execute the SQL query and store in $result 
    $result = $statement1->execute(); 
        // If everything is correct, display the UPDATED user information found (firstname, lastname, address, email, plan) 


     } 
    } 

    $statement1->closeCursor(); 

} 
} 

$body .= "<a href=\"index.html\"><input type=\"submit\" value = \"Main Menu\"/></a>"; 
$body .= "</fieldset>"; 

echo generatePage($title,$body); 

?> 
+3

귀하의 검색어는 오직 하나의 변수': email'만을 참조하지만': address',': email' 및': password'를 바인딩합니다. –

+0

'WHERE email = : email' 암호 나 주소가 없습니다. – chris85

+3

** 일반 텍스트 비밀번호는 저장하지 마십시오 **. 대신 ['password_hash()'] (http://us3.php.net/manual/en/function.password-hash.php)와 ['password_verify()'] (http://us3.php.net /manual/en/function.password-verify.php). 5.5 이전 버전의 PHP를 사용하는 경우 MD5 또는 SHA1을 사용하지 말고 암호를 해시하지 마십시오. 대신 [이 호환성 팩] (https://github.com/ircmaxell/password_compat)을 사용할 수 있습니다. –

답변

1

당신의 준비 select 문 참조 만 : 이메일을하지만 모든 3 개 값을 결합하려고 : 이메일 : 주소, : 비밀번호를.

첫 번째 또는 두 번째 -> execute() 문에서 오류가 있습니까? 내 생각에 당신은 첫 번째 execute() 문에서 실패하고있다.

+0

전자 메일 및 암호를 참조하기 위해 문을 수정했으며 해결할 bind 문을 제거했습니다. 조회는 단지 전자 메일과 암호가 db –

+0

에 저장된 값과 일치하는지 확인하기 때문에 전자 메일과 주소를 바인딩합니다. 이제 'set asdress = : address'를 추가했지만 여전히 SELECT 문이 있습니다. 코드를 업데이트 한 후에 오류 메시지가 변경 되었습니까? 사용자가 제공 한 이메일 주소와 일치하는 레코드를 검색하려고하는 것처럼 보입니다. 이 경우 전자 메일 주소와 일치하는 레코드 만 조회하면됩니다. 그래서 당신의 Query 문은 원래와 같아야합니다. address와 : password에 대한 두 개의 bindValue() 문을 제거하기 만하면됩니다. –

0

SELECT 쿼리에서 "주소"또는 "암호"를 바인딩 할 필요가 없습니다. 유효하지 않은 매개 변수 예외가 발생하는 이유는 이러한 매개 변수를 쿼리의 아무 것도 바인딩하지 않았기 때문입니다 (아마 필요하지 않기 때문일 수 있습니다). 다음은 문제 해결해야합니다에 대한 의견 당

$sqlQuery = "SELECT * FROM $name_of_table WHERE email = :email"; 
$statement1 = $db->prepare($sqlQuery); 
$statement1->bindValue(':email', $inputemail, PDO::PARAM_STR); 

// Execute the SQL query and store in $result 
$result = $statement1->execute(); 

"쿼리가 실패하고에서이 코드를 실행을 문 경우"무엇에 대한보다 정확한 아이디어를 얻을 수있는 var_dump($db->errorInfo());를 추가하여 코드를 디버깅 시도 실패.

+0

그러나 선택 쿼리에서 비밀번호 또는 주소를 참조 할 필요가없는 이유는 무엇입니까? 나는 당신의 코드를 시도했지만 작동하지 않았다. 그것은 완전히 빈 백 –

+0

을 반환했습니다. 당신의 선택 쿼리에서': address' 또는': password'를 사용하지 않았기 때문에. 왜 당신은 당신의 select 쿼리에서 이들을 바인딩하고 싶습니까?원하는 정보가이 정보를 업데이트하는 것이라면 삽입 쿼리에서 주소와 암호 만 바인딩하면됩니다. – Freid001

+0

select 쿼리의'*'는'$ name_of_table' 테이블의 모든 것을 가져올 것임을 유의하십시오. – Freid001