0

我试图在我的 php 代码中从我的应用程序中获取这两个值。但在这样做之前,我正在尝试通过 URL 检查。但我的问题是,如果给出值手册,我得到正确的输出,但是当我通过传递值来检查它时,我得到一个语法错误。任何人都可以帮我解决这个问题。

<?php
$hostname_localhost ="localhost";
$database_localhost ="mobiledb";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);
 $response = array();
mysql_select_db($database_localhost, $localhost);

$day = $_POST['day'];
$Q = $_POST['Qno'];


    // get a product from products table
$result = mysql_query("SELECT $Q FROM `Questions` WHERE `day`='$day'") or die(mysql_error()); 
    //echo $result;

if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["question"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $product = array();


        $product["question".$i] = $row["$Q"];

     $i = $i + 1;

     // push single product into final response array
        array_push($response["question"], $product);


    }


    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No users found";

    // echo no users JSON
    echo json_encode($response);
} 



?>
4

2 回答 2

2

我正在尝试通过 URL 检查

我假设您的意思是您正在尝试访问该网址:

http://localhost/yoursite/yourpage?Qno=5&day=thing

在这种情况下,这些变量将作为$_GET['Qno']和访问$_GET['day']

您可以使用$_REQUEST['Qno']$_REQUEST['day']两种方式接收变量。当然,您的应用程序有很多我什至不会触及的安全漏洞。

于 2013-09-12T23:19:28.917 回答
1

我会尝试逃避你的价值观。可能会修复您的错误,并在一定程度上保护您免受 SQL 注入的影响,您应该谷歌并阅读。

$day = mysql_real_escape_string($_GET['day']);
$Q = mysql_real_escape_string($_GET['Qno']);

在此示例中,我们使用 $_GET 是因为您尝试直接从 URL 获取值。

此外,我们对字符串进行转义以确保我们不会破坏我们的字符串语法并将坏怪物注入您的数据库!

另外:Mysql_ 功能已停止使用,您应该停止使用它。在这里阅读大红框:http: //php.net/manual/en/function.mysql-query.php

于 2013-09-12T23:20:19.487 回答