2015-01-03 8 views
0

다음 코드를 사용하여 html 형식의 데이터베이스에있는 일부 행을 삽입하고 업데이트하십시오. 내가 제출할 때 insert form은 아무 것도하지 않으며, 오류를 삽입하거나 표시하지 않으며 의 메시지에는 위와 같은 삭제 쿼리가 있어도 Database access failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Craciun, Perioada_eveniment=2014-12-25, Tip_even=Muzical, Locatie_eveniment=Bucu' at line 1라고 표시됩니다. 이 문제를 어떻게 해결할 수 있습니까? 추신 : 데이터베이스 연결이 제대로 설정되었습니다 :), 감사합니다!Mysql 알 수없는 오류 삽입 및 업데이트

HTML 
<div class="update_row"> 
       Update 
       <form method="post" action=""> 
       *<input type="text" name="ID_even" Placeholder="Id eveniment"><br> 
       *<input type="text" name="nume" Placeholder="Nume eveniment"><br> 
       *<input type="text" name="perioada" Placeholder="Perioada eveniment"><br> 
       *<input type="text" name="tip" Placeholder="Tip eveniment"><br> 
       *<input type="text" name="locatie" Placeholder="Locatie eveniment"><br> 
       *<input type="text" name="id_organizator" Placeholder="ID Organizator"><br> 
       <input type="submit" name="submit" value="Modifica"> 
       </form> 
      </div> 

      <div class="add_row"> 
       Add 
       <form method="post" action=""> 
       *<input type="text" name="id_add" Placeholder="Id eveniment"><br> 
       *<input type="text" name="nume_add" Placeholder="Nume eveniment"><br> 
       *<input type="text" name="perioada_add" Placeholder="Perioada eveniment"><br> 
       *<input type="text" name="tip_add" Placeholder="Tip eveniment"><br> 
       *<input type="text" name="locatie_add" Placeholder="Locatie eveniment"><br> 
       *<input type="text" name="id_org_add" Placeholder="ID Organizator"><br> 
       <input type="submit" name="submit_add" value="Adauga"> 
       </form> 
      </div> 


PHP 
      if (isset($_POST["id_add"])) 
       $id_add=$_POST["id_add"]; 
      if (isset($_POST["nume_add"])) 
       $nume_add=$_POST["nume_add"]; 
      if (isset($_POST["perioada_add"])) 
       $perioada_add=$_POST["perioada_add"]; 
      if (isset($_POST["tip_add"])) 
       $tip_add=$_POST["tip_add"]; 
      if (isset($_POST["locatie_add"])) 
       $locatie_add=$_POST["locatie_add"]; 
      if (isset($_POST["id_org_add"])) 
       $id_org=$_POST["id_org_add"]; 
       $submitcheck_add=isset($_POST["submit_add"]); 


      if($submitcheck_add && $nume_add !=0 && $perioada_add !=0 && $locatie_add !=0 && $id_org !==0){ 
       $sql = "INSERT INTO evenimente (ID_even, Nume_eveniment, Perioada_eveniment, Tip_even, Locatie_eveniment, ID_Org) 
VALUES ($id_add, $nume_add, $perioada_add, $tip_add, $locatie_add, $id_org)"; 
       $result=query_mysql($sql); 
      } 



      if (isset($_POST["ID_even"])) 
       $id=$_POST["ID_even"]; 
      if (isset($_POST["nume"])) 
       $nume=$_POST["nume"]; 
      if (isset($_POST["perioada"])) 
       $perioada=$_POST["perioada"]; 
      if (isset($_POST["tip"])) 
       $tip=$_POST["tip"]; 
      if (isset($_POST["locatie"])) 
       $locatie=$_POST["locatie"]; 
      if (isset($_POST["id_organizator"])) 
       $id_organizator=$_POST["id_organizator"]; 
       $submitcheck=isset($_POST["submit"]); 

      if($submitcheck && $id !==0 && $nume !==0 && $perioada !==0 && $tip !==0 && $locatie !==0 && $id_organizator !==0){ 
       $sql = "UPDATE evenimente SET Nume_eveniment=$nume, Perioada_eveniment=$perioada, Tip_even=$tip, Locatie_eveniment=$locatie, ID_org=$id_organizator WHERE ID_even=$id"; 
       $result= query_mysql($sql); 
      } 

