2017-12-25 20 views
-2

PHPアプリケーションでは、データベースに記録された電子メール、ユーザー名、および番号でログインできるようにします。 致命的なエラー: 上メンバ関数prepare()に呼び出しリターンでSQL文の準備時にPHPが致命的なエラーを発生する

<?php 
$server = 'localhost'; 
$username = 'default'; 
$password = 'xxxx'; 
$database = 'default'; 

try{ 
    $conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password); 
} catch(PDOException $e){ 
    die("Connection failed: " . $e->getMessage()); 
} 

login.php

<?php 
session_start(); 
if(isset($_SESSION['user_name'])!='') { 
header('Location: index.php'); 
} 
include_once 'init.php'; 
//check if form is submitted 
if (isset($_POST['login'])) { 
$email = $_POST['email']; 
$password = $_POST['password']; 
$records = $conn->prepare('SELECT username,email,number,password FROM users WHERE username = :login OR email = :login OR number = :login'); 
$records->bindParam(':login', $email); 
$records->execute(); 
$results = $records->fetch(PDO::FETCH_ASSOC); 

$message = ''; 

if(count($results) > 0 && password_verify($password, $results['password'])){ 

$gome = $results['username'];$_SESSION['user_name'] = $gome; 
$CookieExpire = 1000; 
$time = 100 * 70 * 48 * 86400; 
$time = time() + $time; 
setcookie('username', '$gome', '$time'); 
setcookie('password', '$password', '$time');header("Location: /"); 

} else { 
$message = 'Sorry, those credentials do not match'; 
} 

} 

?> 

私はこのエラーを取得init.phpしかし、私

非対象物:/var/www/u0377863/public_html/xx.com/dom/login.phpオンライン11:

$records = $conn->prepare('SELECT username,email,number,password FROM users WHERE username = :login OR email = :login OR number = :login'); 
+1

を確認してくださいサイドノート:あなたをインデントすることを学ぶ必要がありますコード。それは今では判りにくいです... – dda

+0

それはおそらく何か間違っています。しかし、あなただけが知ることができる、 – halojoy

+1

$ connは宣言できませんでしたか? –

答えて

-2

は、それはあなたの変数のスコープです

<?php 
 

 
session_start(); 
 

 
if(isset($_SESSION['user_name'])!='') { 
 
\t header('Location: index.php'); 
 
} 
 

 
include_once 'init.php'; 
 

 
//check if form is submitted 
 
if (isset($_POST['login'])) { 
 
\t $email = $_POST['email']; 
 
\t $password = $_POST['password']; 
 
\t $records = $conn->prepare("SELECT username,email,number,password FROM users WHERE (username='$email' OR email = '$email' OR number = '$email')"); 
 
\t $records->execute(); 
 
\t $results = $records->fetch(PDO::FETCH_ASSOC); 
 

 
\t $message = ''; 
 

 
\t if(count($results) > 0 && password_verify($password, $results['password'])){ 
 

 
\t \t $gome = $results['username'];$_SESSION['user_name'] = $gome; 
 
\t \t $CookieExpire = 1000; 
 
\t \t $time = 100 * 70 * 48 * 86400; 
 
\t \t $time = time() + $time; 
 
\t \t setcookie('username', '$gome', '$time'); 
 
\t \t setcookie('password', '$password', '$time');header("Location: /"); 
 

 
\t } else { 
 
\t \t $message = 'Sorry, those credentials do not match'; 
 
\t } 
 

 
} 
 

 
?>

+0

私はあなたのコードを試しましたが、まだ解決していないのは、このエラーがソートできないということですか?それとも他の方法がありますか? – hotway

-3

このコードを試してみてください。

try{ 
    $conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password); 
} catch(PDOException $e){ 
    die("Connection failed: " . $e->getMessage()); 
} 

$conn = null;  
try{ 
    $conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password); 
} catch(PDOException $e){ 
    die("Connection failed: " . $e->getMessage()); 
} 

あなたの$ CONNは、tryブロック内で宣言されるべきであるので、外には見えません。外に初期化することで、どこにでも表示されます。

+0

言語がわからない場合は、お答えしません。あなたが言ったのは完全なごみです。 –

+0

私は言語を知っています。これは最も簡単な解決策ですが、間違いなく最高です。そして、それはエラーについての彼の質問に直接答えるのであって、彼が他のコードと何をしているのかについては答えていない。 –

+2

PHPにブロックスコープはありません。 try-catch内の変数宣言は、その外側に表示されます。 –

-2

1)init.phpコードをlogin.phpに移動して、期待どおりに読み込まれるようにしてください。

2) "new PDO .."の前にグローバル変数$ conn(global $ conn;)を宣言してスコープの問題であるかどうかを確認してください。

3)前の2つのステップが問題に光を当てるしていない場合はPDOがロードされている場合(あなたがそれをチェックするためにhttps://stackoverflow.com/a/6113469/1277044に掲載されている機能を使用することができます)

+0

以下の@m-stangeからの提案は、関数が定義されていないので同様に機能します。 – AlexG

+0

あなたが彼の答えをアップしたのは –

+0

なので、彼を助けることができますか?またはあなたのダウンワード経験を習得していますか? – AlexG

関連する問題