使用MySQL搜索的PHP代码在IE中中断但在Chrome中有效

时间:2017-11-06 17:13:45

标签: php mysql google-chrome internet-explorer foreach

有人可以像我一样帮助找到为什么这在Chrome中可行但在IE中中断?这段代码可能不是很好地组合在一起,但我只是开始学习而且我很自豪我得到了它的工作。 :)

第一个代码块将文本从textarea发送到输出页面。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>-</title>
</head>
<body>
<form action="output.php" method="post" id="artsearch">
<input type="submit" name="submit" value="Search">
</form><br>
<textarea name="textareaname" form="artsearch" cols="53" rows="50"></textarea>
</body>
</html>

此页面以表格形式输出数据。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>-</title>
</head>    
<body>    
<?php
// Database ---------------------------------------------------------
$servername = "-";
$username = "-";
$password = "-";
$dbname = "-";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
    {
    die("Connection failed: " . $conn->connect_error);
    }
// Database ---------------------------------------------------------

$arr = explode("\n", $_POST['textareaname']);

echo "<table>
<tr><td><b><u>ID<td><b><u>Item<td><b><u>Place</b>";

foreach ($arr as $value) 
        {
    $sql = "SELECT * FROM database1 WHERE id=$value";
    $res = $conn->query($sql);

    while($row=$res->fetch_assoc())
        {
        echo "<tr><td>".$row["id"]."<td>".$row["item"]."&nbsp;&nbsp;&nbsp;<td>".$row["place"]; 
        }
        }
echo "</table>";
$conn->close();
?>

</body>
</html>

我不知道IE中断了什么,输出似乎在&#34; foreach&#34;之前停止了。命令,这可能是罪魁祸首吗?

4 个答案:

答案 0 :(得分:2)

您的HTML已损坏。 Chrome会自动更正损坏的标签,这就是它在IE中中断的原因。每</td>您应该有一个<td>。也许,标题应该放在<th></th>标签中?

应该是:

echo "<table>

<tr><th><b><u>ID</u></b></th><th><b><u>Item</u></b></th><th><b><u>Place</b></u></th></tr>";

foreach ($arr as $value) 
        {
    $sql = "SELECT * FROM database1 WHERE id=$value";
    $res = $conn->query($sql);

    while($row=$res->fetch_assoc())
        {
        echo "<tr><td>".$row["id"]."</td><td>".$row["item"]."&nbsp;&nbsp;&nbsp;</td><td>".$row["place"]."</td></tr>"; 
        }
        }
echo "</table>";

答案 1 :(得分:0)

真的很奇怪,它不再有效。这是我的代码,我是否意外改变了其他内容?我现在只是在IE浏览器中获取标题,但Chrome中的一切正常。

count

答案 2 :(得分:0)

你忘记了行尾的</tr>。 :)

答案 3 :(得分:0)

我希望这很容易,但仍然无法奏效。完整的代码(数据库内容除外);

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>-</title>
</head>

<body>

<?php
// Database ---------------------------------------------------------
$servername = "-";
$username = "-";
$password = "-";
$dbname = "-";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
    {
    die("Connection failed: " . $conn->connect_error);
    }
// Database ---------------------------------------------------------

$arr = explode("\n", $_POST['textareaname']);

echo "<table>

<tr><th><b><u>ID</u></b></th><th><b><u>Item</u></b></th><th><b><u>Place</b></u></th></tr>";

foreach ($arr as $value) 
        {
    $sql = "SELECT * FROM database1 WHERE id=$value";
    $res = $conn->query($sql);

    while($row=$res->fetch_assoc())
        {
        echo "<tr><td>".$row["id"]."</td><td>".$row["item"]."&nbsp;&nbsp;&nbsp;</td><td>".$row["place"]."</td></tr>"; 
        }
        }
echo "</table>";
$conn->close();
?>
</body>
</html>

这是get in IE中的输出源;

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>-</title>
</head>

<body>

<table>

<tr><th><b><u>ID</u></b></th><th><b><u>Item</u></b></th><th><b><u>Place</b></u></th></tr>

即使这在IE中也不行,所以我猜这个表不是问题所在。让我疯狂。

// Database ---------------------------------------------------------

$arr = explode("\n", $_POST['textareaname']);

foreach ($arr as $value) 
        {
    $sql = "SELECT * FROM database1 WHERE id=$value";
    $res = $conn->query($sql);    

    while($row=$res->fetch_assoc())
        {               
        echo $row["id"]; 
        }
        }

$conn->close();
?>
</body>
</html>