Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数据库中的值插入到xml表中,并使用ajax以html格式输出它们_Php_Mysql_Ajax_Xml - Fatal编程技术网

Php 将数据库中的值插入到xml表中,并使用ajax以html格式输出它们

Php 将数据库中的值插入到xml表中,并使用ajax以html格式输出它们,php,mysql,ajax,xml,Php,Mysql,Ajax,Xml,我有一个搜索框,用户可以在其中键入姓名,它将显示“firstname”、“username”、“lastname”、“email”、“accountnumber”。到目前为止,我已经能够从数据库中获取数据,制作它的xml结构(这是学校的要求之一)。问题是如何将来自搜索框的值回显到xml表中,然后将结果输出到HTML表中 数据库代码(文件名为ajaxsearch.php):(我知道我使用的是mysql,稍后我会修复它) 我非常感谢所有的帮助,因为我现在真的迷路了。Cient side: 您忘了在u

我有一个搜索框,用户可以在其中键入姓名,它将显示
“firstname”、“username”、“lastname”、“email”、“accountnumber”
。到目前为止,我已经能够从数据库中获取数据,制作它的xml结构(这是学校的要求之一)。问题是如何将来自搜索框的值回显到xml表中,然后将结果输出到HTML表中

数据库代码(文件名为ajaxsearch.php):(我知道我使用的是mysql,稍后我会修复它)

我非常感谢所有的帮助,因为我现在真的迷路了。

Cient side: 您忘了在url中添加搜索链接

    $("#btnSearch").click(function () {
    var searchLink = "ajax-search.php";
    $.ajax({
        type: "POST",
        url: searchLink,
        data: {sSearchFor : $("#txtSearch").val() },
        cache: false,
        dataType: "json",
        success: function (xml) {
            $(xml).find('searchresults').find('result').each(function () {
                var name = $(this).find("name").text();
                alert(name);
            });
        }
    });
});
服务器端: 在.PHP文件中使用此选项。我已经评论了处理文件保存的行:

<?php 
header("Content-type: text/xml");
//Create Database connection
$db = mysql_connect("127.0.0.1","root","");
if (!$db) {
    die('Could not connect to db: ' . mysql_error());
}

//Select the Database
mysql_select_db("bank",$db);

if(isset($_POST['sSearchFor']))
    $sSearchFor = $_POST['sSearchFor'];
else
    $sSearchFor = "";

$sql = "SELECT * FROM customers WHERE name LIKE '%$sSearchFor%'";
$result = mysql_query($sql, $db) or die(mysql_error());

//Create SimpleXMLElement object
$xml = new SimpleXMLElement('searchresults');


//Add each column value a node of the XML object

while($row = mysql_fetch_assoc($result)) {
    $result= $xml->addChild('result');
    $result->addChild('id',$row['id']);
    $result->addChild('name',$row['name']);
    $result->addChild('username',$row['user_name']);
    $result->addChild('lastname',$row['last_name']);
    $result->addChild('email',$row['email']);
    $result->addChild('accountnumber',$row['account_number']);
}

// You can close BD now
mysql_close($db);

//Create the XML file
//$fp = fopen("employeeData.xml","a+");

//$fp = fopen("php://output","a+");

//Write the XML nodes
//fwrite($fp,$xml->asXML()."\r\n" );

//Close file
//fclose($fp);

echo $xml->asXML();

?>
addChild('result');
$result->addChild('id',$row['id']);
$result->addChild('name',$row['name']);
$result->addChild('username',$row['user\u name']);
$result->addChild('lastname',$row['last_name']);
$result->addChild('email',$row['email']);
$result->addChild('accountnumber',$row['account\u number');
}
//现在可以关闭BD了
mysql_close($db);
//创建XML文件
//$fp=fopen(“employeeData.xml”,“a+”);
//$fp=fopen(“php://output“,”a+”;
//编写XML节点
//fwrite($fp,$xml->asXML()。“\r\n”);
//关闭文件
//fclose($fp);
echo$xml->asXML();
?>

希望对你有帮助,祝你好运

警告:这是非常不安全的,因为这些参数不是。永远不要将
$\u GET
数据直接放入查询:它会创建一个巨大的查询
mysql\u query
是一个过时的接口,不应该使用,它正在从PHP中删除。一个现代的替代品。像这样的指南解释了最佳实践。@tadman谢谢你的提示。我知道SQL注入,一旦它开始工作,我会删除/改进我的答案。它本来就不应该存在。PDO使编写没有这些问题的查询变得非常容易。在调试这样的代码时,始终保持JavaScript控制台打开。这是发现脚本错误的唯一方法。还要密切关注网络流量,特别是PHP代码的响应。@tadman会的,先生!谢谢。我已经编辑了我的答案。如果不起作用,请告诉我错误/行为。此外,还应该测试变量$\u POST['sSearchFor']是否已设置(使用isset函数)。仅测试.PHP文件以查看XML是否正确生成。如果是这样,请测试客户端请求。如果我测试php文件,则会出现以下错误:“第1列第2行的错误:文档末尾的额外内容”这会在添加您提供的代码后立即出现。请再次编辑。改用我的文件。我没有安装http服务器应用程序,所以我不能完全测试PHP,但这应该可以。(我不知道我编辑答案时是否会通知您)
$("#btnSearch").click(function () {
    var sSearchFor = $("#txtSearch").val();
    var searchLink = "ajax-search.php?sSearchFor=" + sSearchFor;
    $.ajax({
        type: "GET",
        url: "xmltable.xml",
        cache: false,
        dataType: "xml",
        success: function (xml) {
            $(xml).find('searchresults').each(function () {
                $(this).find("name").each(function () {
                    var name = $(this).text();
                    alert(name);
                });
            });
        }
    });
});
    $("#btnSearch").click(function () {
    var searchLink = "ajax-search.php";
    $.ajax({
        type: "POST",
        url: searchLink,
        data: {sSearchFor : $("#txtSearch").val() },
        cache: false,
        dataType: "json",
        success: function (xml) {
            $(xml).find('searchresults').find('result').each(function () {
                var name = $(this).find("name").text();
                alert(name);
            });
        }
    });
});
<?php 
header("Content-type: text/xml");
//Create Database connection
$db = mysql_connect("127.0.0.1","root","");
if (!$db) {
    die('Could not connect to db: ' . mysql_error());
}

//Select the Database
mysql_select_db("bank",$db);

if(isset($_POST['sSearchFor']))
    $sSearchFor = $_POST['sSearchFor'];
else
    $sSearchFor = "";

$sql = "SELECT * FROM customers WHERE name LIKE '%$sSearchFor%'";
$result = mysql_query($sql, $db) or die(mysql_error());

//Create SimpleXMLElement object
$xml = new SimpleXMLElement('searchresults');


//Add each column value a node of the XML object

while($row = mysql_fetch_assoc($result)) {
    $result= $xml->addChild('result');
    $result->addChild('id',$row['id']);
    $result->addChild('name',$row['name']);
    $result->addChild('username',$row['user_name']);
    $result->addChild('lastname',$row['last_name']);
    $result->addChild('email',$row['email']);
    $result->addChild('accountnumber',$row['account_number']);
}

// You can close BD now
mysql_close($db);

//Create the XML file
//$fp = fopen("employeeData.xml","a+");

//$fp = fopen("php://output","a+");

//Write the XML nodes
//fwrite($fp,$xml->asXML()."\r\n" );

//Close file
//fclose($fp);

echo $xml->asXML();

?>