【问题标题】:How to use sql query within the script如何在脚本中使用 sql 查询
【发布时间】:2015-09-09 00:31:41
【问题描述】:

我有一个在单击按钮时执行的脚本。它工作正常。有两点我想运行删除查询来删除数据库条目

1) 我在脚本中有一个代码可以检查用户是否重新加载或按下了返回按钮。代码是:

 window.onbeforeunload = function(e)
    {
      return 'You will loose a chance of negotiation for today. Are you sure you want to exit? ';
      /***Run delete query for deleting db entry***/
    };

If the user reloads or goes to previous page even after the warning i would like to run delete query

2) 我的脚本中有以下部分,想在最后运行删除查询

var ticker = function() {
      counter--;
      var t = (counter / 60) | 0; // it is round off
      digits.eq(0).text(t);
      t = ((counter % 60) / 10) | 0;
      digits.eq(2).text(t);
      t = (counter % 60) % 10;
      digits.eq(3).text(t);
      if (!counter) {
        clearInterval(timer);
        alert('Time out !');
        resetView();
      }
    };

问题是我不确定我应该如何在我的脚本中编写我的 sql 查询而不让用户知道(查询将在后端运行)。删除查询是这样的

$sql="DELETE FROM product where id='".$id."'";
if(!mysqli_query($con,$sql))
    {
    "Error deleting record:" . mysqli_error($con);
    }

谁能告诉我如何组合代码

我尝试使用的 ajax 代码

 window.onbeforeunload = function(e)
{
 $.ajax({
    type: 'post',
    url: 'test2.php',
    dataType: 'json',
    data: {
      txt: txtbox,
      hidden: hiddenTxt
    },
    cache: false,
    console.log(returndata);
    },
    error: function() { 
      console.error('Failed to process ajax !');
    }
  });
  };

【问题讨论】:

  • 因为它在后端你可以有一个ajax request和相关的id,在你的php函数中(例如:deleteRecord)确保用户已经登录(cookie存在等)并运行查询。
  • @Ofir Baruch 用户已登录,而此过程应该运行,我使用了 ajax 但它不起作用
  • 考虑与我们分享您尝试过但无效的 ajax 代码。另外,请注意didn't work 并不能真正帮助我们找到问题所在——究竟是什么不起作用?有什么警告吗?有输出吗?
  • @OfirBaruch 我已经编辑了我的代码,我在控制台中没有任何错误,但它停止了我的整个脚本
  • network 标签下-> 查找test2.php 行-> 打开preview 标签。你看到了什么?

标签: javascript jquery mysql ajax mysqli


【解决方案1】:

您应该为此使用 AJAX。看看jQuery's ajax documentation,它应该可以帮助您入门。

【讨论】:

  • 我为 ajax 尝试了这段代码:$.ajax({ type: 'post', url: 'test2.php', dataType: 'json', data: { txt: txtbox, hidden: hiddenTxt }, cache: false, console.log(returndata); }, error: function() { console.error('处理ajax失败!'); } });但它没有用
猜你喜欢
  • 2013-03-27
  • 2017-01-10
  • 1970-01-01
  • 2023-01-18
  • 2019-02-25
  • 2017-07-26
  • 2023-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多