0

When I try to add a user to the database with POST a new user is added but all fields are Null.
Any help guys ? Thank you in advance.This is my source code:

if($_SERVER['REQUEST_METHOD'] == "POST")                    
{
 // Get data

$name = isset($_POST['name']) ;

 $email = isset($_POST['email']);

 $password = isset($_POST['password']);

 $status = isset($_POST['status']);

 // Insert data into data base

 $sql = "INSERT INTO users (`name`, `email`, `password`, `status`) VALUES     ('$name', '$email', '$password', '$status')";

 $qur = mysql_query($sql);

if($qur){

$json = array("status" => 1, "msg" => "Done User added!");

}else{

 $json = array("status" => 0, "msg" => "Error adding user!");

 }

}else{

$json = array("status" => 0, "msg" => "Request method not accepted");

}


@mysql_close($conn);

/* Output header */

header('Content-type: application/json');

echo json_encode($json);

**

8
  • 1
    Post your form code also.. Commented Feb 9, 2016 at 10:56
  • Just read through your code and think about how it works - you set the various variables to empty strings if the relevant POST keys are not set. You are seeing empty values in your database, therefor.... Commented Feb 9, 2016 at 10:59
  • Hello steve yes u right and i change my code to be like this but still the same take a look plz Commented Feb 9, 2016 at 11:02
  • $name = isset($_POST['name']) ; $email = isset($_POST['email']); $password = isset($_POST['password']); $status = isset($_POST['status']); // Insert data into data base $sql = "INSERT INTO users (name, email, password, status) VALUES ('$name', '$email', '$password', '$status')"; Commented Feb 9, 2016 at 11:02
  • post your form code. Commented Feb 9, 2016 at 11:11

2 Answers 2

1

isset return only true or false so if you want to insert value you can check it with if condition replace your code with above it will be work fine

    if($_SERVER['REQUEST_METHOD'] == "POST"){
$name = (isset($_POST['name']))?$_POST['name']:'' ;
$email = (isset($_POST['email']))?$_POST['email']:'';
$password = (isset($_POST['password']))?$_POST['password']:'';
$status = (isset($_POST['status']))?$_POST['status']:'';
$sql = "INSERT INTO users (`name`, `email`, `password`, `status`) VALUES     ('$name', '$email', '$password', '$status')";
$qur = mysql_query($sql);
if($qur){
$json = array("status" => 1, "msg" => "Done User added!");
}else{
 $json = array("status" => 0, "msg" => "Error adding user!");
}
}else{
$json = array("status" => 0, "msg" => "Request method not accepted");
}
@mysql_close($conn);
header('Content-type: application/json');
echo json_encode($json);

save this form in html file and check it with this edited example

    <form method="post">
<input type="text" name="name" value="Red Symbol" />
<input type="text" name="email" value="[email protected]" />
<input type="text" name="password" value="chock" />
<input type="text" name="status" value="1" />
<input type="submit" name="submit" value="Submit" />
</form>
Sign up to request clarification or add additional context in comments.

3 Comments

Red Symbol i don't why but still the same problem a new user added but fields are Null again i'm really I'm totally over
please check edited comment and try with test file using above full code
i'm trying to add a user to database over poster plugin i'm not using any html.
0

You are not checking if any of the fields are empty.

You need to do that, and only perform the query if they are not.

You can also restructure your code to avoid nested if/else:

function sendJson($data){

    header('Content-type: application/json');
    echo json_encode($data);
    //stop execution after sending response
    exit;
}
//if not POST request, exit
if($_SERVER['REQUEST_METHOD'] !== "POST") {
    sendJson(["status" => 0, "msg" => "Request method not accepted"]);
}

//default data
$defaults = [
    'name'      =>  false,
    'email'     =>  false,
    'password'  =>  false,
    'status'    =>  false,
];

$input = array_intersect_key(array_merge($defaults, $_POST), $defaults);

//if empty field, exit
if(in_array(false, $input)){
    sendJson(["status" => 0, "msg" => "All fields are required"]);
}

// Insert data into data base
//you REALLY REALLY need to use PDO/MYSQLI with prepared statements, this code is dangerous

$sql = "INSERT INTO users (`name`, `email`, `password`, `status`) VALUES     ('$input[name]', '$input[email]', '$input[password]', '$input[status]')";
$qur = mysql_query($sql);

//if query failed, exit
if(!$qur){
    sendJson(["status" => 0, "msg" => "Error adding user!"]);
}

//if we get here, all is OK
sendJson(["status" => 1, "msg" => "Done User added!"]);

@mysql_close($conn);

5 Comments

i 've tried your solution but always fields are empty i'm testing with poster plugin but nothing again pffff
@sami so what json message do you receive? I have double checked the code, it can not add empty fields to the database
this is the json message { "status": 0 "msg": "All fields are required" } However i'm adding full fields
Right, so then some field was missing, and no data would get added to the database, as expected. What is your issue
i'm cheking my data more times but i don't miss anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.