0

so I have a form and some php code to enter info in the database. However when i run the php file i get these errors:

Notice: Undefined index: first in C:\xampp\htdocs\temp\insert.php on line 24

Notice: Undefined index: last in C:\xampp\htdocs\temp\insert.php on line 25

Notice: Undefined index: email in C:\xampp\htdocs\temp\insert.php on line 26

Notice: Undefined index: message in C:\xampp\htdocs\temp\insert.php on line 27

Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'first' cannot be null in C:\xampp\htdocs\temp\insert.php:29 Stack trace: #0 C:\xampp\htdocs\temp\insert.php(29): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\temp\insert.php on line 29

here is my html form:

<form action="insert.php" method="post">
    <label for="first" >Firstname:</label>
    <input type="text" name="first" id="first" />
    <label for="last" >Surname:</label>
    <input type="text" name="last" id="last" />
    <label for="email" >   Email: </label>
    <input type="text" name="email" id="email" />
    <label for="message">Message:</label>
    <textarea name="message" id="message"> Enter your question here and we will get back
    to you as soon as possible </textarea>
    <input type="Submit">
</form> 

and here is my php:

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

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully <br />";
    }
    catch(PDOException $e)
    {
        echo "Connection failed: " . $e->getMessage();
    }

    $query=$conn->prepare("INSERT INTO contacts (first, last, email, message) VALUES(?,?,?,?)");
    $query->bindParam(1, $first);
    $query->bindParam(2, $last);
    $query->bindParam(3, $email);
    $query->bindParam(4, $message);

    $first=$_POST['first'];
    $last=$_POST['last'];
    $email=$_POST['email'];
    $message=$_POST['message'];

    $query->execute();

    $conn = null;

    echo 'Hi '.$_POST['first'].' ' .$_POST['last'] .' thanks for your interest.</br>';
    echo 'We will contact you at '. $_POST['email'].' soon.</br>';
?>
B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • From where do you take POST parameters in-order to pass to your query ? – User123456 Nov 02 '17 at 04:11
  • 1
    Presumably you are not requesting your `insert.php` page via POST. You must submit your form in order for the `$_POST` array to be populated – Phil Nov 02 '17 at 04:12
  • @BRjava lines 24 through 27 – Phil Nov 02 '17 at 04:13
  • First you have to define the variables. otherwise those variables are undefined . Define $first=$_POST['first']; $last=$_POST['last']; $email=$_POST['email']; $message=$_POST['message']; before binding to the query – User123456 Nov 02 '17 at 04:15
  • @BRjava can we please stop this nonsense. There's been two incorrect answers already with this same misconception. [RTFM](http://php.net/manual/pdostatement.bindparam.php) ~ *"the variable is **bound as a reference** and will **only be evaluated** at the time that `PDOStatement::execute()` is called"* – Phil Nov 02 '17 at 04:15
  • @Phil : can you run the code and see for your self. And try defining variables before you accessing the in the bindings. That way you will see for your self. Or use a basic IDE – User123456 Nov 02 '17 at 04:20
  • @BRjava I suggest you try reading some manuals instead. Start here and pay particular attention to *Example #1* ~ http://php.net/manual/pdo.prepared-statements.php – Phil Nov 02 '17 at 04:23
  • how are you using variable in bindParam method before declaring it? – Paritosh Mahale Nov 02 '17 at 04:25
  • @ParitoshMahale **please** read some [documentation](http://php.net/manual/pdostatement.bindparam.php) as well as the previous comments on this question – Phil Nov 02 '17 at 04:25
  • 1
    @Stavros Kasapi , I think you are running the insert.php drectly. – Nandhi Kumar Nov 02 '17 at 04:36

1 Answers1

0

Edit: just recognized that you need to check isset only :) thanks to @phil

try this code:

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

try {
 $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 echo "Connected successfully <br />";
 }
catch(PDOException $e)
 {
 echo "Connection failed: " . $e->getMessage();
 }



$query=$conn->prepare("INSERT INTO contacts (first, last, email, message) VALUES(?,?,?,?)");
$query->bindParam(1, $first);
$query->bindParam(2, $last);
$query->bindParam(3, $email);
$query->bindParam(4, $message);

 $first=isset($_POST['first'])?$_POST['first']:"";
$last=isset($_POST['last'])?$_POST['last']:"";
$email=isset($_POST['email'])?$_POST['email']:"";
$message=isset($_POST['message'])?$_POST['message']:"";
$query->execute();

$conn = null;

echo 'Hi '.$_POST['first'].' ' .$_POST['last'] .' thanks for your interest.</br>';
echo 'We will contact you at '. $_POST['email'].' soon.</br>';
?>
no0ob
  • 335
  • 4
  • 18