0

My database and PHP code is alright but the problem is that php variable cannot link the input names.

ERROR

Notice: Undefined index: username in C:\xampp\htdocs\site\form.php on line 20

Notice: Undefined index: email in C:\xampp\htdocs\site\form.php on line 21

Notice: Undefined index: password in C:\xampp\htdocs\site\form.php on line 22
Error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'firstname' cannot be null 

form.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email)
    VALUES (:firstname, :lastname, :email)");
    $stmt->bindParam(':firstname', $firstname);
    $stmt->bindParam(':lastname', $lastname);
    $stmt->bindParam(':email', $email);

    // insert a row
    $firstname = $_POST['username'];
    $lastname = $_POST['email'];
    $email = $_POST['password'];
    $stmt->execute();


    echo "New records created successfully";
    }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
$conn = null;
?>
<!DOCTYPE html>
<html>
<title>Sign Up | Log In</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" type="text/css" href="css/form.css">
<body bgcolor="#00dbff">
<div style="padding-left: 45%;margin-top: 2%;"><button class="button">Sign Up</button></div>
<div style="padding-top: 15%; width: 1000px; padding-left: 30%;">

<form method="POST" action="" autocomplete="off">
<div class="w3-container w3-card-4 w3-light-grey">
  <h2>Username:</h2>
  <p>
  <label>Enter Your Username</label>
  <input class="w3-input w3-border w3-round" id="username" name="username" type="text" placeholder="Username....." required pattern="^[A-Za-z0-9]+"></p>
</div>
<div class="w3-container w3-card-4 w3-light-grey">
  <h2>Email:</h2>
  <p>
  <label>Enter Your Email</label>
  <input class="w3-input w3-border w3-round" id="email" name="email" type="text" placeholder="Email....." required pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$"></p>
</div>
<div class="w3-container w3-card-4 w3-light-grey">
  <h2>Password:</h2>
  <p>
  <label>Create a Password</label>
  <input class="w3-input w3-border w3-round" id="password" name="password" type="password" placeholder="Password....." required></p>
</div>
<input class="button1" type="submit" name="submit" style="margin-top: 50px; margin-left: 500px;" value="Next>>">
</form>
</div>
</body>
</html>

please help i have no idea what's the error so its my humble request please don't duplicate it or don't close it i have seen so many examples but i am not getting and i tried all but still i can't find the solution. Thank you all

  • Your prepared statement and form have no corelation whatsoever. You're submitting *username*, *email* and *password*, but expecting *firstname*, *lastname* etc in the prepared statement. Plus, these statements `$firstname = $_POST[...` should be put above the prepared or at least above the `->bindParam(...);` statement. Clearly, you haven't properly done your research. – Rajdeep Paul Aug 12 '17 at 16:12
  • thanks rajdeep for you help but can you please give me proper code or what should i change only some information. – Maunil parikh Aug 12 '17 at 16:14
  • i named the columns as firstname , lastname , email respectively that's why i have written this type of code – Maunil parikh Aug 12 '17 at 16:22
  • Then you need to change the database structure as per your form or vice-versa. You should start off with some basic PHP and PDO tutorials, see [this](https://phpdelusions.net/pdo) and [this](http://php.net/manual/en/pdo.prepared-statements.php). – Rajdeep Paul Aug 12 '17 at 16:25
  • Your code is messy, as others have stated, the posted inputs does not match the content / column names in the database, and also, you do not once check if the form is actually submitted (that $_POST exists on the server)... scrap it, start over, because this is crap. You define the variables you use in your prepared statements, after the prepared statements, so the variables don't exist when you need them, and you do not check once if the $_POST contains anything... At least move the assignments above the query, and do `$firstname = $_POST['username'] ?? null;` and do a check on those for null – junkfoodjunkie Aug 12 '17 at 16:52

0 Answers0