php mysql where子句不读取变量

时间:2015-12-15 06:58:41

标签: php mysql where

我开始对此感到愤怒

mysql_query无法识别我的变量$d1,即使我尝试重命名

这是代码..

HTML:

<form action ="manageVessel.php" method ="POST">
  <select onchange ="this.form.submit();" class ="form-control" name ="ViewPositionCertificates">
      <option>Choose a Position </option>                                                       
      <?php
      $ViewPCertificates = mysql_query("SELECT * FROM table_cmsjob") or die("error" . mysql_error());
      while ($rwViewPCertificates = mysql_fetch_array($ViewPCertificates)) {
          ?>
          <option value =" <?php echo $rwViewPCertificates['jobName']; ?> "> <?php echo $rwViewPCertificates['jobName']; ?></option>  
      <?php } ?>

       </select>   
</form>

PHP:

   <?php if (isset($_POST['ViewPositionCertificates'])) { ?>
      <table class = "table table-bordered">
          <tr class ="bg-primary">
              <td> List of Certificates </td>
          </tr>
          <?php
          $d1 = $_POST['ViewPositionCertificates'];
          echo $_POST['ViewPositionCertificates'];
          $ViewCertificatesFP = mysql_query("SELECT * FROM table_cmsjobassigning WHERE jobName =  '$d1' ") or die("error" . mysql_error());

          while ($rwViewCertificatesFP = mysql_fetch_array($ViewCertificatesFP)) {

              echo "<tr>";
              echo "<td>" . $rwViewCertificatesFP['Certificate'] . "</td>";
              echo "</tr>";
          }
          ?>

      </table>




  <?php } ?>
当我使用字符串例如

时,

MYSQL WHERE子句正常工作

    mysql_query("SELECT * FROM table_cmsjobassigning WHERE jobName =  'MASTER' ") or die("error" . mysql_error());

但是当我使用变量将$_POST['ViewPositionCertificates']分配给变量时,MYSQL WHERE子句不会读取任何帮助吗?

6 个答案:

答案 0 :(得分:3)

<option value ="<?php echo $rwViewPCertificates['jobName']; ?>"> <?php echo $rwViewPCertificates['jobName']; ?></option>   // remove xtra spaces from here......

value属性

中删除空格

答案 1 :(得分:1)

试试这样..

$ViewCertificatesFP = mysql_query("SELECT * FROM table_cmsjobassigning WHERE jobName =  '".mysql_real_escape_string(trim($d1))."' ") or die("error" . mysql_error());

$ViewCertificatesFP = mysql_query("SELECT * FROM table_cmsjobassigning WHERE jobName =  '".addslashes(trim($d1))."' ") or die("error" . mysql_error());

答案 2 :(得分:0)

<?php if (isset($_POST['ViewPositionCertificates'])) { ?>
    <table class = "table table-bordered">
        <tr class ="bg-primary">
            <td> List of Certificates </td>
        </tr>
        <?php
        $d1 = $_POST['ViewPositionCertificates'];
        echo $_POST['ViewPositionCertificates'];
        $ViewCertificatesFP = mysql_query("SELECT * FROM table_cmsjobassigning WHERE jobName =  '".$d1."' ") or die("error" . mysql_error());

        while ($rwViewCertificatesFP = mysql_fetch_array($ViewCertificatesFP)) {

            echo "<tr>";
            echo "<td>" . $rwViewCertificatesFP['Certificate'] . "</td>";
            echo "</tr>";
        }
        ?>
    </table>
<?php } ?>

选择查询语法的更改使用单引号。

答案 3 :(得分:0)

试试这个:

mysql_query("SELECT * FROM table_cmsjobassigning WHERE jobName =  ".$_POST['ViewPositionCertificates'])

答案 4 :(得分:0)

您不能在单引号内编写变量,如果您编写它,那么PHP会将其视为字符串。 所以你的查询将是
log4net

如需更多帮助,请阅读PHP中的变量插值

答案 5 :(得分:-1)

尝试更改您的查询方法可能有所帮助......

$ViewCertificatesFP = mysql_query("SELECT * FROM table_cmsjobassigning WHERE jobName =  '".$d1."' ") or die("error" . mysql_error());