Duplicate entries created on simple INSERT

2019-08-31 07:07发布

问题:

OK after 40 years I am trying to move to OO PHP.

Everything going OK except when I do a very simple INSERT then two almost duplicate (id not duplicated) rows are created.

The table consists of a autoincrement INT id, a VARCHAR msg field and an INT image field defaulted to NULL. With every insert I get a duplicate msg. id works fine.

I fear this may be something to do with the way I am using PDO.

Connection established as:

$pdo = new PDO("mysql:host=$hjoyjoy;dbname=$dhaphap", $usadsad, $pborbor);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');

Class Message gets sent a message which is stored by the setMessage method and then uploaded to the DB by the storeMessage method:

class Message 
{

    protected $_message='Message not set';
    public $id =10;

    public function setMessage($mess)
    {
        $this->_message = $mess;
    }

    public function displayMessage()
    {
        return $this->_message;
    }

//need try/catch safety sections
//@@@@@@@@@@@@@@@@@@********************zxzxz

    public function storeMessage()
    { 
        global $pdo;
        $sql = "INSERT message (msg) VALUES ('$this->_message')";
        $s = $pdo->prepare($sql);
        $s->execute();
    }
}// end CLASS "Message".
?>

<?php
$test = new Message;
$test->setMessage("Dumbo!!!");
$test->storeMessage();
$info=$test->displayMessage();
echo "<h2> $info </h2>";
?>

I have been looking at this for hours but darned if I can see what I have done wrong.

标签: php sql oop pdo