1

bellow is my code.Error shown is like

fatal error: call to undefined function execute() on line 21,

how could i solve this problem?

<?php 

    include 'config/dbconfig.php';
    include 'lib/function.php';
    include  'helper/helper.php';

    $db = new rootfunc();
    $fm = new formate();

    if(!empty($_POST['name']) or !empty($_POST['email']) or !empty($_POST['password1']) or !empty($_POST['dob']) or !empty($_POST['gender']) ){

        $name = $fm->validation($_POST['name']);
        $email = $fm->validation($_POST['email']);
        $password = $fm->validation($_POST['password1']);
        $dob = $_POST['dob'];
        $gender = $fm->validation($_POST['gender']);


        $query = $pdo->prepare("SELECT * FROM user_table WHERE name = ? AND email = ?");
        $query = execute(array($name,$email));
        $numRow = $query->rowCount();

        if(!$numRow){

            $query = "INSERT INTO user_table (name,email,password,dob,gender) VALUES (?,?,?,?,?)";

            $query = $pdo->prepare($query);
            $query->execute(array($name,$email,$password,$dob,$gender));
            echo "Congrates, please login..";

        }else{

            echo "name and email exist..";

        }



    }


?>
Jay Blanchard
  • 32,731
  • 15
  • 70
  • 112
nur alam
  • 127
  • 2
  • 11
  • 3
    **Never store plain text passwords!** Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure that you [don't escape passwords](http://stackoverflow.com/q/36628418/1011527) or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard May 20 '16 at 17:35
  • 3
    Allow users to use the [passwords / phrases](https://xkcd.com/936/) they desire. [Don't limit passwords.](http://jayblanchard.net/security_fail_passwords.html) – Jay Blanchard May 20 '16 at 17:35
  • ok,but what's the problem on execute pdo method in line number 21. – nur alam May 20 '16 at 17:42
  • The problem is basically that you forgot to use the PDO object and just called `execute()` which, yes you guessed it **is an undefined function** i.e. it does not exist in base PHP – RiggsFolly May 20 '16 at 17:44
  • thanks u pick the right thing......problem solved – nur alam May 20 '16 at 18:02

1 Answers1

0

In both cases you are overwriting $query:

$query = "INSERT INTO user_table (name,email,password,dob,gender) VALUES (?,?,?,?,?)";
$query = $pdo->prepare($query);

You need to give the execution a different variable to hold the object, for example:

$query = "INSERT INTO user_table (name,email,password,dob,gender) VALUES (?,?,?,?,?)";
$result = $pdo->prepare($query);
$result->execute(array($name,$email,$password,$dob,$gender));

In addition you should allow users to use the passwords / phrases they desire. Don't limit passwords.

While you're working with passwords never store them as plain text! Please use PHP's built-in functions to handle password security. If you're using a PHP version less than 5.5 you can use the password_hash() compatibility pack. Make sure that you don't escape passwords or use any other cleansing mechanism on them before hashing. Doing so changes the password and causes unnecessary additional coding.


I also noticed this in your code

$numRow = $query->rowCount();

Since the query is a SELECT query it will not work with rowCount()

From the docs:

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

For SELECT when you are not doing a COUNT() query you can return the number of rows like this after you execute the query;

$rows = $result->fetchAll();
$num_rows = count($rows);

In your case it is not necessary to check the count though - just check to make sure the query executed which is enough to get into your conditional statements.

Community
  • 1
  • 1
Jay Blanchard
  • 32,731
  • 15
  • 70
  • 112
  • problem not solved $query = $pdo->prepare("SELECT * FROM user_table WHERE name = ? AND email = ?"); $result = execute(array($name,$email)); $numRow = $result->rowCount(); – nur alam May 20 '16 at 17:48
  • Please do not dump code in comments. That doesn't tell me how the problem was not solved. – Jay Blanchard May 20 '16 at 17:49
  • @nuralam If it solved your problem, consider accepting the answer. Here's how http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work then return here and do the same with the tick/checkmark till it turns green. This informs the community, that a solution was found. Otherwise, others may think the question is still open and may want to post (more) answers. – Funk Forty Niner May 20 '16 at 18:10