Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
<?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
<?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.
Posted
Updated 1-Jun-17 2:24am
v2

1 solution

You are right for these corrections:
$firstName = $_POST['firstName'];
$userName = $_POST['userName'];

Next, look at the flow of your program, you have included 'connect.php' and its code will be executed every time the page is being loaded. What happened then? The two $_POST variables do not exist! That causes the error.
The solution is to use if condition to check for the existence of these two $_POST variables before proceeding with the rest of the PHP code, i.e.
if(isset($_POST['firstName'] && $_POST['userName']){
    $firstName = $_POST['firstName'];
    $userName = $_POST['userName'];
    // other code
}
 
Share this answer
 
v4
Comments
theRealGreenz1 1-Jun-17 23:29pm    
Thanks Peter Leow I will make these changes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900