2014-01-16 69 views
0

我有一個問題,當我嘗試打開一個頁面,其中有一個輸入標籤插入數據庫...當我打開頁面時,數據被刪除並插入一個空白的0在ID ..另一個問題是,總是讓我看看「數組」字樣,並不顯示數據...你能幫我解決這個問題嗎?用mysql插入和刪除標籤PDO

這裏是數據庫結構,我以前的數據插入(在phpMyAdmin中),圖像如何看到它在頁面中的形式和結果當我在DB中打開頁面時(id_pag = 1)數據發生了變化: ! [標籤] [1]

這裏是用代碼的形式:


EDITED從新的代碼

<?php 
    require_once('includes/connection.php'); 
$action = isset($_POST['action']) ? $_POST['action'] : ""; 
if($action == "update"){ 
    try{ 
    $tags = is_array($_POST['tags']) 
    ? implode(', ', $_POST['tags']) 
    : $_POST['tags']; 
    $stmt = $conn->prepare('INSERT INTO TAGS (tags,id_pag) 
    VALUES (:tags,:id_pag)'); 
    $stmt->bindParam(':tags', $tags); 
    $stmt->bindParam(':id_pag', $_POST['id_pag']); 
    $stmt->execute(); 
    echo 'Registro ingresado correctamente.';        
    } 
     catch (Exception $e) 
    { 
      $return['databaseException'] = $e->getMessage(); 
    } 
    $dbh = null;     
}       
?> 

JavaScript的Ajax調用(在 'JS/custom.js'):

$(function(){ 
    $("#tags").submit(function(){ 
     $.ajax({ 
     type:"POST", 
     url:"assets/tag.php?ts=" + new Date().getTime(), 
     dataType:"text", 
     data:$(this).serialize(), 
     beforeSend:function(){ 
      $("#loading").show(); 
     }, 
     success:function(response){ 
      $("#responds").append(response); 
      $("#loading").hide(); 
     } 
     }) 
     return false; 
    }); 
}); 

形式:

<?php 
    try { 
     $stmt = $conn->prepare("select tags,id_pag from TAGS where id_pag = 1"); 
     $stmt->execute(); 
     $row = $stmt->fetch(PDO::FETCH_ASSOC); 
     $tags = $row['tags']; 
     $id_pag = $row['id_pag']; 
    } 
    catch(PDOException $exception) 
    { 
     echo "Error: " . $exception->getMessage(); 
    }         
?> 
<form class="form-horizontal" name="tags" id="tags" method="post"> 
    <div class="form-group"> 
     <label class="control-label col-md-3">Palabras clave</label> 
     <div class="col-md-9"> 
      <input id="tags_1" type="text" class="form-control tags" name="tags[]" value="<?php echo $tags; ?>"/> 
      <input type="hidden" class="form-control tags" name="id_pag" value="1"/> 
      <input type="hidden" name="action" value="update" /> 
      <span class="input-group-btn"> 
       <button class="btn btn-success" type="submit" name="enviar"><i class="fa fa-check"></i> Grabar</button> 
      </span> 
     </div> 
    </div> 
</form> 
<div id="loading" style="display:none;"><img src="assets/img/ajax_loader.gif" /></div> 

現在我可以看到數據,但是當我嘗試保存新標籤Ajax調用是空的,不要發送nothing..the Ajax的loader.gif被顯示,但在DB不插入任何

+1

你很容易受到[SQL注入攻擊](http://bobby-tables.com) 。 PDO提供了針對此的機制,並且您不使用它們中的任何一個。至於你的數組問題,'$ foo = array(); echo $ foo;'是它的原因 - 你試圖在字符串上下文中輸出一個數組。 –

+0

對於任何和所有用戶數據,尤其是'$ _POST'參數,您必須**使用[適當的SQL轉義](http://bobby-tables.com/php)。你在這裏做的是完全繞過PDO數據佔位符系統,這是非常糟糕的。 – tadman

+0

@tadman和Marc B,感謝您的教程..我更改了代碼,現在我可以看到數據庫中的數據,但是當我嘗試保存新標籤時,ajax調用爲空...您能幫我解決這個問題 –

回答

0

這裏是我的問題的答案:

代碼:

<?php 
    require_once("connection.php"); 
    $action = isset($_POST["action"]) ? $_POST["action"] : ""; 
    if($action == "update_tag"){ 
     try{ 
      $tags = is_array($_POST['tags']) 
      ? implode(', ', $_POST['tags']) 
      : $_POST['tags']; 
      $sql = 'UPDATE TAGS SET 
      tags = :tags, 
      id_pag = :id_pag 
      WHERE id_tags= :id_tags'; 
      $stmt = $conn->prepare($sql); 
      $stmt->bindParam(':tags', $tags, PDO::PARAM_STR); 
      $stmt->bindParam(':id_pag', $_POST['id_pag'], PDO::PARAM_STR); 
      $stmt->bindParam(':id_tags', $_POST['id_tags'], PDO::PARAM_INT); 
      $stmt->execute(); 
      echo "Configuracion actualizado correctamente."; 
     } 
     catch(PDOException $exception){ 
      echo "Error: " . $exception->getMessage(); 
     } 
    } 
?> 

形式:

<?php 
    try { 
     $stmt = $conn->prepare("select id_tags, tags, id_pag from TAGS where id_pag = 1"); 
     $stmt->execute(); 
     $row = $stmt->fetch(PDO::FETCH_ASSOC); 
     $id_tags = $row['id_tags']; 
     $tags = $row['tags']; 
     $id_pag = $row['id_pag']; 
     }catch(PDOException $exception){ 
     echo "Error: " . $exception->getMessage(); 
    } 
?> 
<form class="form-horizontal form-bordered" name="tags_save" id="tags_save" method="post"> 
    <fieldset> 
     <div class="control-group"> 
      <label class="control-label col-md-3"><?php echo $translate->__('Keywords'); ?></label> 
      <div class="col-md-9"> 
       <input id="tags_1" type="text" class="form-control tags" name="tags[]" value="<?php echo $tags; ?>"/> 
       <input type="hidden" name="id_pag" value="1" id="id_pag"/> 
       <input type="hidden" name="id_tags" value="<?php echo $id_tags; ?>" id="id_tags"/> 
      </div> 
     </div> 
     <div class="control-group"> 
      <div class="row"> 
       <div class="col-md-9"> 
        <div class="col-sd-offset-3 col-md-3"> 
         <input type="hidden" name="action" value="update_tag" /> 
         <button type="submit" class="btn btn-success" name="enviar"><i class="fa fa-check"></i> <?php echo $translate->__('Save'); ?></button> 
        </div> 
       </div> 
      </div> 
     </div> 
    </fieldset> 
</form> 
<div id="loading_tag" style="display:none;"><img src="assets/img/ajax_loader.gif" /></div> 

JavaScript的AJAX:

$(function(){ 
    $("#tags_save").submit(function(){ 
     $.ajax({ 
      type:"POST", 
      url:"includes/tag.php?ts=" + new Date().getTime(), 
      dataType:"text", 
      data:$(this).serialize(), 
      beforeSend:function(){ 
       $("#loading_tag").show(); 
      }, 
      success:function(response){ 
       $("#responds").append(response); 
       $("#loading_tag").hide(); 
      } 
     }) 
     return false; 
    }); 
}); 

現在我可以更新和刪除標籤!但是我唯一不知道的是如何在沒有標籤的情況下創建它。