简体   繁体   中英

PHP Wrong parameter count for mysql_query() - Possible Syntax Error

I've been battling with this for a while, I'm not familiar with PHP syntax, here's my code:

    $eventArray = array($this->ReturnLastRecordId() + 1, $managerid, $title, $description, $category, $address, $location, $startdate, $starttime, $enddate, $endtime, $price, $endofticketdate, $totalseats,  $totalseats);
    $sql = "INSERT INTO event (eventid, managerid, title, description, category, address, location, startdate, starttime, enddate, endtime, price, endofticketdate, totalseats, totalseats)";
    $data->StoreData($eventArray, $sql);

In the function:

public function StoreData($dataArray, $sqlquery)
    {

        include 'config.php';

        $i = count($dataArray);
        echo $i;
        switch ($i) {
            case 15:
            mysql_query($sqlquery . "VALUES (" . '$arraydata[0]' ,   '$arraydata[1]',  '$arraydata[2]',  '$arraydata[3]', '$arraydata[4]', '$arraydata[5]', '$arraydata[6]', '$arraydata[7]', '$arraydata[8]', '$arraydata[9]',  '$arraydata[10]', '$arraydata[11]', '$arraydata[12]',  '$arraydata[13]',  '$arraydata[14]' . ")", $con) or die (mysql_error());
            mysql_close($con);
                break;

        }
    }

Obviously I've checked the number of parameters multiple times, including the database, but still the parameter count get thrown :(.

You are not concatenating the SQL string. Your code passes multiple separate PHP function parameters.

The most lazy fix would be:

 $values = implode("','", $arraydata);

 mysql_query("$sqlquery VALUES ('$values')");

I would advise that you enclose the whole single string that mysql_query expects in double quotes, and then use string interpolation within. Avoid manual . concatenation.

Your concatenation of the sql query is wrong. The commas in your solution are outside of the string, thus interpreted by php as additional paramaters.

It should be something like this:

    mysql_query($sqlquery . " VALUES ('$arraydata[0]' ,   '$arraydata[1]', )

and so on.

You used the concatenation operand . when building your SQL query where you shouldn't have. Give a try with the following:

public function StoreData($dataArray, $sqlquery) {

    include 'config.php';

    $i = count($dataArray);
    echo $i;
    switch ($i) {
        case 15:
            mysql_query($sqlquery . " VALUES ('{$arraydata[0]}', '{$arraydata[1]}', '{$arraydata[2]}', '{$arraydata[3]}', '{$arraydata[4]}', '{$arraydata[5]}', '{$arraydata[6]}', '{$arraydata[7]}', '{$arraydata[8]}', '{$arraydata[9]}', '{$arraydata[10]}', '{$arraydata[11]}', '{$arraydata[12]}', '{$arraydata[13]}', '{$arraydata[14]}')", $con) or die (mysql_error());
            mysql_close($con);
            break;
    }
}

Try with this modification of your code:

    $eventArray = array(($this->ReturnLastRecordId() + 1), $managerid, $title, $description, $category, $address, $location, $startdate, $starttime, $enddate, $endtime, $price, $endofticketdate, $totalseats,  $totalseats);
    $sql = "INSERT INTO event (eventid, managerid, title, description, category, address, location, startdate, starttime, enddate, endtime, price, endofticketdate, totalseats, totalseats)";
    $data->StoreData($eventArray, $sql);

public function StoreData($dataArray, $sqlquery)
    {

        include 'config.php';

        $i = count($dataArray);
        echo $i;
        switch ($i) {
            case 15:
            mysql_query($sqlquery . " VALUES (" . '$arraydata[0]' ,   '$arraydata[1]',  '$arraydata[2]',  '$arraydata[3]', '$arraydata[4]', '$arraydata[5]', '$arraydata[6]', '$arraydata[7]', '$arraydata[8]', '$arraydata[9]',  '$arraydata[10]', '$arraydata[11]', '$arraydata[12]',  '$arraydata[13]',  '$arraydata[14]' . ")", $con) or die (mysql_error());
            mysql_close($con);
                break;

        }
    }

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