gpt4 book ai didi

php - 为什么这没有发送到我的 mysql 服务器?

转载 作者:行者123 更新时间:2023-11-29 21:14:06 25 4
gpt4 key购买 nike

所以,每次我去http://localhost/api/calls.php?gamename=test&gameowner=hi&gameownerid=1&placeid=2&serverjobid=hi&serverid=jaja&serverplayers=1&sendername=bob&senderid=3&senderage=14&senderwarnings=0&calltype=non&reportinfo=hi&suspect=none

它绝对没有显示任何内容,并且不会将数据发送到我的 mysql 数据库。

这是我的代码。为了安全起见,我删除了 mysql 信息。

<?php
$servername = "";
$username = "";
$password = "";
$database = "";

// Establish MySQL Connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
die("MySafeServer Database Connection Failed: " . $conn->connect_error);
}

if (array_key_exists('param',$_GET)) {
$gamename = $_GET['param'];
$gameowner = $_GET['param'];
$gameownerid = $_GET['param'];
$placeid = $_GET['param'];
$serverjobid = $_GET['param'];
$serverid = $_GET['param'];
$serverplayers = $_GET['param'];
$sendername = $_GET['param'];
$senderid = $_GET['param'];
$senderage = $_GET['param'];
$senderwarnings = $_GET['param'];
$calltype = $_GET['param'];
$reportinfo = $_GET['param'];
$suspect = $_GET['suspect'];
mysql_query("INSERT INTO mss_calls3 (gamename, gameowner, gameownerid, placeid, serverjobid, serverid, serverplayers, sendername, senderid, senderage, senderwarnings, calltype, reportinfo, suspect) VALUES ($gamename, $gameowner, $gameownerid, $placeid, $serverjobid, $serverid, $serverplayers, $sendername, $senderid, $senderage, $senderwarnings, $calltype, $reportinfo, $suspect)");
};
?>

最佳答案

@Mark 是对的,您应该坚持仅使用 mysqli 函数。

正如 @andrewsi 所说,由于您没有查询数据,因此代码中没有任何内容可以打印插入语句是否成功,而仅在失败时打印,因此我添加了“成功!” echo 。您仍然需要查询数据库以查看值是否已插入。

@Matt 和 @Mark 关于准备语句的观点对于清理您的输入至关重要 - 这是安全 101,您应该对其进行一些谷歌搜索。

但最终,我认为@CodeGodie 解决了你最大的问题,就是让它正常工作。您可以使用 $_GET['param'] 将所有变量分配给相同的值,除了最后的“suspect”。从您在问题中发布的链接来看,您的查询字符串中没有“param”。我不完全确定您想要什么,但我假设您想要将参数名称与变量名称相匹配。我不认为它是这样工作的,但是以下未经测试的代码应该可以帮助您:

<?php

$params = array(
"gamename",
"gameowner",
"gameownerid",
"placeid",
"serverjobid",
"serverid",
"serverplayers",
"sendername",
"senderid",
"senderage",
"senderwarnings",
"calltype",
"reportinfo",
"suspect"
);

$cols = "";
$vals = "";
$binding_type = "";
$get_params = array();

// first pass to build the query,
// and validate inputs exist
for ($params as $param) {
if ( isset($_GET["$param"]) ) {
$cols .= "$param,";
$vals .= "?,";
$get_params []= $_GET["$param"];
// determine the binding type as either integer or string
if (is_numeric($_GET["$param"]))
$binding_type .= "i";
else
$binding_type .= "s";
} else die("$param is not set");
}

// trim trailing commas
$cols = rtrim($cols, ",");
$vals = rtrim($vals, ",");

$sql = "INSERT INTO mss_calls3 ($cols) VALUES ($vals);";

$servername = "";
$username = "";
$password = "";
$database = "";

// Establish MySQL Connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
die("MySafeServer Database Connection Failed: " . $conn->connect_error);
}

// prepare statement
$stmt = $conn->prepare($sql) or die($conn->error);

// bind parameters
// watch this is the tricky dynamic part I got help from the following, but may need some work:
// http://stackoverflow.com/questions/627763/php-and-mysqli-bind-parameters-using-loop-and-store-in-array
// http://no2.php.net/manual/en/mysqli-stmt.bind-param.php#89171
call_user_func_array( array($stmt, 'bind_param'), array_merge(array($stmt, $binding_type), $get_params));

// execute
if( $stmt->execute() )
echo "success!";
else
echo $stmt->error;

$stmt->close();
$conn->close();

?>

关于php - 为什么这没有发送到我的 mysql 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36107956/

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