获取此代码的空白记录

时间:2016-01-04 11:14:14

标签: php

看看这段代码,我在数据库中得到空记录。

$firstname=isset($_POST['firstname'])?mysql_real_escape_string($_POST['firstname']):"";

这一行没有给出名字,但它给出了空白值而不是我试图插入数据库的值。

这段代码有什么不对?

create.php

<?php
    include_once('confi.php');
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
        $firstname=isset($_POST['firstname'])?mysql_real_escape_string($_POST['firstname']):"";
        $lastname=isset($_POST['lastname'])?mysql_real_escape_string($_POST['lastname']):"";
        $email=isset($_POST['email'])?mysql_real_escape_string($_POST['email']):"";
        $password=isset($_POST['password'])?mysql_real_escape_string($_POST['password']):"";
        $status=0;

        $query = "INSERT INTO users (firstname,lastname,email,password,status) VALUES ('$firstname','$lastname','$email','$password','$status')";
        $insert = mysql_query($query);
        if($insert)
        {
            $data=array("result"=>1,"message"=>"successfully added!");
        }
        else
        {
            $data=array("result"=>0,"message"=>"Error!");
        }
    }
    else
    {
        $data=array("result"=>0,"message"=>"Request Method is Wrong!");
    }


    mysql_close($conn);
    /* JSON Response */
    header('Content-type: application/json');
    echo json_encode($data);

?>

confi.php

<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db('tuts_rest', $conn);
?>

1 个答案:

答案 0 :(得分:0)

在您的php文件中添加以下行并删除您已添加的标题访问

if (isset($_SERVER['HTTP_ORIGIN'])) {

        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }

    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') 
    {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))

            header("Access-Control-Allow-Headers:            
    {
    $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
    }