Duplicate records in MySQL after checking (PHP)

Recently, I get some strange duplicate entries in my MySQL database. The entries in this table are inserted during the PUT request on the PHP page . The table contains 3 fields without external links:

  • manga_id (primary key, automatic growth) - bigint (20);
  • name - varchar (255);
  • manga_cid - varchar (255).

The PHP code is as follows:

class MangaHandler {

private function getMangaFromName($name) {
    $id = $this->generateId($name);
    $mangas = new Query("SELECT * FROM tbl_manga WHERE manga_cid = '" . $this->conn->escapeString($id) . "'", $this->conn);

    if(!$mangas || $mangas->hasError()) {
        logError("getMangaFromName($name): " . $this->conn->getError());
        return null;
    }

    if($mangas->moveNext()) {
        return $mangas->getRow();
    }

    return null;
}

private function addManga($name) {
    $manga_row = null;
    $error = false;

    $cid = $this->generateId($name);

    $sql = sprintf("INSERT INTO tbl_manga(name, manga_cid) VALUES ('%s', '%s')", $this->conn->escapeString($name), $this->conn->escapeString($cid));
    if(!$this->conn->execute($sql))
        $error = true;

    // some more code ...

    if($error) {
        logError("addManga($name): " . $this->conn->getError());
    }

    return $manga_row;
}

public function addMangaSourceAndFollow($name, $url, $source_id, $user_id, $stick_source = false, $stick_lang = 'English') {
        // validate url

        $manga = $this->getMangaFromUrl($url, $source_id);
        if(!$manga) {
            $manga = $this->getMangaFromName($name);
            if(!$manga) $manga = $this->addManga($name);

            // ...

        }

        // ...

        return true;
}
}

class MangaRestService extends CommonRestService 
{
   public function performPut($url, $arguments, $accept, $raw) {
        header('Content-type: application/json');
        header("Cache-Control: no-cache, must-revalidate");

        $json = json_decode($raw, true);

        // some input validation and auth

        $ms = new MangaHandler();

        try {

            $ret = $ms->addMangaSourceAndFollow(null, $json['url'], $source['source_id'], $user['user_id'], $enforce == 1);

            // throw exception if some ret is invalid
            // return proper json response

        } catch(Exception $e) {
            $conn->rollback();
            logError("MangaRestService.performPut($url, [" . implode("; ", $arguments) . "], $accept, $raw): " . $e->getMessage());
            echo RestResponse::getSomeErrorResponse()->toJSON();
        }   
    }
}

$serv = new MangaRestService();
// performs some checks and invokes the appropriate GET, POST, PUT or DELETE method
// in this case it the performPut method above
$serv->handleRawRequest(); 

The manga name is filtered (only alphanumeric characters, underscores and some other characters are allowed) and becomes manga_cid (which must be unique in the table) .

, manga_cid. , , . , , , . manga_cid ( 2). , . , .

, - HTTP- PUT , , INSERT ? , , , , .

MySQL, . , , , , , . , manga_id . , , : -)

, Query:

class Query extends QueryBase
{
    function Query($query, &$conn)
    {
        $this->recordset = array();
        $this->has_error=0;
        $regs = mysqli_query($conn->getConnection(), $query);

        if(!$regs)
        {
            $this->has_error=1;
            return;
        }

        $index = 0;
        $this->current_index=-1;

        while(($row = mysqli_fetch_array($regs, MYSQL_ASSOC)))
        {
            $this->recordset[$index]=$row;
            $index++;
        }

        mysqli_free_result($regs);
    }

    public function moveNext()
    {
        if($this->current_index<(sizeof($this->recordset)-1))
        {
            $this->current_index++;
            return 1;
        }
        else
        return 0;
    }

    public function moveBack()
    {
        if($this->current_index>=1)
        {
            $this->current_index--;
            return 1;
        }
        else
        return 0;
    }

    public function recordCount()
    {
        return sizeof($this->recordset);
    }


    public function get($field)
    {
        return $this->recordset[$this->current_index][$field];
    }

    public function getRow()
    {
        return $this->recordset[$this->current_index];
    }

    public function hasError()
    {
        return $this->has_error;
    }
}

.

+4
3

, , ? , 'put'?

0

, , - , - (, ? image click...)

, .

0

I'm not sure if this will fix this for you, but it would be better to return false; instead of returning "null" on error from your getManageFromName () method.

Another thing to check is your hasErrors () method; maybe sometimes they can return errors?

You might be better off writing it like this:

private function getMangaFromName($name) {
    $id = $this->generateId($name);
    $mangas = new Query("SELECT * FROM tbl_manga WHERE manga_cid = '" . $this->conn->escapeString($id) . "'", $this->conn);

    if($mangas->moveNext()) {
        return $mangas->getRow();
    } else {

           if(!$mangas || $mangas->hasError()) {
                 logError("getMangaFromName($name): " . $this->conn->getError());
           }

           return null;
    }
}

// Then check it as follows:

$manga = $this->getMangaFromName($name);
if ( empty($manga) ) {
    $manga = $this->addManga($name);
}
0
source

All Articles