0

我想在加载期间执行查询。我该怎么做?这是我的代码。这是我要执行的查询。

<?php
    mysql_query("UPDATE `requests` SET `stat_id`= 3 WHERE `proj_id`='".$_GET['projid']."' and DATE(`req_date`)='".$_GET['date']."'");   
?>

我有一个javascript

<script type="text/javascript">
$(document).ready(function(){
    $('input#approve').click(function(){
        var date = $(this).parent().attr('id');
        var projid = $(this).parent().attr('class');

        window.location.href="index.php?page=requests&date="+date+"&projid="+projid+"";
    });
});
</script>

单击按钮批准时,页面刷新,但查询未执行。

4

4 回答 4

0

你也可以使用ajax,在你的java脚本中写这个

var date = $(this).parent().attr('id');
var projid = $(this).parent().attr('class');
$.ajax({
    type: "POST",
    url: "index.php",
    data: "{stat_id: 3,proj_id:projid,Date:date}",
    success: function(result) {
        alert(result);
    }

结果将是布尔值或 0/1 的形式。

于 2013-01-06T14:16:24.067 回答
0
$(document).ready(function(){
    $('input#approve').click(function(){
        var elem = $(this).parent(),
            date = elem.attr('id'),
            projid = elem.attr('class');

        $.get("index.php", { page: 'requests', date: date, projid: projid }, function() {   
            console.log('updated!')
        })
    });
});
于 2013-01-06T14:19:14.517 回答
0

mysql_query将返回一个值,您可以使用它来查看问题是否在查询中以及是否正在执行。

<?php
  //Output GET Params
  echo $_GET['date'] . " " . $_GET['projid'];

 //Escape Slashes in Date http://php.net/manual/en/function.addslashes.php
  $date = addslashes($_GET['date']);

  $result = mysql_query("UPDATE `requests` SET `stat_id`= 3 WHERE `proj_id`='".$_GET['projid']."' and DATE(`req_date`)='".$date."'");  

  //Check to see if an error has occurred. If so output.
  if (!$result) {
     die('Invalid query: ' . mysql_error());
  }

?>

Javascript:(硬编码值)

<script type="text/javascript">
$(document).ready(function(){
$('input#approve').click(function(){

   //Hard code for now to ensure you have accurate data
    var date = "12/12/12";
    var projid = 15;

    window.location.href="index.php?page=requests&date="+date+"&projid="+projid+"";
});
});

于 2013-01-06T14:27:21.540 回答
0

您可以使用以下代码index.php更新表

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

mysql_query("UPDATE Persons SET stat_id=3 WHERE proj_id='".$_POST['projid']."' and DATE(`req_date`)=DATE('".$_POST['date']."')");

mysql_close($con);
?> 

检查w3Schools

然后用ajax调用这个页面

$.ajax({
  type: "POST",
  url: "Index.php",
  data: { projid: projid , date: date }
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

jQuery ajax 参考

于 2013-01-06T14:34:51.823 回答