简体   繁体   中英

Why isn't this updating the positions in the database when the other one does

So I am trying to change the order of my references under my reference Heading, for I have multiple of them under one, but the problem here is that I cannot change the order of them. If I just put the code straight into phpmyadmin it works but it doesn't seem to work trough here for some reason.

And I have no idea why. I tried try catch but it didn't give me any errors

Here are my methods

public function changeHeadingPositions(array $positions) {

    $count = count($positions);

    $return = '';

    for ($i=1; $i <= $count ; $i++) { 
        $sql = 'UPDATE ' . $this->headingsTable . ' SET position = :pos WHERE headingID = :headingID';
        $sql = $this->connect()->prepare($sql);
        $sql->execute(['pos' => $i, 'headingID' => (int)$positions[$i-1]]);

        $return = $return . ' Otsikon ' . $positions[$i-1] . ' sijainti on nyt ' . $i . '<br>';
    }

    return $return;

}

public function changeReferencePositions(array $positions) {

    $count = count($positions);

    $return = '';

    for ($i = 1; $i <= $count; $i++) {
        $sql = 'UPDATE ' . $this->refsTable . ' SET position = :pos WHERE refID = :refID';
        $sql = $this->connect()->prepare($sql);
        $sql->execute(['pos' => $i, 'refID' => (int)$positions[$i-1]]);

        $return = $return . ' Referenssin ' . $positions[$i-1] . ' sijainti on nyt ' . $i . '<br>';
    }

    return $return;
}

And Here is my code that I use to call it

declare(strict_types =1);
include 'includes/class-autoloader.inc.php';

$array0 = array(4, 3, 2, 1);

$object = new Reference();
echo $object->changeHeadingPositions($array0);

$array1 = array(5, 4, 3, 2, 1);

echo $object->changeReferencePositions($array1);

So what am I doing wrong?

Ok so, I fixed it by adding `` these on the ends of the refsTable which is named as "references" in the database.

So now the code looks like this.

public function changeReferencePositions(array $positions) {

        $count = count($positions);

        $return = '';

        for ($i = 1; $i <= $count; $i++) {

            $sql = 'UPDATE `' . $this->refsTable . '` SET position = :pos WHERE refID = :refID';
            $sql = $this->connect()->prepare($sql);
            $sql->execute(['pos' => $i, 'refID' => (int)$positions[$i-1]]);

            $return = $return . ' Referenssin ' . $positions[$i-1] . ' sijainti on nyt ' . $i . '<br>';

        }

        return $return;
    }

It seems that MySQL didn't like the references without these `` quotes

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