简体   繁体   中英

PHP submit showing blank page

I have created a form for users to submit information which gets added to a database but when I click the submit button submit.php appears blank which I assume means there has been some form of error. I can't find any errors myself, hoping someone can.

<?php
$con=mysqli_connect("localhost","tyler1996","Tylerkernick1996","essays");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="INSERT INTO essays (author, email, essaytitle, subject, examboard, essay)
VALUES
       ('$_POST[author]','$_POST[subject]','$_POST[essaytitle]','$_POST[subject]','$_POST[examboard]','$_POST[essay]')";

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?> 

The error you're probably having is embedding $_POST['whatever'] directly in the string. You're also not quoting the key of the $_POST array. You're also vulnerable to injection, as @Fabio said, and you need to turn on error reporting, which will tell you all these things. If you're on apache, this may help. It'll be in a php.ini file somewheres.

add these two lines to the top of your php file:
ini_set("display_errors","on");
error_reporting(E_ALL && ~E_NOTICE);


Also add exit after the mysqli connection failure echo.

And then you need to quote the keys in the $_POST array like this :$_POST[\"subject\"]

Do all of these and then let us know what is the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM