【问题标题】:How do I convert $_POST to string then assign to a $variable? Wanna use it in SQL如何将 $_POST 转换为字符串然后分配给 $ 变量?想在 SQL 中使用它
【发布时间】:2013-07-30 08:27:35
【问题描述】:

我不知道如何将此 $_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>

【问题讨论】:

  • 您的if($results != $sql) 条件完全错误。至少测试一下$results !== false
  • 不要使用mysql_*函数,改用mysqli或PDO
  • SQL 注入 ftw ! :-)
  • $DeptCode 打印出什么?

标签: php arrays parsing variables post


【解决方案1】:

像这样使用不带括号的[]

name='Department'

【讨论】:

  • 为什么这被否决了?这是迄今为止唯一正确的答案。同一组中的单选按钮需要相同的名称,因此不应是 OP 命名的数组。
【解决方案2】:

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

<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 表单中的数组,请参阅:

【讨论】:

    【解决方案3】:

    您可以为此目的使用 PHP 变量。变量变量获取变量的值并将其视为变量的名称。例如:

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

    参考:http://php.net/manual/en/language.variables.variable.php

    【讨论】:

      【解决方案4】:

      就用这个吧:

      implode("", $DeptCode);
      

      【讨论】:

        【解决方案5】:

        我想它会帮助你。

        $post_string = json_encode($your_post_variable);
        

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

        您也可以通过

        获取发布数据值
        json_decode();
        

        【讨论】:

          【解决方案6】:

          试试这个:

          $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。更安全

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-05-28
            • 1970-01-01
            • 2019-12-05
            • 1970-01-01
            • 1970-01-01
            • 2018-02-18
            • 1970-01-01
            • 2018-03-28
            相关资源
            最近更新 更多