LE :

PDO 예 :

if (isset($_POST["ID_even"])) 
       $id=$_POST["ID_even"]; 
      if (isset($_POST["nume"])) 
       $nume=$_POST["nume"]; 
      if (isset($_POST["perioada"])) 
       $perioada=$_POST["perioada"]; 
      if (isset($_POST["tip"])) 
       $tip=$_POST["tip"]; 
      if (isset($_POST["locatie"])) 
       $locatie=$_POST["locatie"]; 
      if (isset($_POST["id_organizator"])) 
       $id_organizator=$_POST["id_organizator"]; 
       $submitcheck=isset($_POST["submit"]); 

      if($submitcheck && $id !==0 && $nume !==0 && $perioada !==0 && $tip !==0 && $locatie !==0 && $id_organizator !==0){ 
        $servername = "localhost"; 
        $username = "root"; 
        $password = "root"; 
        $dbname = "organizator_evenimente"; 
      try { 
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 
        // set the PDO error mode to exception 
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

        $sql = "UPDATE evenimente SET Nume_eveniment=$nume, Perioada_eveniment=$perioada, Tip_even=$tip, Locatie_eveniment=$locatie, ID_Org=$id_organizator WHERE ID_even=$id"; 

        // Prepare statement 
        $stmt = $conn->prepare($sql); 

        // execute the query 
        $stmt->execute(); 

        // echo a message to say the UPDATE succeeded 
        echo $stmt->rowCount() . " records UPDATED successfully"; 
        } 
       catch(PDOException $e) 
        { 
        echo $sql . "<br>" . $e->getMessage(); 
        } 

       $conn = null; 
       } 

그리고 오류 : UPDATE evenimente SET Nume_eveniment=Concert Craciun, Perioada_eveniment=2014-12-25, Tip_even=Muzical, Locatie_eveniment=Bucuresti, ID_Org=2 WHERE ID_even=2 SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Craciun, Perioada_eveniment=2014-12-25, Tip_even=Muzical,

내가이 시점에서 붙어있어주세요! 당신의 쿼리 문자열 값 주위

+3

아마도 검색어에 누락 된 따옴표가 있습니다. 어쨌든 준비된 문장을 사용해야합니다. 다른 것들 (SQL injections!) 외에도 그들은 이것을 처리합니다. – Sirko

+0

그것은 단지 학교 프로젝트입니다, 어쨌든 고마워요! –

+2

Ugg! 자신이하는 일을 모를 때 PDO 준비 문을 사용하십시오. – user1032531

답변

3

당신이 놓치고있는 따옴표 :

$sql = "INSERT INTO evenimente (ID_even, Nume_eveniment, Perioada_eveniment, Tip_even, Locatie_eveniment, ID_Org) 
VALUES ('$id_add', '$nume_add', '$perioada_add', '$tip_add', '$locatie_add', '$id_org')"; 
+0

Nop, 아무 것도 변경되지 않았습니다 ... –

+1

쿼리 게시 –

+0

내 쿼리는 위에 표시된 것입니다. 내가 (양식 추가) 제출하면 오류를 표시하지 않으며 페이지를 새로 고치고 db에 아무 것도 추가하지 않습니다. –

0

먼저 당신이 당신의 코드를 청소하고 입력을 저장해야합니다.

코드에서 ID_Org 매개 변수를 보았습니다. 그러나 업데이트시에는 ID_org이므로 열 이름이 ID_org 또는 ID_Org인지 확인하십시오. 가장 좋은 방법은 대문자를 데이터베이스 테이블의 이름으로 사용하지 않는 것입니다.

btw이 같은 코드를 작성하면 더 읽기 쉽습니다.

<?php 
    $id_add = filter_input(INPUT_POST,'id_add'); 

    //at first check which method is called 
    $insert = filter_input(INPUT_POST,'submit_add'); 
    if($insert){ 
     //do insert 
     //try using PDO or mysqli for SQL Injection 
    }else{ 
     $update = filter_input(INPUT_POST,'submit'); 
     if($update){ 
      //do update 
     }//else{ 
      //here should be code to handle error or nothing if it is ok 
     //} 
    }