当我尝试打开一个输入标签要插入数据库的页面时,我遇到了一个问题...当我打开页面时,数据被删除并插入一个空白的ID,其中包含0 ..另一个问题是,总是向我显示“数组”这个词并且不显示数据......你能帮我解决这个问题吗?
这里是DB结构,我以前的数据插入(在phpMyAdmin中),图像如何在页面中看到它的形式以及当我在数据库中打开页面(id_pag = 1)时数据发生变化的结果: ![标签] [1]
以下是带有代码的表单:
使用新代码编辑
<?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调用为空,不发送任何内容..显示ajax-loader.gif但不在DB中插入任何内容
答案 0 :(得分: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;
});
});
现在我可以更新和删除标签了!但是我唯一不知道的是当它是一个没有标签的新页面时如何创建它。