2014-01-16 74 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; 
    }); 
}); 

现在我可以更新和删除标签!但是我唯一不知道的是如何在没有标签的情况下创建它。