1

For the life of me I can't figure out why posting to this page is not working, i have been trying things I have found from all over Stackoverflow and the internet.

The first part works for the "id" section but the update function does not.

PHP message: PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters'

updatecompany.php(21): Fr\LS::updateCompany(Array, NULL)

These are from the error logs. Any help would be deeply appreciated.

if (isset($_POST["id"]) && is_numeric($_POST["id"])) {
    $id = $_POST["id"];
    $vid = \Fr\LS::getCompany("id", $id);
    $vname = \Fr\LS::getCompany("name", $id);
    $vlogo = \Fr\LS::getCompany("logo", $id);
    $vinfo = \Fr\LS::getCompany("info", $id);
    $vsite = \Fr\LS::getCompany("site", $id);
    $vest = \Fr\LS::getCompany("est", $id);
} elseif ( isset($_POST["update"]) ) {
    \Fr\LS::updateCompany(array(
        "name" => $_POST["name"],
        "logo" => $_POST["logo"],
        "info" => $_POST["info"],
        "site" => $_POST["site"],
        "est" => $_POST["est"]
    ),
    $_POST["id"]);
    echo "<center>Company updated!";
    echo "<br><a href='updatecompany.php" . $_POST["id"] ."'>go back</a></center>";
} else {
   die("No server with that id.");
}

public static function updateCompany($toUpdate = array(), $company = null) {
    self::construct();
    if( is_array($toUpdate) && !isset($toUpdate['id']) ) {
        if($company == null) {
            echo "No company ID set!";
        }
        $columns = "";
        foreach($toUpdate as $k => $v) {
            $columns .= "`$k` = :$k, ";
        }
        $columns = substr($columns, 0, -2); // Remove last ","

        $sql = self::$dbh->prepare("SELECT {$columns} FROM companys WHERE `id` = ? ORDER BY `id` LIMIT 1");
        $sql->bindValue(":id", $company);
        foreach($toUpdate as $key => $value) {
            $value = htmlspecialchars($value);
            $sql->bindValue(":$key", $value);
        }
        $sql->execute();
    } else {
        return false;
    }
}
2
  • 1
    What's going on with the column definitions? Also I'd use rtrim over that substr. Commented Jan 6, 2017 at 2:20
  • ..and you don't update at all anywhere!? Commented Jan 6, 2017 at 2:33

1 Answer 1

3

One part of your problem is:

This

"SELECT {$columns} FROM companys WHERE `id` = ? ORDER BY `id` LIMIT 1"

is no update statement.

Change it to

"UPDATE companys SET {$columns} WHERE `id` = :id"
Sign up to request clarification or add additional context in comments.

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.