Shady Shady - 8 months ago 25
MySQL Question

Php form not sending input to the database and not showing print_r

So i have this code which is supposed to send name and email address to the database. Not only is it not doing that, but in my second line of code where I want it to print_r whatever has been entered I still get a blank screen. Is there a setting to show what errors are occurring in my code that I've got turned off or is this code wrong?

//Only process the form if $_POST isn't empty
if(! empty($_POST)){
print_r($_POST); exit;

//Connect to MYSQL
$mysqli = new mysqli('localhost', 'root','','snippets');

//Check connection
if($mysqli ->connect_error){
die('Connect Error: '. $mysqli->connect_errno . ': ' . $mysqli->connect_error);

//Insert data
$sql = "INSERT INTO user(name, email) VALUES ('{$mysqli->real_escape_string($_POST['name'])}','{$mysqli->real_escape_string($_POST['email'])}' )";
$insert = $mysqli->query($sql);

//Print reponse from MySQL
echo "Success! Row ID: {$mysqli->insert_id}";
die("Error: {$mysqli->errno}:{$mysqli->error}");

//Close connection
$mysqli ->close();
<form method="post" action="">
<input name="name" type="text">
<input name="email" type="email">
<input type="submit" value="Submit Form">


$_POST is not being populated on POST requests. The most likely cause is that your PHP is set to not create the superglobals ($_POST, $_GET...).

The fix may be in your php.ini file. Open that file and look for the setting variables_order (See the docs). I suspect that this has been commented out (or doesn't exist) in your configuration file. Set it to:

variables_order = "EGPCS"

If you're not sure where the active php.ini is located, change your PHP file to have just the code below:

phpinfo(); exit;

A lot of info about your installation will appear. The value of Loaded Configuration File will tell you which file to edit to change your PHP settings.

If all else fails, you can extract the submitted values yourself; it's really simple:

$args = []; //will hold the submitted arguments

From there, just use $args as you would use $_POST. It contains your form inputs. Just be aware that it would also contain the inputs that you'd typically see in $_GET, when a "GET" request is made