1

Okay so I fixed my mistake but I am still getting an error. I am not use to MySQLi but I would like to help my friend get something working for his site he is making.

    <?php

if(isset($_POST['submit']))
{
$firstname = filter($_POST['firstname']);
$lastname = filter($_POST['lastname']);
$age = filter($_POST['age']);
}

$db = new MySQLi('localhost', 'root', '', 'register');
    if ($db->connect_error) {
            $message = $db->connect_error;
            die($message);
    }


    $sql = 'SELECT * FROM users';
    $result = $db->query($sql);
    if ($db->error) {
            $message = $db->error;
            die($message);
    }

    $db->query("INSERT INTO users (firstname,lastname,age)
    VALUES ('".$firstname."', '".$lastname."', '".$age."')");

    mysqli_close($db);

?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="confirmation.php" method="POST">
First Name:<br />
<input type="text" name="firstname" placeholder="First name" />
Last Name:<br />
<input type="text" name="lastname" />
Age:<br />
<input type="text" name="age"  />
Submit:<br />
<input type="submit" name="submit" />
</form>
</body>
</html>

This is the error we are getting ->

( ! ) Notice: Undefined variable: firstname in C:\wamp\www\website\register.php on line 25
Call Stack
# Time Memory Function Location
1 0.0008 244952 {main}( ) ..\register.php:0

( ! ) Notice: Undefined variable: lastname in C:\wamp\www\website\register.php on line 25
Call Stack
# Time Memory Function Location
1 0.0008 244952 {main}( ) ..\register.php:0

( ! ) Notice: Undefined variable: age in C:\wamp\www\website\register.php on line 25
Call Stack
# Time Memory Function Location
1 0.0008 244952 {main}( ) ..\register.php:0

When I do this in the old MySQL I don't get no such error like this so I am very confused since it is working for me when I do it but with the SQLi I get the error.

3
  • 1
    You're running your query before you assign the values to your variables. This is both obvious and something that doesn't work no matter what API you use. Commented Feb 4, 2015 at 1:02
  • Oh wow I can't believe I missed that yes I see why it wouldn't work at all, thank you John for pointing that out. Commented Feb 4, 2015 at 1:05
  • Can you show what filter() does? Commented Feb 4, 2015 at 1:30

3 Answers 3

1

This code block needs to be first, before any MySQL query attempt.

if(isset($_POST['submit']))
{
    $firstname = filter($_POST['firstname']);
    $lastname = filter($_POST['lastname']);
    $age = filter($_POST['age']);
}

This code block defines the variables used. In your current example, the variables aren't set until after the query attempt - that's why you're getting the error.

Thus:

<?php

if(isset($_POST['submit']))
{
    $firstname = filter($_POST['firstname']);
    $lastname = filter($_POST['lastname']);
    $age = filter($_POST['age']);
    $db = new MySQLi('localhost', 'root', '', 'register');
    if ($db->connect_error) {
            $message = $db->connect_error;
            die($message);
    }


    $sql = 'SELECT * FROM users';
    $result = $db->query($sql);
    if ($db->error) {
            $message = $db->error;
            die($message);
    }

    $db->query("INSERT INTO users (firstname,lastname,age)
     VALUES ('".$firstname."', '".$lastname."', '".$age."')");

    mysqli_close($db);
}

?>
Sign up to request clarification or add additional context in comments.

3 Comments

That's still an error as you're always running the query whether the values are set or not
Yeah I am still getting the error I updated my post or question I hope it helps on explain my error I am getting, its the Value() part
Please see the updated code block from @Sphinxxx - his if() statement properly encapsulates the rest of the query code, so the query is not attempted unless this is a $_POST form submission.
1

You should make sure that the fields actually have data in them.

$required = array(
    'firstname',
    'lastname',
    'age'
);

$missing = array();
foreach($required as $item) {
    if(!isset($_POST[$item}) || empty($_POST[$item])) {
        $missing[] = $item;
    }
}

if(!empty($missing)) {
    die("You need to supply the required fields. (" .implode(", ", $missing). ")");
} else {
    // run the query here.
}

As you can see, we run the $required array to ensure the values are set in the $_POST superglobal and are not empty. As we require them to run this.

I still, however think your filter() function has something to do. Could you supply that?

Comments

0

Give this a shot:

<?php

if(isset($_POST['submit']))
{
$firstname = filter($_POST['firstname']);
$lastname = filter($_POST['lastname']);
$age = filter($_POST['age']);


$db = new MySQLi('localhost', 'root', '', 'register');
    if ($db->connect_error) {
            $message = $db->connect_error;
            die($message);
    }


    $sql = 'SELECT * FROM users';
    $result = $db->query($sql);
    if ($db->error) {
            $message = $db->error;
            die($message);
    }

    $db->query("INSERT INTO users (firstname,lastname,age)
     VALUES ('".$firstname."', '".$lastname."', '".$age."')");

    mysqli_close($db);
}
else { die("No post data was submitted");  }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.