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

crsMC
  • 635
  • 3
  • 11
  • 24
  • 1
    try using an absolute path in your form... like: `
    – rm-vanda Nov 07 '14 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` – crsMC Nov 07 '14 at 23:55
  • 1
    When trying to execute the query, that `mysqli($conn,$sql);` should be `$conn->query($sql)` – Kypros Nov 08 '14 at 00:01
  • Thanks for that but i'm still getting the error – crsMC Nov 08 '14 at 00:03

1 Answers1

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.
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 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 `connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?>` Thanks again! – crsMC Nov 08 '14 at 00:18
  • 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! – crsMC Nov 08 '14 at 00:23
  • 1
    @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`. – Funk Forty Niner Nov 08 '14 at 00:35
  • 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! – crsMC Nov 08 '14 at 00:37
  • 1
    @HiiiPower If it needs to be blank because there is no password set for it, yes, leave it blank. – Funk Forty Niner Nov 08 '14 at 00:40