2014-10-19 102 views
0

我正在使用以下代碼通過fancybox打開網址。我想在用戶點擊這個按鈕時更新mysql數據庫。如何使用鏈接單擊按鈕後更新Mysql數據庫?

當前的代碼是:

<a class="fancybox fancybox.iframe more_info_btn" href="http://www.google.com"> 
<button style="cursor:pointer; padding:8px;">Test Live Session</button></a> 

我想,當用戶點擊按鈕,在打開的fancybox URL一起更新MySQL數據庫。

mysql的更新如下

mysql_query("UPDATE tablename SET test_session_clicked='1' 
WHERE reg_id=".$_SESSION['user_id21']"); 
+0

只是使用按鈕提交表單或使用AJAX,如果你不想刷新頁面 – Ghost 2014-10-19 10:08:54

+0

通常會用JavaScript來完成,所以在按鈕[click](https://api.jquery.com/click/)上[fire o [請求](https://api.jquery.com/jquery.ajax/)到運行查詢的服務器端(php)。 – JimL 2014-10-19 10:09:36

+0

請勿使用'mysql_'功能。他們已被棄用。你應該考慮遷移到'mysqli_'或PDO。而且你的代碼很可能傾向於SQL注入。同時,您要求縮小差距的技術稱爲AJAX - 您使用JS對執行查詢的服務器腳本進行AJAX調用,並(可選)返回響應。 – Terry 2014-10-19 10:09:55

回答

1

使用此

<a class="fancybox fancybox.iframe more_info_btn" href="http://www.google.com"> 
<button style="cursor:pointer; padding:8px;" id="btn">Test Live Session</button></a> 



jQuery('#btn').click(function(){ 
    jQuery.ajax({ 
     type:'POST', 
     url:'your Php file path', 
     data:{'updated':true}, 
     dataType:'json' 
     success:function(data){ 
      alert(data.error); 
     } 
    }) 
    }) 

php文件你

if (isset($_POST['updated'])){ 

    $queryStr = "UPDATE tablename SET test_session_clicked='1' 
    WHERE reg_id=".$_SESSION['user_id21']; 
    if (mysql_query($qyeryStr)){ 
     echo json_encode(array('error'=>false)); 
    }else{ 
     echo json_encode(array('error'=>true)); 
    } 
    } 
+0

thanx尋求幫助。我試着給你的代碼..但它不更新數據庫... – chplab 2014-10-19 11:04:01

+0

你alreade集$ _SESSION ['user_id21']; ??? – 2014-10-19 11:04:57

+0

是的....和在PHP中,數據庫連接也被使用... – chplab 2014-10-19 11:07:16

相關問題