Why isn't this working

Now I have tried for two days and don’t understand why this isn’t working. I used to do like this but haven’t been working with web stuff for some time and I guess new things happened or I just forgot.
I have tried some scripts I found on the web, but this part isn’t working.

I just want to create a form and post the fields and then insert into my db.
When I write the code like this it is working:

$firstname = 'Mr Mister';

But when I try this it won’t work.

$firstname = $_POST["name"];

So what is wrong here? What should I do to make it work? I’m lost.
And yes, I will sanitize the inputs later before putting them in my tables.

Some possible reasons are:

  1. The method attribute in the form tag is not equal to POST.
  2. there is no input tag with name attribute equal to name in the form.
  3. Form is not sent / sent to the wrong address / whatever issue of the kind

Show us the form code.

Did I say I’m stupid? Of course it was something wrong in the form…
Sorry. Now it’s working. I was looking in the wrong place…

But now I tried to make a form and the INSERT from the same page. But that one isn’t working. Why is that?
Any ideas?

<?php
if (!isset($_POST['submit'])) {

$servername = "localhost";
$username = "myusername";
$password = "password";
$dbname = "mydbname";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$name==$_POST["name"];
$sql = "INSERT INTO test (testa) VALUES ('$name')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
mysqli_close($conn);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>TESTING</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label for="name">Name:</label>
  <input name="name" type="text" id="name" />
  <input type="submit" name="submit" id="submit" value="CLICK" />
</form>
<p>&nbsp;</p>
</body>
</html>
!isset($_POST['submit'])
^ this

What?

You’re checking for that variable to NOT be set (the !) rather for it to be set.

Pretty much this one again.

Aha. Yes, the ! shouldn’t be there of course.
I tried that, but nothing was added and then I removed the == and just put = there.
And now it seems to be working.

I forget things like this when I haven’t been working with this for a long, long time. Probably over a year since I tried last time. I will now try to learn again. Old man. Hard to learn. Over 50 and started with this as a hobby years ago.

Thanks a lot for all your help here.

I hadn’t noticed that, to be fair. == for comparison, = for assignment.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.