执行代码时出现以下错误:
[client 127.0.0.1:50592] PHP Notice: Undefined index: abonent in C:\\Bitnami\\wampstack-7.1.19-0\\apache2\\htdocs\\Piggyservice\\show-balance.php on line 5
[client 127.0.0.1:50592] PHP Warning: mysqli_query(): Empty query in C:\\Bitnami\\wampstack-7.1.19-0\\apache2\\htdocs\\Piggyservice\\Common.php on line 27
[client 127.0.0.1:50592] PHP Warning: mysqli_query(): Empty query in C:\\Bitnami\\wampstack-7.1.19-0\\apache2\\htdocs\\Piggyservice\\Common.php on line 28
[client 127.0.0.1:50592] PHP Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\\Bitnami\\wampstack-7.1.19-0\\apache2\\htdocs\\Piggyservice\\Common.php on line 28
这是common.php文件
<?php
require_once 'config.php';
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'wp_fs';
$con = mysqli_connect("127.0.0.1", "root", " ", "wp_fs") or die("Unable to Connect to '$dbhost'");
function getConnection()
{
$con = mysqli_connect("127.0.0.1", "root", " ", "wp_fs");
if(!mysqli_select_db( $con,"wp_fs"))
die('Could not select database');
return $con;
}
function createAccount($abonent)
{
$con = getConnection();
$query = "INSERT INTO `accounts`(`abonent`, `balance`) values($abonent,
".DEFAULT_BALANCE.")";
if(!mysqli_query($con,$query))
die('createAccount: Query failed: ' . mysqli_error());
}
function getBallance($abonent)
{
$con = getConnection();
$query = mysqli_query($con,"SELECT `balance` FROM `accounts` WHERE `abonent` = $abonent");
$result = mysqli_query($con,$query );
if(!mysqli_query($con, $query)) die('getBallance: Query failed: ' . mysqli_error());
if(mysqli_num_rows($result) == 0)
{
return -1;
}
$data = mysqli_fetch_array($result);
return $data['balance']; }
function setBalance($abonent, $balance)
{
$con = getConnection();
$query = "UPDATE `accounts` SET `balance`= $balance WHERE `abonent` = $abonent";
if(!mysqli_query($con,$query))
die('setBalance: Query failed: ' . mysqli_error());
}
?>
我想做的是在调用函数show-balance.php时获得平衡
<?php
require_once 'common.php';
if(isset($_POST['abonent']))
die("Not enough parameters");
$abonent = $_POST['abonent'];
$balance = getBallance($abonent);
if($balance == -1)
{
createAccount($abonent);
$balance = DEFAULT_BALANCE;
}
header('Content-Type: text/xml');
print '<?xml version="1.0" encoding="UTF-8"?>'
?>
<page version="2.0">
<title>Balance</title>
<div>
Your balance is <?=$balance?>
</div>
<navigation>
<link accesskey="0" pageId="a01.xml" type="back">Back</link>
</navigation>
</page>
测试数据库只有一个带有两列的表(帐户) “平衡”和“平衡”
在测试链接时:
http://127.0.0.1/pigyservice/show-balance.php?abonent=12406774740&subscriber=12406774740
我有以下错误: getBallance:查询失败
答案 0 :(得分:0)
您正在尝试在URI查询中传递参数,并且这些值可以在PHP中使用$ _GET或$ _REQUEST进行访问,并且您正在检查$ _POST。
您还要检查 $ _ POST [“ abonent”] 是否有任何值,您需要反转逻辑以检查其是否为空:
if ( ! isset ( $_POST["abonent"]))
{
die ( "Not enough parameters");
}
您可以使用$ _REQUEST(混合GET和POST请求变量),方法是:
if ( ! isset ( $_REQUEST["abonent"]))
{
die ( "Not enough parameters");
}
$abonet = $_REQUEST["abonent"];
此外,请谨慎使用SQL注入,不要将外部参数直接传递给查询。您可以在此链接中查看有关PHP SQL Injection的信息。