user3448661
user3448661

Reputation: 17

Inserting form information with php to mysql does not work

I have a problem inserting information into a sql database. The user needs to answer a question and submit that.

 <!DOCTYPE html>
    <html>
    <head>
    <title>Some title</title>
    </head>
    <body>
    <form action="neg.php" method="post">
    <b>Enter a title:</b><br /><input type="text" name"title" /><br />
    <input type="submit" value="I !" />
    </form>
    </body>
    </html>

The php page looks like this:

<?php
/* get all input*/
$connection = mysqli_connect("localhost","X","Y","Z") or die("Some error occurred during connection " . mysqli_error($connection));
$sql="INSERT INTO xyz (title)
VALUES
('$_POST[title]')";
if (!mysqli_query($connection,$sql))
  {
  die('Error: ' . mysqli_error($connection));
  }
echo "1 record added";
?>

Can anyone please help me out here? I'm really stuck, tried a million things, but simply do not see what went wrong. I also do not get an error, so I'm unsure what the problem is. Can anyone please help me out here?

Thanks in advance!

Upvotes: 0

Views: 34

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

EDIT

OP changed INSERT INTO dislike to INSERT INTO xyz from an edit after my submitting this answer, including changing value="I don't want to see this show ever again!" to value="I !"


Original answer from original question:

The reason why your query is not working is because the = is missing in name"title"

Change it to name="title"

You should also consider using prepared statements or PDO.

The method you're using now, is open to SQL injection

I've made it a bit more safer for you doing it the following way:

<?php
/* get all input*/
$connection = mysqli_connect("localhost","X","Y","Z") or die("Some error occurred during connection " . mysqli_error($connection));

$title=mysqli_real_escape_string($connection,$_POST['title']);
$sql="INSERT INTO dislike (title) VALUES ('$title')";
if (!mysqli_query($connection,$sql))
  {
  die('Error: ' . mysqli_error($connection));
  }
echo "1 record added";
?>

HTML rewrite:

<!DOCTYPE html>
    <html>
    <head>
    <title>Dislike series</title>
    </head>
    <body>
        <form action="neg.php" method="post">
        <b>Enter a title:</b><br /><input type="text" name="title" /><br />
        <input type="submit" value="I don't want to see this show ever again!" />
    </form>
    </body>
</html>

Here are a few tutorials on prepared statements that you can study and try:

Here are a few tutorials on PDO:

Upvotes: 1

Related Questions