http://wowmoon.ru/1/?option=main&id_descr=5 When adding a comment, the previous entry in the database is duplicated. Tell me how to solve the problem, I apologize for a ton of code earlier, I just don’t know where the error might be.

abstract class ACore { public $pdo; function __construct() { $this->pdo = new PDO("mysql:host=localhost;charset=cp1251;dbname=blog", "root", ""); $this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); } protected function getHeader() { include("templates/header.html"); } protected function getLeftBar() { include("templates/left.html"); } protected function getFooter() { include("templates/footer.html"); } public function getBody() { if($_POST) { $this->obr(); } if($_GET['id_descr']) $this->cods = $this->capcha(); $this->getHeader(); $this->getLeftBar(); $this->getContent(); $this->getFooter(); } } class main extends ACore { protected $id; protected $error; protected $echoERROR; protected $error_comm; protected function capcha() { //Вопросы $q[0] = "Ананас"; $q[1] = "Π‘Π°Π½Π°Π½Ρ‹"; $q[2] = "Арбуз"; $q[3] = "Π―Π±Π»ΠΎΠΊΠΎ"; //Π˜Π·ΠΎΠ±Ρ€Π°ΠΆΠ½ΠΈΡ $imgq[0] = "images/cap1cha.jpg";//ананас $imgq[1] = "images/cap2cha.jpg";//Π±Π°Π½Π°Π½Ρ‹ $imgq[2] = "images/cap3cha.jpg";//Π°Ρ€Π±ΡƒΠ· $imgq[3] = "images/cap4cha.jpg";//яблоко for ($iall=0;$iall<4;$iall++)//Π€ΠΎΡ€ΠΌΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ массива cods("сортировка","Π½Π΅ Π·Π°ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ ΠΊΠΎΠ΄","Π·Π°ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ ΠΊΠΎΠ΄","ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅","вопрос","элСмСнт содСрТит ΠΈΠ½Π΄ΠΈΠΊΠ°Ρ‚ΠΎΡ€, ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½Ρ‹ΠΉ Π»ΠΈ ΠΎΡ‚Π²Π΅Ρ‚") { for($i=0;$i<8;$i++)//Ρ„ΠΎΡ€ΠΌΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΊΠΎΠ΄Π° ΠΈΠ· 8 символов { $simvol = chr(rand(97,122));//Π²Ρ‹Π±ΠΈΡ€Π°Π΅ΠΌ любой английский символ $code[$i] = $simvol;//сохраняСм Π² массив } $sort = rand(1,100);//опрСдСляСм ΠΏΠΎΠ·ΠΈΡ†ΠΈΡŽ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ Π² ΠΊΠ°ΠΏΡ‡Π΅ (сортировка) $code = implode("",$code);//склСиваСм ΠΊΠΎΠ΄ ΠΈΠ· 8-ΠΌΠΈ символов $cods[$iall][0] = $sort;//записываСм Π² массив порядок появлСниС ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΎΠΊ (сортировочный Π½ΠΎΠΌΠ΅Ρ€) $cods[$iall][1] = $code;//записываСм Π½Π΅ Π·Π°ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ $code = md5($code);//ΡˆΠΈΡ„Ρ€ΡƒΠ΅ΠΌ ΠΊΠΎΠ΄ $cods[$iall][2] = $code;//записываСм Π² массив ΡˆΠΈΡ„Ρ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ $cods[$iall][3] = $imgq[$iall];//записываСм Π² массив ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ $cods[$iall][4] = $q[$iall];//записываСм Π² массив вопрос $cods[$iall][5] = "false";//Ρ„ΠΈΠΊΡΠΈΡ€ΡƒΡŽΡ‚ΡΡ Ρ‡Ρ‚ΠΎ всС ΠΎΡ‚Π²Π΅Ρ‚Ρ‹ Π½Π΅ ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½Ρ‹Π΅, ΠΌΡ‹ Π΅Ρ‰Π΅ Π½Π΅ Π²Ρ‹Π±Ρ€Π°Π»ΠΈ ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½Ρ‹ΠΉ =) unset($code);//ΡƒΠ½ΠΈΡ‡Ρ‚ΠΎΠΆΠ°Π΅ΠΌ ΠΊΠΎΠ΄ } rsort($cods);//сортируСм массив $truepars = rand(0,3);//Π²Ρ‹Π±ΠΈΡ€Π°Π΅ΠΌ ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½Ρ‹ΠΉ ΠΎΡ‚Π²Π΅Ρ‚ (ΠΈΠ· 4-Ρ… ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΎΠΊ) $cods[$truepars][5] = "true";//мСняСм Ρƒ ΠΎΠ΄Π½ΠΎΠ³ΠΎ элСмСнта массива ΠΈΠ½Π΄ΠΈΠΊΠ°Ρ‚ΠΎΡ€ с false Π½Π° true. Π’Π΅ΠΌ самым Π²Ρ‹Π±ΠΈΡ€Π°Π΅ΠΌ элСмСнт массива содСрТащий ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½Ρ‹ΠΉ ΠΊΠΎΠ΄ ΠΈ ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½Ρ‹ΠΉ вопрос session_start();//ΠΎΡ‚ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ сСссию if($_SESSION['code'])unset($_SESSION['code']);//Ссли ΠΊΠΎΠ΄ Π² сСссии сущСствуСт Ρ‚ΠΎ ΡƒΠ½ΠΈΡ‡Ρ‚ΠΎΠΆΠ°Π΅ΠΌ Π΅Π³ΠΎ $_SESSION['code'] = $cods[$truepars][2];//записываСм ΡˆΠΈΡ„Ρ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² сСссию return $cods; } protected function getComm($error) { $stmt = $this->pdo->prepare("SELECT * FROM comm WHERE descript='$this->id' ORDER BY id DESC"); $stmt->execute(); $row = $stmt->fetch(); $sm_read_comm = file("templates/comm.html"); $sm_read_comm = implode("",$sm_read_comm); if( $row!="" ) { while( $row = $stmt->fetch() ) { printf($sm_read_comm,$row['author'],$row['date_comm'],$row['text']); } } else echo "<p>ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠ΅Π² Π½Π΅Ρ‚, Π²Ρ‹ Π±ΡƒΠ΄Π΅Ρ‚Π΅ ΠΏΠ΅Ρ€Π²Ρ‹ΠΌ</p>"; $form = file("templates/comm_form.html"); $form = implode("",$form); if($error != "")//Ссли Π΅ΡΡ‚ΡŒ ошибки { $error = explode("|",$error); $this->echoERROR .= "<p style='color:red;margin:0px;'>ΠžΠ±Π½Π°Ρ€ΡƒΠΆΠ΅Π½Ρ‹ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ ошибки:</p>"; for($i=0;isset($error[$i]);$i++) { if($error[$i] != "")$this->echoERROR .= "<p style='color:red;margin:0px;'>>$error[$i]</p>"; } $form = sprintf($form,$this->echoERROR); } else $form = sprintf($form,""); for($i=0;$i<4;$i++) { $form = str_replace("[_code".$i."]",$this->cods[$i][1],$form);//вставляСм 4 ΠΊΠΎΠ΄Π° Π² Ρ„ΠΎΡ€ΠΌΡƒ $form = str_replace("[_img".$i."]",$this->cods[$i][3],$form);//вставляСм 4 изобраТСния Π² Ρ„ΠΎΡ€ΠΌΡƒ if($this->cods[$i][5] == "true")$form = str_replace("[_q]",$this->cods[$i][4],$form);//Π²ΠΊΠ»Π΅ΠΈΠ²Π°Π΅ΠΌ вопрос Π² Ρ„ΠΎΡ€ΠΌΡƒ } //ΠΊΠ°ΠΏΡ‡Π° if($chpu == 0)$link = "index.php?option=main&id_descr=".$this->id;//...динамичСская else//Если ЧПУ Π²ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΎ Ρ‚ΠΎ { //Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ имя ЧПУ ΠΈΠ· Π±Π°Π·Ρ‹ Π΄Π°Π½Π½Ρ‹Ρ… ΠΏΠΎ id поста $stmt = $this->pdo->prepare("SELECT nameurl FROM pop WHERE id='$descript'"); $stmt->execute(); $link = "descript/".$myrow_link['nameurl'];//склСиваСм ЧПУ } $form = str_replace("[_action]",$link,$form);//ссылка ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠ° $form = str_replace("[_id]",$this->id,$form); echo $form; } //--------------ΠžΠ‘Π ΠΠ‘ΠžΠ’Π§Π˜Πš ΠšΠžΠœΠœΠ•Π’ΠΠ Π˜Π•Π’ protected function obr() { if(isset($_POST['id_comm']))$id_comm = $_POST['id_comm']; if(isset($_POST['txt_comm']))$txt_comm = $_POST['txt_comm']; if(isset($_POST['author_comm']))$author_comm = $_POST['author_comm']; if(isset($_POST['code_comm']))$code_comm = $_POST['code_comm']; $date_day = date("d"); $date_month = date("m"); $date_year = date("Y"); $date_time = date("H:i"); $date_comm = $date_day."/".$date_month."/".$date_year." ".$date_time; if($id_comm & $txt_comm & $author_comm & $code_comm) { $id_comm = htmlspecialchars($id_comm); $txt_comm = htmlspecialchars($txt_comm); if($code_comm != "") { session_start(); if( md5($code_comm) != $_SESSION['code'] )$this->error_comm .= "Π’Ρ‹ Π²Ρ‹Π±Ρ€Π°Π»ΠΈ Π½Π΅ Ρ‚Ρƒ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ!|"; unset($_SESSION['code']); session_destroy(); } else $error_comm .= "Π’Ρ‹ Π½Π΅ ΠΏΠΎΠ΄Ρ‚Π²Π΅Ρ€Π΄ΠΈΠ»ΠΈ, Ρ‡Ρ‚ΠΎ Π’Ρ‹ Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊ|"; if($author_comm != "" AND $author_comm != "Автор*") { if(!preg_match("/^[-_0-9a-zA-ZΠ°-яА-Π― ]+$/s",$author_comm))$this->error_comm .= "НС ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½Ρ‹ΠΉ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ поля 'Автор'|"; if(mb_strlen($author_comm) > 25)$this->error_comm .= "Π’ ΠΏΠΎΠ»Π΅ 'Автор' слишком ΠΌΠ½ΠΎΠ³ΠΎ символов|"; } else $this->error_comm .= "Π’Ρ‹ Π½Π΅ Π·Π°ΠΏΠΎΠ»Π½ΠΈΠ»ΠΈ ΠΏΠΎΠ»Π΅ 'Автор'|"; if($txt_comm == "" || $txt_comm == "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ тСкст*")$this->error_comm .="Π’Ρ‹ Π½Π΅ Π²Π²Π΅Π»ΠΈ тСкст!|"; if(!isset($this->error_comm)) { $id_comm = str_replace("'","&#039",$id_comm); $txt_comm = str_replace("'","&#039",$txt_comm); $txt_comm = str_replace("\n","<BR>",$txt_comm); //ДобавляСм сообщСниС Π² Π±Π°Π·Ρƒ Π΄Π°Π½Π½Ρ‹Ρ… $stmt = $this->pdo->prepare("INSERT INTO comm (author,text,date_comm,descript) VALUES ('$author_comm','$txt_comm','$date_comm','$id_comm')"); $stmt->execute(); if( !$stmt->execute() ) echo "НС ΡƒΠ΄Π°Π»ΠΎΡΡŒ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ Π² Π‘Π”=("; header("location: ".getenv('HTTP_REFERER')); exit; } } } protected function getContent() { if($_GET['id_descr']) { $this->id = $_GET['id_descr']; $stmt = $this->pdo->prepare("SELECT * FROM pop WHERE id = '$this->id'"); $stmt->execute(); $row = $stmt->fetch(); if( $row!="" ) { $sm_read = file("templates/text.html"); $sm_read = implode("",$sm_read); echo "<div id='content'>"; printf($sm_read,$row['img'],$row['url'],$row['title'],$row['price'],$row['article'],$row['text']); //--------Π²Ρ‹Π²ΠΎΠ΄ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠ΅Π² self::getComm($this->error_comm); echo "</div>"; } else {echo "<div id='content'><h4 align='center'>Π’ΠΎΠ²Π°Ρ€Π° с Π΄Π°Π½Π½Ρ‹ΠΌ id Π½Π΅ сущСствуСт.</div></h4>";}; } else { $sm_read_main = file("templates/main.html"); $sm_read_main = implode("",$sm_read_main); echo $sm_read_main; $sm_read_pop = file("templates/pop.html"); $sm_read_pop = implode("",$sm_read_pop); $stmt = $this->pdo->prepare("SELECT * FROM pop"); if(!$stmt->execute()) echo "<div id='content'><h4 align='center'>НСт записСй Π² Π‘Π”.</div></h4>"; while( $row = $stmt->fetch() ) { printf($sm_read_pop,$row['id'],$row['img'],$row['id'],$row['title'],$row['price']); } echo ' </div> </div> </div>'; } } 

    2 answers 2

    everything is simple :)

     $stmt->execute(); if( !$stmt->execute() ) echo "НС ΡƒΠ΄Π°Π»ΠΎΡΡŒ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ Π² Π‘Π”=("; 

    here you write comments 2 times :)

     $returncode = $stmt->execute(); if( !$returncode ) echo "НС ΡƒΠ΄Π°Π»ΠΎΡΡŒ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ Π² Π‘Π”=("; 

    So it will be more correct

    • Corrected. Now, when adding a comment, the previous one is added. (((((( - Andret
    • one
      Clean your cookies, clean the database ... hard for the code to read ... - thunder
    • Yes)))) thanks for pointing the idea))) $ stmt = $ this-> pdo-> prepare ("SELECT * FROM pop WHERE id = '$ this-> id'"); $ stmt-> execute (); $ row = $ stmt-> fetch (); there is a mistake, that's just how to fix it? - Andret
    • Added to where? for here is a sample ... - thunder
    • here $ row = $ stmt-> fetch () you need to write something else then to check for the presence of data in the database - Andret

    added to the database. Because of this line all comments except the last were displayed.