下面代码的问题是变量sp1。当用数据库中已经存在的城市名称将sp1替换为"where"子句时,一切都很好。但当我从应用程序发送startPoli1变量时,php什么也不返回。Logcat显示每次都发送startPoli1。任何建议;
<?php
$con=mysql_connect("......","......","......" );
$database = "......";
$ok = mysql_select_db($database, $con);
mysql_set_charset("UTF8",$con);
$us1 = $_POST['username1'];
$sp1 = $_POST['startPoli1'];
$fp1 = $_POST['finalPoli1'];
$w1 = $_POST['weight1'];
$em1 = $_POST['eidosmetaf1'];
$dD1 = $_POST['depDate1'];
$dT1 = $_POST['depTime1'];
$sql = mysql_query( " SELECT `username1`,`startPoli1`, `finalPoli1`, `eidosmetaf1`, `weight1` , `depDate1` , `depTime1`, `tilefono1`
FROM customer ,registration1
where (customer.startPoli1 = 'sp1') and
(customer.username1 = registration1.username )");
if($sql === FALSE)
{
die(mysql_error());
}
$results = array();
while($row = mysql_fetch_assoc($sql))
{
$results[] = array(
'username1' => $row['username1'],
'startPoli1' => $row['startPoli1'],
'finalPoli1' => $row['finalPoli1'],
'eidosmetaf1' => $row['eidosmetaf1'],
'weight1' => $row['weight1'],
'depDate1' => $row['depDate1'],
'depTime1' => $row['depTime1'],
'tilefono1' => $row['tilefono1']
);
}
echo json_encode(array('select_itin_results' =>$results));
mysql_close($con);
?>
您没有使用变量$sp1的值
相反,您使用的是字符串'sp1'
,您的子句中缺少美元符号$
:
where (customer.startPoli1 = 'sp1')
应更改为:
where (customer.startPoli1 = '$sp1')
但要注意解决方案带来的威胁:
您很容易受到sql注入的攻击,可以通过停止使用mysql_*
函数来避免这种情况,因为它们已被弃用,您应该开始使用PDO或mysqli_*
的准备语句。你可以通过查看其他关于此事的有用帖子来了解情况。
您忘记在变量名之前添加$
。即where (customer.startPoli1 = '$sp1')
,因此最终查询使用字符串"sp1"而不是变量$sp1
的值
了解后,了解进行sql查询的正确方法,以避免人们使用sql注入