2016-10-08 9 views
0

私はちょうどそのローカルのクエリが正常に働いていたinsert.iコアのPHP insert.iを行っていますcorrect.sameの時間は、コピーし、その下に私のコアphpの挿入クエリが動作しません

<?php 
    $db_host="localhost"; 
    $db_user="username_goes_here"; 
    $db_pass="password_goes_here"; 
    $connection = @mysql_connect($db_host,$db_user,$db_pass) or die("Error connecting to database"); 
    $db = mysql_select_db("abserve_wechat", $connection); 

    $username = $_REQUEST['username']; 
    $phonenumber = $_REQUEST['phonenumber']; 
    $password = $_REQUEST['password']; 
    $email = $_REQUEST['email']; 
    $pass=md5($password); 

    unset($_REQUEST['adminer_version']); 
     //print_r($_REQUEST);exit; 

     $result = mysql_query("SELECT * FROM `abserve_users` WHERE `phonenumber`='$phonenumber'"); 
     if(mysql_num_rows($result) == 1) 
     { 
      $response['message'] = "phonenumber already taken"; 
      echo json_encode($response);exit; 
     }else{ 
      $insert = mysql_query("INSERT INTO `abserve_users` (username,phonenumber,password,email) VALUES ('$username', '$phonenumber','$pass','$email')"); 

     $response['message'] = "Inserted successfully"; 

     echo json_encode($response);exit; 
     } 
?> 
+0

あなたが取得している正確なエラーは何ですか? –

+0

警告:mysql_num_rows()は、パラメータ1がリソースであることを期待しています。/home/abservedemotech/public_html/projects/android/wechatapp/wecant.php 18行目でbooleanを指定しています。 {"message": "Inserted successfully"}しかし、値は – murugesh

+0

あなたのPHP版を挿入していないのですか? –

答えて

0

コードに関するいくつかのことを示します。

1)入力データをサニタイズしていないので、SQLインジェクションに脆弱になります。

このエラーの

$username = $_REQUEST['username']; 
$phonenumber = $_REQUEST['phonenumber']; 
$password = $_REQUEST['password']; 
$email = $_REQUEST['email']; 

2):

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/abservedemotech/public_html/projects/android/wechatapp‌​/wecant.php on line 18 

あなたは$結果がブール値)は(TRUEまたはFALSEであることを意味はmysql_num_rows($結果)のための "ブール" エラーを受信して​​いる場合。 mysql_query()関数(http://php.net/manual/en/function.mysql-query.php)のドキュメントを参照すると、成功した場合にのみリソースを返します。 ON ERRORでは、ブール値を返します。つまり、mysql_query()のSELECT文に問題があります。私はあなたのスクリプト(mysql_select_db()やmysql_connect()など)で問題が実際にはるかに高い可能性があると言っています。おそらくあなたはプロダクション環境でPHP 5.5以上を使用していますか? mysql_query()はPHP 5.5では廃止されて以来、これは知っておくことが重要です。

3)mysql_connect()で@記号を使用しています。つまり、本番環境でmysql_connect()がサポートされているかどうかを確認するのに役立つエラーをすべて抑制しています。このコマンドを実行した後 http://php.net/manual/en/language.operators.errorcontrol.php

0

php Desktop\test_stack.php 

私が得たと行が挿入:

{"message":"Inserted successfully"} 

私が得た二回目の場合:

php Desktop\test_stack.php 
{"message":"phonenumber already taken"} 

チェック要求変数、テーブルフィールドタイプ。

ダンプmysql_error();エラーを取得します。

また、MysqliまたはPDOライブラリを使用してください。(はmysql_num_rows($結果)場合=代わりにこの

の($ result-> NUM_ROWS> 0){ }

場合

はあなたにこのコードを

0

利用ありがとう= 1)

0

問題は、あなたのコードでは、あなたのSQLクエリでは、形成されていません

クエリが正常に

実行する場合$結果に格納された値は、リソースタイプのものであろう3210

$result = mysql_query("SELECT * FROM `abserve_users` WHERE `phonenumber`='$phonenumber'"); 

そうでない場合は、あなたの$ result変数は、ブール値が含まれています。

あなたはvar_dump($ result)を使用できます。結果変数をチェックする。

開発のために次の行を使用する必要があります。

$result = mysql_query("SELECT * FROM `abserve_users` WHERE `phonenumber`='$phonenumber'") or die("query failed to execute"); 

クエリが正常に実行されているかどうかを常に確認する必要があります。あなたのアプリケーションは、何らかのエラーにより毎回死ぬクエリはあなたができたことができcheckquery機能でこの

$query = "SELECT * FROM `abserve_users` WHERE `phonenumber`='$phonenumber'"; 
$result = mysql_query($query) or checkerror($query); 

のように失敗した場合に、カスタム関数を使用しない場合

あなたも、このようにそれを使用することができますhave

function checkerror($query){ 


    echo "<div style='position:fixed;z-index:400;top:20%;border:5px solid red;width:600px;left:0;right:0;margin:auto;padding:20px;background:white;'> 
    <h1>Sorry to disturb you !! But an error is found</h1> 
    <p>The error is regarding the following query :</p> 
    <p>".$query."</p> 
    </div>"; 
    //die(); 

} 

MysqliまたはPdoの使用を開始する他のより良い方法があります。

0

It's better to use MySqli functions instead of MySql functions.this code inserts the record in live properly.

<?php 
$db_host="localhost"; 
$db_user="root"; 
$db_pass=""; 
$db="test"; 
$connection = @mysqli_connect($db_host,$db_user,$db_pass,$db) or die("Error connecting to database"); 
    if(isset($_POST["submit"])) 
{ 
    $sql1="select * from abserve_users where phonenumber='".$_POST["phonenumber"]."' "; 
    $result=mysqli_query($connection,$sql1); 
    if(mysqli_num_rows($result)==1) 
    { 
     $response['message']="phonenumber already taken"; 
     echo json_encode($response);exit; 
    } 
    else 
    { 
     $username=$_POST["username"]; 
     $phonenumber=$_POST["phonenumber"]; 
     $password=$_POST["password"]; 
     $email=$_POST["email"]; 
     $sql2="insert into abserve_users (username,phonenumber,password,email) values ('$username','$phonenumber','".md5($password)."','$email')"; 
     $insert = mysqli_query($connection,$sql2); 
     $response['message']="Inserted successfully"; 
     echo json_encode($response); 
     exit; 
    } 
} 
?> 
<!DOCTYPE html> 
<head> 
    <title>insert_select</title> 
</head> 
<body> 
<form action="#" method="post"> 
    username:<input type="text" name="username"><br><br> 
    phonenumber:<input type="text" name="phonenumber"><br><br> 
    password:<input type="password" name="password"><br><br> 
    email:<input type="email" name="email"><br><br> 
    <input type="submit" name="submit" value="submit"> 
</form> 
</body> 
</html> 
関連する問題