php - 这是使用 .post 的好方法吗?

标签 php jquery mysql .post

我做了一些代码。我正在使用 noty little framework .

    <script type="text/javascript">
function generateNormal(type, text, layout)
{
    if (typeof(layout)==='undefined') layout = 'topRight';
    var n = noty(
    {
        text: text,
        type: type,
        dismissQueue: true,
        theme: 'default',
        layout: layout
   }
   );    
}
function generateButtonDelete(id)
{
    if (typeof(layout)==='undefined') layout = 'topRight';
    var n = noty(
    {
        text: 'Are you sure you want to delete the record?',
        type: 'alert',
        dismissQueue: true,
        theme: 'default',
        layout: 'center' ,
        buttons: [
                  {addClass: 'btn btn-primary', text: 'Ok', onClick: function($noty) {
                    $noty.close();
                    $.post("delete.php", {id: id },
                            function(data) {
                                if (data)
                                {
                                    generateNormal('success', 'The record is deleted with succes!')       
                                }
                                else
                                {
                                    generateNormal('error', 'There is a problem with the database, please try again.')
                                }
                            });                    
                  }
                  }
    ]
   }
   );    
}


</script>

上面的代码将由 PHP 生成的表格使用:

<table>
<tr>
    <th>
        Delete
    </th>
    <th>
        ID
    </th>
    <th>
        Description 
    </th>
</tr>
<!--  This section will be created by PHP -->
<tr>
    <td>
        <a href="#" onclick="generateButtonDelete(1)">Delete</a>
    </td>
    <td>
        1
    </td>
    <td>
        Description row 1 
    </td>
</tr>
    <tr>
    <td>
        <a href="#" onclick="generateButtonDelete(2)">Delete</a>
    </td>
    <td>
        2
    </td>
    <td>
        Description row 2 
    </td>
</tr>
<!--  End of section that will be create by PHP -->

在 delete.php 文件中,删除记录的查询将使用 PHP 执行。并回显 true 或 false,判断查询是否执行良好。

代码在控制台中运行完美,没有任何错误。 但我不确定这是否安全,这只是一个概念证明。 html 和 PHP 页面将使用登录脚本进行保护。

我希望有人能告诉我这是否安全?

抱歉我的英语不好。

编辑:我制作了一些 PHP 代码。但这只是一个概念证明,所以我没有测试代码,但 Eclipse 没有给出任何错误。

    <?php 
if (isset($_POST['id']))
{
    if (is_int($_POST['id']))
    {
        if ($_POST['id'] > 0)
        {
            $con = new PDO('mysql:host=localhost;dbname=testdb;charset=UTF-8', 'username', 'password');
            $sql = "DELETE FROM table_name WHERE id=?";
            $q = $con->prepare($sql);
            $execution = $q->execute(array($_POST['id']));

            if ($execution) echo true;
            else echo false;
        }   
        else echo false;
    }
    else echo false;
}
?>

最佳答案

就 SQL 注入(inject)而言,您的代码是安全的,您可以像这样整理那些 if else 语句:

if (isset($_POST['id']) && (int) $_POST['id'] > 0) {

我看到的唯一问题是没有检查用户是否允许删除记录,我猜你也有一些 session 逻辑,也许还有一个查询检查当前用户是您要删除的记录的所有者/管理员。

关于php - 这是使用 .post 的好方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11774395/

相关文章:

javascript - 需要将 PHP 正则表达式转换为 JavaScript 正则表达式

PHP:如何使用较少的查询

javascript - 如何使用 PHP 隐藏空的 ACF 字段?

javascript - 单击时更新所有表下拉列表项

php - session_set_save_handler - 为什么此代码不起作用?

php - Mysql UPDATE 查询错误

javascript - 使用javascript关闭窗口回调url?

php - 将图像 ID 传递给 URL 段

javascript - 如何创建类似 Google Closure 的继承结构

带 CASE 的 mysql COUNT()