0

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

4
  • 1
    try using an absolute path in your form... like: <form action="http://localhost/wordpress/wp_admin/output.php" [...] - and verify that path is exactly correct -- and then, I have some other tips for you ^_^ Commented Nov 7, 2014 at 23:46
  • @rm-vanda ok, I have taken the output.php file out of the plugin folder and moved it to the wp-admin folder. I am now getting this error, along with 4-5 of the same errors just on different lines, this is the first one Notice: Undefined index: alldayyes in C:\wamp\www\wordpress\wp-admin\output.php on line 12 Commented Nov 7, 2014 at 23:55
  • 1
    When trying to execute the query, that mysqli($conn,$sql); should be $conn->query($sql) Commented Nov 8, 2014 at 0:01
  • Thanks for that but i'm still getting the error Commented Nov 8, 2014 at 0:03

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Fred, this is very helpful to me. Still, when I clicked the submit button on my form, it brings up the error, NOT FOUND how can this be fixed? I have also checked with seperate code that it connects to the db, here it is below <?php $servername = "localhost"; $username = "root"; $password = ""; // Code to check if your able to connect to the database // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?> Thanks again!
Also, where it says $servername = "yourhost"; $username = "username"; $password = "password"; $database = "your_database"; $conn = new mysqli($servername, $username, $password, $database); $servername = "yourhost"; do I replace the "yourhost" etc, etc. with my details? Bceause for the password field, I don't have one, I left it blank, and username is root!
@HiiiPower You're welcome. Since this seems to be related to Wordpress, I don't know anything about Wordpress. For $servername = "yourhost"; yes it needs to be changed to either localhost or what your service provider has given you for information, if you're on a hosted platform. If it's on your own machine, then it's more than likely to be localhost.
No problem, this really did help, seeing as I only started to create stuff while using php because before this all I used was c#, asp.net, sql server, etc. Anyways, seeing as I set this up on my own machine, I didn't set a password for the database, I have a username = root, and left the password field blank. SHould I leave it blank for the code? Thanks, it's in the answer, thanks again for your help!
@HiiiPower If it needs to be blank because there is no password set for it, yes, leave it blank.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.