dongsui5464 2017-12-31 20:15
浏览 60

使用ajax将值插入数据库,其中session变量等于当前用户会话[关闭]

I'm trying to insert a value generated by a PHP script using an ajax function into my database based on the current session. Below it's my ajax function to generate a new address:

<script>
function newaddress(){
  $.ajax({
        url:"newaddress.php", //the page containing php script
        type: "POST", //request type
        success:function(result){                           
        document.getElementById('depositAddress').value = result;       
        alert("New address generated");
       }
     });
 }
</script>

My question is, How to add the 'result' to the database using the current session running on the page? I thought something along the lines of

<?php
    include_once 'dbh.inc.php';
    $sql = "INSERT INTO users(BTCAddress) VALUES ('result') WHERE ('u_uid') = ($_SESSION['u_uid']);";
?>

The second php script was just rough working out. Do i need to use the post method? If so, How to get the result generated by the ajax javascript to post into the php file in order to save the address generated into the database 'user' where the session is equal to the current session running on the web page?. Sorry if i have waffled on or overcomplicated things. Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • donglingyi4679 2017-12-31 21:46
    关注

    I solved it. I realise that there were some stupid questions in my first post - sorry about that. It was as easy as appending the newaddress.php file to add a new session variable and then UPDATE the database.

    $user = $_SESSION['u_uid'];
    $_SESSION['BTCAddress'] = $btcaddress;
    $sql = "UPDATE users SET BTCAddress = '$btcaddress' WHERE user_uid = '$user';";
    mysqli_query($conn, $sql);
    

    Very simple in the end. Thanks for all the input.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度