I have written a very simple PHP program following
this tutorial. I am using XAMPP Control Panel v3.2.2 and in it I am using Apache and MySQL. When I try to submit the for I am getting following error
Cannot POST /connect.php
When I open
localhost/test/connect.php
through a browser, then it shows following error
Fatal error: Function name must be a string in ..\xampp\htdocs\test\connect.php on line 7
The error is isolated to POST only. Read till end
Here is the HTML.
<?php
include('connect.php');
?>
<!DOCTYPE html>
<html>
<title>test PHP</title>
<body>
<form name="user reg" method="post" action="connect.php">
<input type="text" name="firstName" value="">
<input type="text" name="userName" value="">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
And PHP
<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "testdb";
$firstName = $_POST('firstName');
$userName = $_POST('userName');
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
if($conn->connect_error){
die("Connection Failed: ".$conn->connect_error);
}
if(empty($firstName)){
echo "enter the first name";
die();
}
if(empty($userName)){
echo "enter the user name";
die();
}
$sql = "INSERT INTO registeredusers (firstName, userName)
VALUES('$firstName', '$userName')";
if($conn->query($sql) === TRUE){
echo "data has been entered";
}else{
echo "error". $sql."<br>".$conn->error;
}
$conn->close;
?>
What I have tried:
I have also looked into
this it wasn't helpful. After opting suggested changes, as shown,
$firstName = $_POST['firstName'];
$userName = $_POST['userName'];
It gave following error.
Notice: Undefined index: firstName in C:\xampp\htdocs\test\connect.php on line 7
Notice: Undefined index: userName in C:\xampp\htdocs\test\connect.php on line 8
enter the first name
Following a similar
post solution and editing php.ini.
Here it states that editing php.ini file is not needed as XAMPP has all the required permissions needed on Windows.
After removing $_POST from
$firstName = $_POST['firstName'];
$userName = $_POST['userName'];
and setting the variables to some string, I can save data to MySQL
You can follow up
here.