cala
cala

Reputation: 1421

PHP form not saving information to mySQL database error

Ok, so I have tested if the database is connected and it is. But, my problem is that the form won't go onto the output.php page to be able to save it to the database, I'm getting a NOT FOUND error. I have included a screenshot of the error, the code seems right to me but I can't figure out why it can't find the page when it's clearly in the FOLDER. Thanks for your time in advance.


form.php file


<!DOCTYPE HTML> 
<html>
<head>
</head>
<body> 

<?php
$fname = $surname = "";
?>

<h2>Fill out user details</h2>
<form action="output.php" method="post" > 
   First Name: <input id="fname" type="text" name="fname" >
   <br><br>
   Surname <input id="surname" type="text" name="surname">
   <br><br>

   <input type="submit" value="Submit">
</form>

</body>
</html>

output.php file

<!DOCTYPE HTML> 
<html>
<head>
</head>
<body>
<?php $fname=$_POST["fname"]; 
$surname=$_POST["surname"];?>

<!--Connect to the database-->
<?php
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully"; ?>

<?php
$sql="INSERT INTO 'user'.'user_tbl'('user_fname','user_surname') VALUES ('$fname','$surname')";
mysqli($conn,$sql);
?>

<?php
header("Location: http://localhost/mysite/");
?>
</body>
</html>

Error

Upvotes: 0

Views: 546

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74220

There are a few things wrong with your code.

You are using the wrong identifiers for your table and columns, being single quotes.

Use bacticks

$sql="INSERT INTO `user`.`user_tbl`(`user_fname`,`user_surname`) VALUES ('$fname','$surname')";

Then there is this line mysqli($conn,$sql); it should be mysqli_query($conn,$sql);

or

if(!$result = $conn->query($sql)){
    die('There was an error running the query [' . $conn->error . ']');
}

Then this

$conn = new mysqli($servername, $username, $password);

there should be a 4th parameter for it being for the database

$conn = new mysqli($servername, $username, $password, $database);

however, I see no variables set for any of these.

Here is an example taken from http://php.net/manual/en/function.mysqli-connect.php

$link = mysqli_connect("myhost","myuser","mypassw","mybd") 
or die("Error " . mysqli_error($link)); 

or using your variables and the one that was missing, and replacing with your own credentials

$servername = "localhost"; // or what your service provider said to use
$username = "username"; // your username
$password = "password"; // if one is needed. Leave blank if no password is needed
$database = "your_database"; // your database name

$conn = new mysqli($servername, $username, $password, $database);

Plus, your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements, they're much safer.

Checking for errors would have signaled the error, if you had used mysqli_query instead of just mysqli

or die(mysqli_error($conn)) to mysqli_query()


However, you are doing what is called "outputting before header" with HTML above your PHP

the header being

header("Location: http://localhost/mysite/");

you need to place your HTML under PHP.

Actually, you don't need the following tags for your SQL/PHP

<!DOCTYPE HTML> 
<html>
<head>
</head>
<body>

and

</body>
</html>
  • Just the PHP/SQL.

Upvotes: 1

Related Questions