ajankuv
ajankuv

Reputation: 497

php insert array based on html form

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;
    }
}

Upvotes: 1

Views: 56

Answers (1)

Jeff
Jeff

Reputation: 6943

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"

Upvotes: 3

Related Questions