使用switch case将PHP函数和数组作为参数,更新MySQL数据库

使用switch case将PHP函数和数组作为参数,更新MySQL数据库,php,mysql,arrays,Php,Mysql,Arrays,HTML代码:- <form action="run.php" method="post"> Ambulance ID:<input type="text" name="amb_id"> Select the any of the point and submit: <input type="radio" name="tposition" value="1">t1 (1 km away from the signal) <input type="rad

HTML代码:-

<form action="run.php" method="post">
Ambulance ID:<input type="text" name="amb_id">
Select the any of the point and submit:
<input type="radio" name="tposition" value="1">t1  (1 km away from the signal)
<input type="radio" name="tposition" value="2">t2 (before 500 mtrs point) 
<input type="radio" name="tposition" value="3">a3 (500 Mtr from signal)
<input type="radio" name="tposition" value="4">t3 (before signal, after 500 mtrs)
<input type="submit" value="Submit">
</form>
<?php
$ambid = $_POST['amb_id'];
//lattitude array
$lat=array(13.092593,13.092781,13.093126,13.09344,13.093889,13.094349,13.094882,13.095485,13.096575);

//longitude array
$lon=array(77.586415,77.585009,77.583454,77.58251,77.581598,77.580793,77.580096,77.57946,77.578486);

//connect to the db
$con = mysql_connect('localhost', 'root','');
mysql_select_db('traffic', $con);

//check the radio button
  if (isset($_POST['tposition'])) {
     switch($_POST['tposition']) {
    case 1:
        updateDb($lat[0],$lon[0]);
        break;
    case 2:
        updateDb($lat[1],$lon[1]);
        break;

        }
}

    else { echo "Please select any of the tpositon radio button"; }

function updateDb($lati,$longi)
{
$query = "UPDATE emergency SET e_latitude=$lati,e_longitude=$longi WHERE amb_id=$ambid ";
$res= mysql_query($query) or die("Unable to update the latlong values because : " . mysql_error());

}
mysql_close($con);

?>

救护车号码:
选择任一点并提交:
t1(距离信号机1公里)
t2(500米站前)
a3(距离信号机500米)
t3(信号前,500米后)
PHP代码:-

<form action="run.php" method="post">
Ambulance ID:<input type="text" name="amb_id">
Select the any of the point and submit:
<input type="radio" name="tposition" value="1">t1  (1 km away from the signal)
<input type="radio" name="tposition" value="2">t2 (before 500 mtrs point) 
<input type="radio" name="tposition" value="3">a3 (500 Mtr from signal)
<input type="radio" name="tposition" value="4">t3 (before signal, after 500 mtrs)
<input type="submit" value="Submit">
</form>
<?php
$ambid = $_POST['amb_id'];
//lattitude array
$lat=array(13.092593,13.092781,13.093126,13.09344,13.093889,13.094349,13.094882,13.095485,13.096575);

//longitude array
$lon=array(77.586415,77.585009,77.583454,77.58251,77.581598,77.580793,77.580096,77.57946,77.578486);

//connect to the db
$con = mysql_connect('localhost', 'root','');
mysql_select_db('traffic', $con);

//check the radio button
  if (isset($_POST['tposition'])) {
     switch($_POST['tposition']) {
    case 1:
        updateDb($lat[0],$lon[0]);
        break;
    case 2:
        updateDb($lat[1],$lon[1]);
        break;

        }
}

    else { echo "Please select any of the tpositon radio button"; }

function updateDb($lati,$longi)
{
$query = "UPDATE emergency SET e_latitude=$lati,e_longitude=$longi WHERE amb_id=$ambid ";
$res= mysql_query($query) or die("Unable to update the latlong values because : " . mysql_error());

}
mysql_close($con);

?>

运行上述脚本时,我得到一个错误,如下所示 “无法更新latlong值,因为:您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以了解在第1行的“”附近使用的正确语法。”


但是,如果在每种情况下都使用相同的代码行,则不使用函数。为什么呢?您能帮我提前谢谢吗。

可能发生此错误,因为您正在插入
字符,而没有在您插入mysql查询的值周围使用
mysql\u real\u escape\u string()
。我建议你像这样封装你的价值观

$ambid = mysql_real_escape_string($_POST['amb_id']); 
但是,我不建议您使用mysql函数,因为它与您的代码一样,非常弱,并且可用于mysql注入。学习PDO。试试这个

function updateDb($lati,$longi)
{
global $ambid;
$lati = mysql_real_escape_string($lati);
$longi = mysql_real_escape_string($longi);
$ambid = mysql_real_escape_string($ambid);
$query = "UPDATE emergency SET `e_latitude`='$lati',`e_longitude`='$longi' WHERE `amb_id`='$ambid' ";
$res= mysql_query($query) or die("Unable to update the latlong values because : " . mysql_error());

}