简体   繁体   中英

sql error :SQLSTATE[HY093] while binding params

hi is use this pdo php class https://github.com/indieteq/PHP-MySQL-PDO-Database-Class

i use it for connecting to db and crud until now all jobs works fine

but when i run this code keep getting this error :SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

all of binds works fine except this :

    function generateRandomString()
{
    $length = 5;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i ++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    $key = $randomString;
    $setto = false;
    while ($setto == false) {
                $result = $this->db->query('SELECT `vkey` FROM `polls` WHERE `vkey`=:vkey', array(
                    'vkey' => $key
                ));
        if (count($result) > 0) {
            $randomString = '';
            for ($i = 0; $i < $length; $i ++) {
                $randomString .= $characters[rand(0, $charactersLength - 1)];
            }
            $key = $randomString;
        } else {
            $setto = true;
        }
    }
    echo $key;
    return $key;
}

i try to get a random stirng and check it in db before it use that code or not .

here is my complete code ` function __construct() { parent::__construct(); }

function addpoll($args, $questions)
{
    for ($i = 0; $i < 2 + $questions; $i ++) {
        $args[$i] = parent::escape($args[$i]);
    }
    $args[1] = parent::convertHashtags($args[1]);
    $this->db->bind('title', $args[0]);
    $this->db->bind('description', $args[1]);
    $this->db->bind('answer1', $args[2]);
    $this->db->bind('answer2', $args[2]);
    $param = '';
    if ($questions > 2) {
        for ($i = 3; $i < $questions + 1; $i ++) {
            $param = $param . ':answer' . $i . ',';
            $this->db->bind('answer' . $i, $args[$i]);
        }
        if (10 - $questions > 0) {
            $count = 0;
            while (10 - $questions > $count) {
                $param = $param . ' \'\' ,';
                $count ++;
            }
        }
    } else {

        $count = 0;
        while (10 - $questions > $count) {

            $param = $param . ' \'\' ,';
            $count ++;
        }
    }

    $this->db->bind('subject', $args[$questions + 1]);

    $sql = 'INSERT INTO `polls` (`id`, `title`, `about`, `answer1`, `answer2`, `answer3`, `answer4`, `answer5`, `answer6`, `answer7`, `answer8`, `answer9`, `answer10`, `mode`, `confirmation`, `who`, `password`, `confirmed`, `createdate`, `expiredate`,`subject`,`pkey`) VALUES (NULL,:title, :description ,:answer1 ,:answer2,';
    $param = $param . ' 0 , 0 , 0 , \'\' , 0 ,' . time() . ' ,';

    $settings = time() + ($args[$questions + 3] * 86400) . ',:subject , ';
    $sql = $sql . $param . $settings . '\'' . $this->generateRandomString() . '\'' . ')';
    echo $sql;

    $this->db->query($sql);
}

private function getsub($id)
{
    $result = $this->db->query('SELECT * FROM `subject` WHERE id=:sid ', array(
        'sid' => $id
    ));
    if (count($result) == 1) {
        return $id;
    }

    return 1;
}

function generateRandomString()
{
    $length = 5;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i ++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    $key = $randomString;
    $setto = false;
    while ($setto == false) {
                $result = $this->db->query('SELECT `vkey` FROM `polls` WHERE `id`=:id', array(
                    'id' => $key
                ));
        if (count($result) > 0) {
            $randomString = '';
            for ($i = 0; $i < $length; $i ++) {
                $randomString .= $characters[rand(0, $charactersLength - 1)];
            }
            $key = $randomString;
        } else {
            $setto = true;
        }
    }
    echo $key;
    return $key;
}

` i tried diffrent keywords like :vkey2 or.. and echo all of variables thanks

ok i found the problem problem was in binds if you use this class dont ever try bind variables before the execute just store them in array and ate the end bind with execute i change the code and work good ;)

private $binds=array();
function __construct()
{
    parent::__construct();
}

function addpoll($args, $questions)
{
    for ($i = 0; $i < 2 + $questions; $i ++) {
        $args[$i] = parent::escape($args[$i]);
    }
    $args[1] = parent::convertHashtags($args[1]);

    $this->binds['title']=$args[0];
    $this->binds['description']=$args[1];
    $this->binds['answer1']=$args[2];
    $this->binds['answer2']=$args[3];
    $param = '';
    if ($questions > 2) {
        for ($i = 3; $i < $questions + 1; $i ++) {
            $param = $param . ':answer' . $i . ',';
            $this->binds['answer' . $i]= $args[$i];
        }
        if (10 - $questions > 0) {
            $count = 0;
            while (10 - $questions > $count) {
                $param = $param . ' \'\' ,';
                $count ++;
            }
        }
    } else {

        $count = 0;
        while (10 - $questions > $count) {

            $param = $param . ' \'\' ,';
            $count ++;
        }
    }
    $this->binds['subject']=$args[$questions + 1];
    $this->binds['date']=time();
    $this->binds['todate']=time() + ($args[$questions + 3] * 86400);
    $this->binds['pkey']=$this->generateRandomString();


    $sql = 'INSERT INTO `polls` (`id`, `title`, `about`, `answer1`, `answer2`, `answer3`, `answer4`, `answer5`, `answer6`, `answer7`, `answer8`, `answer9`, `answer10`, `mode`, `confirmation`, `who`, `password`, `confirmed`, `createdate`, `expiredate`,`subject`,`pkey`) VALUES (NULL,:title, :description ,:answer1 ,:answer2,';
    $param = $param . ' 0 , 0 , 0 , \'\' , 0 ,:date ,';
    echo 'test';
    $settings = ':todate ,:subject , ';
    $sql = $sql . $param . $settings . ':pkey)';

    $this->db->query($sql,$this->binds);
}

private function getsub($id)
{
    $result = $this->db->query('SELECT * FROM `subject` WHERE id=:sid ', array(
        'sid' => $id
    ));
    if (count($result) == 1) {
        return $id;
    }

    return 1;
}

function generateRandomString()
{
    $length = 5;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i ++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    $key = $randomString;
    $setto = false;
    while ($setto == false) {
                $result =  $this->db->query('SELECT * FROM `polls` WHERE pkey=:pkey ', array(
        'pkey' => $key
    ));
        if (count($result) > 0) {
            $randomString = '';
            for ($i = 0; $i < $length; $i ++) {
                $randomString .= $characters[rand(0, $charactersLength - 1)];
            }
            $key = $randomString;
        } else {
            $setto = true;
        }
    }

    return $key;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM