1

I want to insert data given by the user into the database when I use simple mysqli it works perfectly but when I use PDO data is inserted into the database.

<?php

$server = 'localhost';
$username = 'root';
$pass = '';
$database = 'db';
try {
    $conn = new PDO("mysql:host=$server;dbname=$database", $username, $pass);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
$name = '';
$password = '';
$email = '';
$uid = '';

$sql = "INSERT INTO users(name,email,password,userid) VALUES(?,?,?, ?)";
if (isset($_POST['username'])) {
    $name = $_POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];
    $uid = $_POST['uid'];
    $newpass = hash("sha256", $password);
    if ($name != '' && $password != '' && $email != '') {
        $insertUser = $conn->prepare($sql);
        $insertUser->execute($name, $email, $password, $uid);
    }
}

$conn = null;
12
  • 1
    Do you get an error? Commented May 24, 2021 at 10:01
  • @ADyson No I am not getting any error. Commented May 24, 2021 at 10:04
  • 1
    This should give you an error execute($name, $email, $password, $uid) Commented May 24, 2021 at 10:04
  • So please explain us what happens when you run this code. Which line of it gets executed? Commented May 24, 2021 at 10:06
  • 1
    Maybe you have error reporting switched off in PHP then? Because you should definitely be seeing one, as Dharman notes. Either that or the code is not entering one of your if blocks, and therefore execute never runs. Done any debugging? Commented May 24, 2021 at 10:06

1 Answer 1

1

Just use parameters array:

    $sql = "INSERT INTO users(name,email,password,userid) VALUES(?, ?, ?, ?)";
    if(isset($_POST['username']))
    {
        $name = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];
        $uid = $_POST['uid'];
        $newpass = hash("sha256", $password);
        if($name!='' && $password!='' && $email!='')
        {
            $insertUser = $conn->prepare($sql);
            // use ARRAY instead plain variables
            $insertUser->execute([$name, $email, $password,  $uid]);
        }
    }

Execute PHP online

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

5 Comments

what if we want to validate that data that user has entered is not present in database I am using select query in similar manner but unable to monitor the result of select query if you have any idea please share. $checkUser1 = "SELECT * FROM users WHERE email = ?"; $checkUser = $conn->prepare($checkUser); $checktUser->execute($email);
Use $stmt->rowCount(); phpize.online/…
i tried in this way but now I'm unable to insert the data. phpize.online/…
Your code work perfect, look it here: phpize.online/…
done thank you so much for your cooperation

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.