php - 在 PHP/HTML 表单中传递一个 mysql 行

标签 php html mysql

我已经成为这里的常客了...

我正在尝试根据使用 MYSQL 语句找到的结果在 PHP 中动态打印出一个表。

看下面的代码,我得到了下面的错误

[Fri Jun 09 18:51:32.478737 2017] [fcgid:warn] [pid 63368] [client 5.69.190.95:64631] mod_fcgid: stderr: PHP Parse error: syntax error, unexpected 'showhistory' ( T_STRING),期待','或';'在第 84 行的/home/tools/public_html/searchhistory.php 中,referer:http://tools.cidetech.co.uk/history.php

似乎只有循环内部的“表单”构建有问题我在这一行之前没有问题-

 echo " <td><form method="POST" action="showhistory.php">
                                                       <input type="hidden" name="id_director" value=".$row["id"]"
                                                       </form></td> "; 

我似乎无法弄清楚我哪里出错了,这在纯 HTML 中可以正常工作,但是它需要在 mysql/php 部分内部,因为我需要通过按钮内部传递行 ID。

具体来说,这是我正在努力处理的这部分代码

 for ($i = 0; $i < count($idArray); $i++)
        {
                        $sql="SELECT * FROM history WHERE id LIKE '%{$idArray[$i]}%'";
                        $result=$con->query($sql);

                                        while($row=$result->fetch_assoc())
                                        {
                                                 echo "<tr>";
                                                 echo "<td><pre>".$row["id"]."</pre></td>";
                                                 echo "<td><pre>".$row["date"]."</pre></td>";
                                                 echo "<td><pre>".$row["domain"]."</pre></td>";
                                                 echo " <td><form method="POST" action="showhistory.php">
                                                       <input type="hidden" name="id_director" value=".$row["id"]"
                                                       </form></td> ";         
                                        }
                }
                echo "</table>";
                mysqli_close($conn);

   ?>

完整的代码可以在这里看到

   <DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link rel="stylesheet" type="text/css" href="stylesheet.css">
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title>CWCS Domain Checker Tool</title>
</head>
<body>
  <div class="header">
    <a href="index.php">
  <img src="cwcs-logo.png">
  </a>
</div>
  <hr/>
<div class="searchform">
<form action="searchhistory.php" method="post">
<label for="domain"> <input class="submit" type="text" name="domain" /> </label>
<input class="submitbutton" type="submit" name="search" value="Search for Domain" />
  </form>
</div>
<?php
#define connection info/variables needed
$servername = "localhost";
$username = "";
$password = "";
$dbname = "domainhistory";
$domain = $_POST['domain'];
$idArray = array();
#creates mysql connection
$con=new mysqli($servername,$username,$password,$dbname);
  if($con->connect_error)
  {
        echo 'Connection Faild: '.$con->connect_error;
  }
  else
  {
        $sql="SELECT * FROM history WHERE domain LIKE '%{$domain}%'";
        $result=$con->query($sql);
        #Pushes the ID of the mysql row into an array
        while($row=$result->fetch_assoc())
         {
           array_push($idArray,$row["id"]);
         }
    }
  mysqli_close($conn);
?>
<!---prints out the ID's stored in the array -->
  <?php
  $servername = "localhost";
  $username = "";
  $password = "";
  $dbname = "domainhistory";
  $con=new mysqli($servername,$username,$password,$dbname);
  if($con->connect_error)
  {
        echo 'Connection Faild: '.$con->connect_error;
  }
  else
  {
  }
  echo "<table>";
  echo "<tr>";
  echo "<th> ID </th>";
  echo "<th> Domain</th>";
  echo "<th> Date </th>";
  echo "</tr>";

## - loops through the ID array, and then prints out the data relating to that ID.
  for ($i = 0; $i < count($idArray); $i++)
        {
                        $sql="SELECT * FROM history WHERE id LIKE '%{$idArray[$i]}%'";
                        $result=$con->query($sql);

                                        while($row=$result->fetch_assoc())
                                        {
                                                 echo "<tr>";
                                                 echo "<td><pre>".$row["id"]."</pre></td>";
                                                 echo "<td><pre>".$row["date"]."</pre></td>";
                                                 echo "<td><pre>".$row["domain"]."</pre></td>";
                                                 echo " <td><form method="POST" action="showhistory.php">
                                                       <input type="hidden" name="id_director" value=".$row["id"]"
                                                       </form></td> ";         
                                        }
                }
                echo "</table>";
                mysqli_close($conn);

   ?>


</body>
</html>

最佳答案

您应该在 php 中使用 ' 而不是 "作为您的字符串,因为您在 html 标记中使用了 ",而 "用于回显。

关于php - 在 PHP/HTML 表单中传递一个 mysql 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44464152/

相关文章:

javascript - content.select() 不适用于 <code> 元素

php - 如何使用连接从三个表中选择数据

java - 在php中创建JSONArray并将其发送到android

php - WAMP 上的 LDAP 身份验证

javascript - 单击“提交”按钮时无法发布/出错

javascript - 调用函数时如何设置if条件

javascript - Yii2 的 GridView CheckboxColumn 的回调

javascript - 如何使用 jquery selected multiple select 用 flask 捕获 html 表单输入

php - 缺少一些东西,不知道是什么

MySQL json提取/搜索不匹配