1

I have a simple html form set up with one field "name" and then submit.

I'm trying to be able to input name and hit submit, have it go to separate form handling page, which will then use php to input data into my database and table.

I have about 99% correct, it even worked once. Then for some reason it didn't upon my next entry.

My html form is form.html and my form handling page is fh.php

The fh.php page simply references another php page for connection and that's all working fine.

My table is called "articles"

Here's my form.html page code

<html>
<body>
        <form action="fh.php" method="post">

        Name:   <input type="text" name="name">
                <br>
                <input type="submit" value="INSERT">
        </form>
</body>
</html>

and here's my fh.php page:

<?php
include_once('dbconnect.php');
if($debugMode){
    var_dump($_POST);
}
$name = $_POST['name'];

$sql = "INSERT INTO articles (name) VALUES ('$name')";

if(!mysqli_query ($link,$sql))
{
    echo 'Not Inserted';
} 
else 
{
    echo 'Inserted';
}
header("refresh:2; url=form.html");
?>

And then finally, here's FH page will load briefly and say:

Success: A proper connection to MySQL was made! The my_db database is great. Host information: 127.0.0.1 via TCP/IP array(1) { ["name"]=> string(5) "sdfsd" } Not Inserted

Then refreshes and goes back to my form page and the entry isn't made. Any help would be greatly appreciated. My sql db is running on MAMP and is all working fine.

Funk Forty Niner
  • 74,372
  • 15
  • 66
  • 132
Wes Creations
  • 315
  • 2
  • 8
  • anything from `mysqli_error($link)` ? – Funk Forty Niner Dec 14 '18 at 20:41
  • as in `echo 'Not Inserted: ' . mysqli_error($link); exit;` and not using the header, so that it doesn't redirect to see the possible error. – Funk Forty Niner Dec 14 '18 at 20:45
  • 1
    **Note:** The duplicate(s) that was/were used to closed the question with, are to be consulted and tried in order for you to debug your code. This helps to avoid probable countless comments asking for clarification/more code and possibly you never answering to them or expecting someone to spend time putting something together to test it; that isn't their job to do this, as noble as it may be. If you followed the steps "to the letter" and you still don't know how to fix your code or how to interpret the errors, you can update your post by including what those errors were, if any. – Funk Forty Niner Dec 14 '18 at 20:52
  • 1
    @FunkFortyNiner ok yes - that did work and gave me a message saying duplicate key "0". which makes sense because if I delete that record with key "0" it allows me to do it once more. Maybe it's just not set to auto increment? I'm looking for fix now, that you for getting me this far. – Wes Creations Dec 14 '18 at 21:09
  • It could be because of the table not having an AI'd column. – Funk Forty Niner Dec 14 '18 at 21:11
  • @FunkFortyNiner you're amazing. Yes, I had my table set up wrong. Deleted 'id' and redid it making sure to make primary and AI and that did the trick. Seriously thank you so much. – Wes Creations Dec 14 '18 at 21:16
  • You're most welcome, cheers! – Funk Forty Niner Dec 14 '18 at 21:18
  • Oh, just another note and an important one at that. If you are to take this online, I suggest you look into using a prepared statement for this, since you are open to an SQL injection. Consult http://php.net/manual/en/mysqli.prepare.php being the mysqli version. PDO also offers that http://php.net/manual/en/pdo.prepared-statements.php, but that can be a bit complicated to grasp at first. We wouldn't want your database to be compromised or deleted. Stay safe ;-) – Funk Forty Niner Dec 14 '18 at 21:42

0 Answers0