douchensou6969 2015-12-16 20:54
浏览 26

使用AJAX返回数据库信息[关闭]

I have a script that I intend to automate that copies a database table. I've been informed about replication but I have no idea of how to get it working properly, so I've decided to use this method instead.

To try and minimize security risks, our live site is going to make requests for the database table that our other site regularly uses. The request script, which I'll refer to as request.php, is as follows.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <script type="text/javascript">
            //Idea is to use this to get response to provide table data
            //What goes here?
        </script>
    </head>

    <body>

    <?php
        //Define database handler
        $dbWriter = new mysqli(database connection info);

        if($mysqli->connect_errno){
            $errorMessage = sprintf("'%s'", $mysqli->connect_error);
            echo $errorMessage;
        }
        else{
            $dbWriter->query("DELETE * FROM qb_inventory_status");

            //Processing of data goes here
            foreach($returnedItems as $item){
                //Create query to insert individual records
                $start = "INSERT INTO table(fieldA, fieldB...) VALUES (";
                $end = sprintf("'%s', %d, %d, %f, %f)", fieldA, fieldB...);
                $start .= $end;

                //Database handler uses $start to insert record
                $dbWriter->query($start);
            }

            $dbWriter->close();
        }
    ?>

    </body>
</html>

The response script (response.php) is as follows.

<?php
require_once($documentRoot."/classes/DBHandler.php");

$db_controller = new DBHandler();

$query = "SELECT * FROM table";
$result = $db_controller->select($query);

//This array is what should be returned so request.php can handle it
while($rowObject=mysql_fetch_object($rs)){        
    $rtnItems[]=$rowObject;
}

//Return $rtnItems here

?>

I know how to retrieve information from a database and put it in a format suitable for processing within the same file, but when it comes to a situation like this, I'm not sure how to pass it back to request.php. How can I make this work? I need to be able to write the returned array to the database. I've never done the latter process with Javascript before, so I was also wondering if it's possible to access the returned database with the PHP portion of request.php.

  • 写回答

1条回答 默认 最新

  • douhuang5623 2015-12-16 21:08
    关注

    You can do the basic of ajax with something like this

    To call

    $.ajax({url: "response.php", success: function(result){
            $("#div1").html(result);
     }});
    

    Check always the references on jquery http://api.jquery.com/jquery.ajax/

    Result in this case is the info you want to "paste" in your frontend.

    Use the data handler in response (to retrieve the info), you dont need it in the frontend UNLESS you are querying directly from it.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算