在我的项目中,形成了具有从DB推导出的值的表。在循环中对2个表进行比较并输出这些值之后,我停止了保存到数据库。我在 rel_eq 表格中进行了保存,比较使 rel_eq 表格显示为 rel_cat 。请帮忙解决这个问题。
这里我比较2个表的列并在循环中输出输出
<?php
global $wpdb;
$table_name = $wpdb->prefix . 'rel_eq';
$table_cat = $wpdb->prefix . 'rel_cat';
$eq_arr = $wpdb->get_results("SELECT * FROM $table_name JOIN $table_cat on $table_name.relcat = $table_cat.id");
?>
<table align="center" cellpadding="10" border="1" id="user_table">
<tr>
<th>Title</th>
<th>Anonce</th>
<th>Url</th>
<th>Category</th>
<th>Action</th>
</tr>
<?php
foreach($eq_arr as $row):
?>
<tr id="row<?php echo $row->id;?>">
<td id="title_val<?php echo $row->id;?>"><?php echo $row->title;?></td>
<td id="anonce_val<?php echo $row->id;?>"><?php echo $row->anonce;?></td>
<td id="url_val<?php echo $row->id;?>"><?php echo $row->url;?></td>
<td id="relcat_val<?php echo $row->id;?>"><?php echo $row->name; ?></td>
<td>
<input type='button' class="edit_button" id="edit_button<?php echo $row->id;?>" value="edit" onclick="edit_row('<?php echo $row->id;?>');">
<input type='button' class="save_button" id="save_button<?php echo $row->id;?>" value="save" onclick="save_row('<?php echo $row->id;?>');">
<input type='button' class="delete_button" id="delete_button<?php echo $row->id;?>" value="delete" onclick="delete_row('<?php echo $row->id;?>');">
</td>
</tr>
<?php
endforeach;
?>
在这里,我将从表中编辑的数据写入数据库
<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/wp-config.php' );
global $wpdb;
$table_name = $wpdb->prefix . 'rel_eq';
if ( isset( $_POST['edit_row'] ) ){
$wpdb->update( $table_name, array(
'id'=>$_POST['row_id'],
'title' => $_POST['title_val'],
'anonce' => $_POST['anonce_val'],
'url' => $_POST['url_val'],
'relcat' => $_POST['cat_val']),
array( 'id' => $_POST['row_id'] ),
array( '%s', '%s', '%s', '%s')
);
echo mysql_insert_id();
exit();
}
if ( isset( $_POST['insert_row'] ) ){
$wpdb->insert( $table_name, array(
'title' => $_POST['title_val'],
'anonce' => $_POST['anonce_val'],
'url' => $_POST['url_val'],
'relcat' => $_POST['cat_val']),
array( '%s', '%s', '%s', '%s')
);
echo mysql_insert_id();
exit();
}
if ( isset( $_POST['delete_row'] ) ){
$wpdb->delete( $table_name, array(
'id'=>$_POST['row_id']),
array( '%s')
);
echo mysql_insert_id();
exit();
}
我的ajax请求
function edit_row(id)
{
var title=document.getElementById("title_val"+id).innerHTML;
var anonce=document.getElementById("anonce_val"+id).innerHTML;
var url=document.getElementById("url_val"+id).innerHTML;
document.getElementById("title_val"+id).innerHTML="<input type='text' id='title_text"+id+"' value='"+title+"'>";
document.getElementById("anonce_val"+id).innerHTML="<input type='text' id='anonce_text"+id+"' value='"+anonce+"'>";
document.getElementById("url_val"+id).innerHTML="<input type='text' id='url_text"+id+"' value='"+url+"'>";
document.getElementById("edit_button"+id).style.display="none";
document.getElementById("save_button"+id).style.display="block";
}
function save_row(id)
{
var title=document.getElementById("title_text"+id).value;
var anonce=document.getElementById("anonce_text"+id).value;
var url=document.getElementById("url_text"+id).value;
jQuery.ajax
({
type:'post',
url:'../wp-content/plugins/related-equipment/includes/update.php',
data:{
edit_row:'edit_row',
row_id:id,
title_val:title,
anonce_val:anonce,
url_val:url
},
success:function(response) {
if(response=="success")
{
document.getElementById("title_val"+id).innerHTML=title;
document.getElementById("anonce_val"+id).innerHTML=anonce;
document.getElementById("url_val"+id).innerHTML=url;
document.getElementById("edit_button"+id).style.display="block";
document.getElementById("save_button"+id).style.display="none";
}
jQuery(document).ready(function($){
$('#row'+id+' input').css('display','none');
$('#row'+id).html('<td>'+title+'</td><td>'+anonce+'</td><td>'+url+'</td><td><input type=\'button\' class=\'edit_button\' id=\'edit_button"+id+"\' value=\'edit\' onclick=\'edit_row("+id+");\'/><input type=\'button\' class=\'save_button\' id=\'save_button"+id+"\' value=\'save\' onclick=\'save_row("+id+");\'/><input type=\'button\' class=\'delete_button\' id=\'delete_button"+id+"\' value=\'delete\' onclick=\'delete_row("+id+");\'/></td>');
});
}
});
}
function delete_row(id)
{
jQuery.ajax
({
type:'post',
url:'../wp-content/plugins/related-equipment/includes/update.php',
data:{
delete_row:'delete_row',
row_id:id,
},
success:function(response) {
if(response=="success")
{
var row=document.getElementById("row"+id);
row.parentNode.removeChild(row);
}
jQuery(document).ready(function($){
$('#row'+id).remove();
});
}
});
}
function insert_row()
{
var title=document.getElementById("new_title").value;
var anonce=document.getElementById("new_anonce").value;
var url=document.getElementById("new_url").value;
jQuery.ajax
({
type:'post',
url:'../wp-content/plugins/related-equipment/includes/update.php',
data:{
insert_row:'insert_row',
title_val:title,
anonce_val:anonce,
url_val:url
},
success:function(response) {
if(response!="")
{
var id=response;
var table=document.getElementById("user_table");
var table_len=(table.rows.length)-1;
var row = table.insertRow(table_len).outerHTML="<tr id='row"+id+"'><td id='title_val"+id+"'>"+title+"</td><td id='anonce_val"+id+"'>"+anonce+"</td><td id='url_val"+id+"'>"+url+"</td><td><input type='button' class='edit_button' id='edit_button"+id+"' value='edit' onclick='edit_row("+id+");'/><input type='button' class='save_button' id='save_button"+id+"' value='save' onclick='save_row("+id+");'/><input type='button' class='delete_button' id='delete_button"+id+"' value='delete' onclick='delete_row("+id+");'/></td></tr>";
document.getElementById("new_title").value="";
document.getElementById("new_anonce").value="";
document.getElementById("new_url").value="";
}
}
});
}
答案 0 :(得分:0)
我无法完全消化你所说的问题&#39;在这里,但在我看来,如果它不起作用,可能是因为没有指定“编辑”。正确。
您指定4个字段加上更新ID,因此替换参数应修改如下(假设&#39; id&#39;是整数):
$wpdb->update($table_name,
array('id'=>$_POST['row_id'],'title' => $_POST['title_val'],'anonce' => $_POST['anonce_val'],'url' => $_POST['url_val'],'relcat' => $_POST['cat_val']),
array( 'id' => $_POST['row_id'] ),
array( '%d', '%s', '%s', '%s', '%s'),
array( ‘%d’ ));
此外,在&#39;删除&#39;中,您确定&#39;行ID&#39;是一个字符串(%s)?
if ( isset( $_POST['delete_row'] ) ){
$wpdb->delete( $table_name, array(
'id'=>$_POST['row_id']),
array( '%s')
);
echo mysql_insert_id();
exit();
}
Wordpress文档是一个很好的资源,如果我没有清楚地解释我能看到的内容: