gpt4 book ai didi

php - 通过 PHP 的 XML 请求 MySQL

转载 作者:行者123 更新时间:2023-11-30 23:16:13 24 4
gpt4 key购买 nike

实际上,我正在使用 XML 在 Flash 网页中工作。我想用 XML 创建一个使用 PHP 将数据插入 de MySQL 数据库的单页,但我被卡住了...而且我的 XML-PHP 知识不足以完成这项任务。

这是 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<data>
<title>Sing up</title>
<request field1="user" field2="email" field3="password" field4="other">reg.php</request>
<description><![CDATA[Please sing up!]]></description>
</data>

reg PHP 文件:

        <?php

function Reg()
{
if (isset($_POST['reg'])==true) {
require_once('db_conf.php');
$user = $_POST['username'];
$pass = sha1(strtoupper($user.':'.$_POST['password']));
$email = $_POST['email'];

$con = mysql_connect($dbhost, $dbuser, $dbpassword);

if (!$con)
{
die('Could not connect!');
} else {
mysql_select_db("$logondb", $con);

$sql="INSERT INTO accounts (username, sha_pass_hash, email) VALUES ('$user','$pass','$email')";

if (!mysql_query($sql,$con))
{
die('Error creating account.');
}

echo $succesmsg;

mysql_close($con);

}


} else {
?>

以及用于数据库的 PHP conf:

    <?php
$dbhost = 'localhost';

$dbuser = 'user';

$dbpassword = 'password';

// Accounts Database
$logondb = 'accounts';

$errormsg="Error creating account..";

$succesmsg="Account created!";
?>

最佳答案

我想建议改用 PDO - 这是一种更通用的方法。然后你应该使用数据绑定(bind)来减少针对 SQL 注入(inject)的漏洞......

这会让你更进一步:

<?php

function Reg()
{
if (isset($_POST['reg'])==true) {
require_once('./db_conf.php');
$user = $_POST['username'];
$pass = sha1(strtoupper($user.':'.$_POST['password']));
$email = $_POST['email'];

$dsn = "mysql:host=$dbhost;dbname=$logondb";
$pdo = new PDO($dsn,$dbuser,$dbpassword);

if (!$pdo)
{
die('Could not connect!');
} else {
$sql = "INSERT INTO accounts (username, sha_pass_hash,email) VALUES (:user,:pass,:email)";
$stmt = $pdo->prepare($sql);
$res = $stmt->execute(array("user"=>$user , "pass"=>$pass , "email" => $email));
if (!$res) {
echo "Error :((<pre>";
var_dump($stmt->errorInfo());
echo "</pre>";
} else
{
echo $succesmsg;
}


}
} else {
echo "reg was not set - terminating...!";
}

}

Reg(); // execute it!

?>

关于php - 通过 PHP 的 XML 请求 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17695880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com