0

我不知道如何将此 $_POST 转换为字符串并将其分配给变量而不是数组。是否有像 C# 中的 Convert.ToString(RadioButtonValue) ?我想在我的 SQL 语句中使用该变量作为参数。

$DeptCode = $_POST['Department'];
        print_r($DeptCode);

        $sql = "SELECT EMPLOYEE.EmpID, EmpName FROM EMPLOYEE, EMPLOYEE_SPECIALIZATION WHERE EMPLOYEE.EmpID = EMPLOYEE_SPECIALIZATION.EmpID AND EmpStatus='active' AND DeptCode = '$DeptCode'";
        $results = mysql_query($sql,$con);
        if($results != $sql)
        {
            die('Error' . mysql_error());
        }

这是我的 SQL 语句。我究竟做错了什么?

$sql = "SELECT EMPLOYEE.EmpID, EmpName FROM EMPLOYEE, EMPLOYEE_SPECIALIZATION WHERE EMPLOYEE.EmpID = EMPLOYEE_SPECIALIZATION.EmpID AND EmpStatus='active' AND DeptCode = '$DeptCode'";

当我运行它时......它总是显示

Array ( [0] => PD ) Error

这是整个代码:

<html>
<head>
    <title>New Checkup</title>
</head>
<body>
    <h1><a href="http://localhost/clinic/InsertPatient.php">Insert Patient</a></h1><br>
    <h1><a href="http://localhost/clinic/InsertEmployee.php">Insert Doctor and Specialization</a></h1>
    <h1><a href="http://localhost/clinic/InsertProcedureHTML.php">Insert Products and Services</a></h1>
    <h1><a href="http://localhost/clinic/NewCheckup.php">New Checkup</a></h1>
    <form method="post">
        <?php
        //action="http://localhost/clinic/NewCheckup2.php"
            $con = mysql_connect('localhost', 'root', "");
            if(!$con)
            {
                die('Could not connect: ' . mysql_error());
            }   
            mysql_select_db("db_clinic", $con) or die(mysql_error());
            $sql = "SELECT DeptCode, DeptName FROM DEPARTMENT";
            $results = mysql_query($sql,$con);
            while($row=mysql_fetch_assoc($results))
            {                       
                echo "<input type='radio' name='Department[]' value='".$row['DeptCode']."'>".$row['DeptName'];
            }
            mysql_close($con);
        ?>
        <input type="submit" name="btnSubmit">
    </form>

    <?php
    if(isset($_POST['btnSubmit']))
    {
        $con = mysql_connect('localhost', 'root', "");
        if(!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        mysql_select_db("db_clinic", $con) or die(mysql_error());

        $DeptCode = $_POST['Department'];
        print_r($DeptCode);
        echo $DeptCode;
        $sql = "SELECT EMPLOYEE.EmpID, EmpName FROM EMPLOYEE, EMPLOYEE_SPECIALIZATION WHERE EMPLOYEE.EmpID = EMPLOYEE_SPECIALIZATION.EmpID AND EmpStatus='active' AND DeptCode = '$DeptCode'";
        $results = mysql_query($sql,$con);
        if($results != $sql)
        {
            die('Error' . mysql_error());
        }
        mysql_close($con);
    }
    ?>
</body>

4

6 回答 6

2

Use without brackets [] like this

name='Department'
于 2013-07-30T08:29:33.010 回答
1

您正在使用表单输入创建一个数组:

<input type='radio' name='Department[]' value='
                                    ^^
                                    ||
                     PHP HTML Form Variable Array Notation

这也会在您进行print_r调试时显示:

$DeptCode = $_POST['Department'];
print_r($DeptCode);

Array ( [0] => PD ) 

因此,要么不创建数组,要么将其作为数组访问。

要了解有关 PHP 中数组的更多信息,请参阅:

要了解有关 PHP 表单中的数组的更多信息,请参阅:

于 2013-07-30T08:32:38.450 回答
0

You can use PHP Variable variables for this purpose. A variable variable takes the value of a variable and treats that as the name of a variable. Eg:

foreach($_POST as $key => $value){
 $$key = $value; //create variable
}

Reference: http://php.net/manual/en/language.variables.variable.php

于 2013-07-30T08:30:00.200 回答
0

just use this :

implode("", $DeptCode);
于 2013-07-30T08:30:08.020 回答
0

尝试这个:

$deptCode = $_POST['Department'];

$post_array = implode(" ", array_keys($deptCode));
$escaped_values = array_map('mysql_real_escape_string', array_values($deptCode));
$newDeptCode = implode(" ", $escaped_values);

要在 mysql 语句中使用它:

//tokenize the string
$token = strtok($newDeptCode, " ");

while($token != FALSE){

   //your query statement
   mysql_query("SELECT EMPLOYEE.EmpID, EmpName FROM EMPLOYEE, EMPLOYEE_SPECIALIZATION WHERE EMPLOYEE.EmpID = EMPLOYEE_SPECIALIZATION.EmpID AND EmpStatus='active' AND DeptCode = '$token'");

   $token = strtok(" ");
}

我强烈建议您开始使用 PDO。它更安全

于 2013-07-30T08:56:12.707 回答
0

我想它会帮助你。

$post_string = json_encode($your_post_variable);

现在你有了 $post_string 中的字符串值。

您也可以通过以下方式获取发布数据值

json_decode();
于 2013-07-30T08:33:57.353 回答