表编辑更新删除内联ajax,php和sql

时间:2015-03-26 08:24:42

标签: php jquery sql ajax

我发现这个示例代码使用ajax,php和sql编辑,更新和删除表中的行。

当我在localhost中运行此代码时,只有删除功能有效,所有其他功能都没有。

当我添加一行时,数据库中的行为空。

当我更新一行时,它总是使用第一行中的数据更新所有行。

任何人都可以帮我解决这个问题吗? example can be found here

这是我的代码

<?php
 include 'configuration.php';
 include 'functions/functions.php';
 $action = $_REQUEST['action'];

 switch($action) {

 case "load":
    $query  = mssql_query('SELECT * FROM job_creation ORDER BY id ASC');
    $count  = mssql_num_rows($query);
    if($count > 0) {
        while($fetch = mssql_fetch_array($query)) {
            $record[] = $fetch;
        }
    }
    ?>
     <a href="gridder_addnew" 
    id="gridder_addnew" class="gridder_addnew">
    <img src=   "images/insert.png" alt="Add New" title="Add New" /></a>
    <table class="as_gridder_table">
        <tr class="grid_header">
            <td><div class="grid_heading">no</div></td>
            <td><div class="grid_heading">First Name</div></td>
            <td><div class="grid_heading">Last Name</div></td>
            <td><div class="grid_heading">Actions</div></td>
        </tr>
        <tr id="addnew">
            <td>&nbsp;</td>
            <td colspan="6">
            <form id="gridder_addform" method="post">
            <input type="hidden" name="action" value="addnew" />
            <table width="100%">
            <tr>
                <td><input type="text" name="fname"
              id="fname"   class="gridder_add" /></td>
                <td><input type="text" 
            name="lname" id="lname" class="gridder_add" /></td>
                <td>&nbsp;
                <input type="submit" 
            id="gridder_addrecord" value="" 
            class="gridder_addrecord_button" title="Add" />
                <a href="cancel" id="gridder_cancel" 
            class="gridder_cancel"><img src="images/delete.png" 
            alt="Cancel" title="Cancel" /></a></td>
            </tr>
            </table>                    
            </form>
        </tr>
        <?php
        if($count <= 0) {
        ?>
        <tr id="norecords">
            <td colspan="7" align="center">No records found <a href="addnew" id="gridder_insert" class="gridder_insert"><img src="images/insert.png" alt="Add New" title="Add New" /></a></td>
        </tr>
        <?php } else {
        $i = 0;
        foreach($record as $records) {
        $i = $i + 1;
        ?>
        <tr class="<?php if($i%2 == 0) { echo 'even'; } else { echo 'odd'; } ?>">
            <td><div class="grid_content sno"><span><?php echo $i; ?></span></div></td>
            <td><div class="grid_content editable"><span><?php echo $records['name_ar']; ?></span><input type="text" class="gridder_input" name="<?php echo encrypt("name_ar|".$records['id']); ?>" value="<?php echo $records['name_ar']; ?>" /></div></td>
            <td><div class="grid_content editable"><span><?php echo $records['name_en']; ?></span><input type="text" class="gridder_input" name="<?php echo encrypt("name_en|".$records['id']); ?>" value="<?php echo $records['name_en']; ?>" /></div></td>

            <td>
            <a href="<?php echo encrypt($records['id']); ?>" class="gridder_delete"><img src="images/delete.png" alt="Delete" title="Delete" /></a></td>
        </tr>
        <?php
            }
        }
        ?>
        </table>
    <?php
break;

case "addnew":
    $fname      = isset($_POST['name_ar']) ? mssql_real_escape_string($_POST['name_ar']) : '';
    $lname      = isset($_POST['name_en']) ? mssql_real_escape_string ($_POST['name_en']) : '';
    mssql_query("INSERT INTO Job_creation (name_ar, name_en) VALUES ('$fname', '$lname')");
break;


case "update":
    $value  = $_POST['value'];
    $crypto = decrypt($_POST['crypto']);
    $explode = explode('|', $crypto);
    $columnName = $explode[0];
    $rowId = $explode[1];
    $query = mssql_query("UPDATE Job_creation SET name_ar = 
    '$value',name_en    = '$value' WHERE id = '$rowId' ");
    break;
    case "delete":
    $value  = decrypt($_POST['value']);
    $query = mssql_query("DELETE FROM Job_creation WHERE id = '$value' ");
    break;
  }
  ?>

注意:此示例在mySQL中有效,但我将其转换为MSSQL 非常感谢那些帮助我的人:)

1 个答案:

答案 0 :(得分:0)

解决问题插入(添加新内容)
替换此代码

<tr>
<td><input type="text" name="fname"
    id="fname"   class="gridder_add" /></td>
<td><input type="text" 
        name="lname" id="lname" class="gridder_add" /></td></tr>
case "addnew":
$fname      = isset($_POST['name_ar']) 
?   mssql_real_escape_string($_POST['name_ar']) : '';
$lname      = isset($_POST['name_en']) ?
mssql_real_escape_string    ($_POST['name_en']) : '';
mssql_query("INSERT INTO Job_creation (name_ar, name_en) 
VALUES ('$fname',  '$lname')");
break;

到此代码

<tr>
<td><input type="text" name="name_ar" id="name_ar" 
class="gridder_add" /> </td>
<td><input type="text" name="name_en" id="name_en" 
class="gridder_add" /></td></tr>
case "addnew":
    $fname      = $_POST['name_ar'];
    $lname      = $_POST['name_en'];
mssql_query("INSERT INTO Job_creation (name_ar, name_en) VALUES 
('$fname', '$lname')"); 
break;

解决问题更新
替换此代码

case "update":
$value  = $_POST['value'];
$crypto = decrypt($_POST['crypto']);
$explode = explode('|', $crypto);
$columnName = $explode[0];
$rowId = $explode[1];
$query = mssql_query("UPDATE Job_creation SET name_ar = 
'$value',name_en    = '$value' WHERE id = '$rowId' ");
break;

到此代码

    case 
   "update":
    $value  = $_POST['value'];
    $crypto = decrypt($_POST['crypto']);
    $explode = explode('|', $crypto);
    $columnName = $explode[0];
    $rowId = $explode[1];
    $query = mssql_query("UPDATE Job_creation SET 
    $columnName = '$value' WHERE id = '$rowId' ");
    break;