解析错误:语法错误,第15行/hipt/sign.php中的意外“if”(T_IF)

时间:2015-01-23 19:20:49

标签: php json get passwords

我正在

  

解析错误:语法错误,第17行/home/rainlikq/public_html/script/signupt.php中的意外'$ username'(T_VARIABLE)用户名声明....

有人可以建议我解决方案

我正在使用它进行注册...... 我正在尝试执行它..... 其实我正在将json数据解析为android设备...

 <?php

$host="localhost";          //replace with database hostname 
$username="rainlikq_rahul";     //replace with database username 
$password="rahul1";             //replace with database password 
$db_name="rainlikq_rainforest";     //replace with database name

 $con=mysql_connect($host, $username, $password)or die("cannot connect"); 



mysql_select_db($db_name)or die("cannot select DB");

 if(isset($_POST['name'], $_POST['password'], $_POST['password2'], $_POST['username'], $_POST['mobile']))
{
 
$username   =   $_POST['username'];
$password   =   $_POST['password'];     
$password2  =   $_POST['password2'];        
$name       =   $_POST['name'];
$mobile     =   $_POST['mobile'];

$result = mysql_query("insert into user(user_email,user_password,name,mobile) values( '$username','$password','$name','$mobile')");
 

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

if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Signed up successfully";
        // echoing JSON response
        echo json_encode($response);
    } else 
    {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

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

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





?>

1 个答案:

答案 0 :(得分:3)

根据您最初发布的问题而不标记为编辑

请参阅:https://stackoverflow.com/revisions/28117175/1

您忘了关闭几个isset()来电:

if (isset($_POST['name']) && isset($_POST['password']) && isset($_POST['password2'] && isset($_POST['username'] && isset($_POST['mobile'])) {
                                                                                 //^ Here                      ^ And here

请使用:

if (isset($_POST['name']) && isset($_POST['password']) && isset($_POST['password2']) && isset($_POST['username']) && isset($_POST['mobile'])) {             

此外,由于您使用AND运算符,您可以在isset()调用中放置多个变量,如下所示:

if(isset($_POST['name'], $_POST['password'], $_POST['password2'], $_POST['username'], $_POST['mobile'])) {

有关isset()的详情,请参阅手册:http://php.net/manual/en/function.isset.php

从那里引用:

  

如果提供了多个参数,则只有在设置了所有参数后,isset()才会返回TRUE。评估从左到右进行,并在遇到未设置的变量时立即停止。

旁注:

1。您使用$username 2次,首先在您的连接中,然后在if语句中将其分配为新的(可能您会遇到此问题)

2。您使用mysql_* API,我建议您使用mysqli_*PDO。因为:从PHP 5.5.0开始,不推荐使用此扩展,因此不建议用于编写新代码,因为将来会将其删除。

3。您使用了运算符<>,它完全没问题,但对于某些人来说,它可能看起来有点奇怪,我会使用!=和您可以在SQL语句中使用的另一个运算符

4。我还建议您在文件顶部添加错误报告(仅限于分段):

<?php
    ini_set("display_errors", 1);
    error_reporting(E_ALL);
?>