2

我对 PHP/MySQL 非常陌生,我不知道最好的工具集或如何正确调试。

这是我正在处理的代码片段。我有两个主要问题。

1:我注意到转义序列不起作用。在页面显示中忽略所有 \n。

2:我的代码没有进入while循环,因此没有显示结果。当我在 phpMyAdmin 上运行相同的查询时,我得到一个结果。

这是输出:

我在你的数据库中!echoinside post我们有一个结果表存在名称:outside post

<?php
$mysqli = new mysqli("****", "*****", "******", "******");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo "I'm in your db!";
echo "echo";
if( $_POST["submit"] ) {
echo "inside post"; 
    if (!($stmt = $mysqli->prepare("SELECT Cust_name FROM SERVICE_TICKET WHERE Ticket_num=?"))) {
            echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }
    if (!$stmt->bind_param("i", $_POST['ticket'])) {
        echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
    }
    if (!$stmt->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
    }
    if (!$stmt->bind_result($Cust_name)) {
        echo "Binding output parameters failed: (" . $stmt->errno . ") " . $stmt->error;
    }
    else {
        echo "We have a result";
        echo "<table><br/>";
        echo "table exists";
        printf("<br/>\n\n\n\n\nname:  %s", $Cust_name);
        while ($stmt->fetch()){
            echo "in while loop";
            echo "<tr>\n<td>".$Cust_name."</td>\n</tr>";
        }
        echo "</table>";
    }

}
echo "outside post";
?>

为表单添加 html:

<head>
    <title>Bicycle Store Service Ticket</title>
    <link rel="stylesheet" type="text/css" href="web.css" />
</head>
<body>
    <h1>ACME BICYCLE SHOP</h1>
    <h3 align="center">GET IN GEAR!</h3>

    <form action="search_service.php" method="post">
    <h3>CHECK A SERVICE TICKET</h3>

    <h4>Ticket Number: <input type="text" ticket="ticket">&nbsp;&nbsp;

    <input type="submit" name="submit" value="Search">
    </form>

    <p><a href="index.html">HOME</a></p>
</body>
</html>

任何建议都会非常有帮助。

4

2 回答 2

3

问题 #1:转义序列正在工作,但在 HTML 中,换行符是“无关紧要的空白”,因此不会显示。<br>您可以使用标签添加换行符。

问题 #2:看起来您的查询返回一个空结果集(没有指定编号的票证),因此第一次调用$stmt->fetch()返回 false。

编辑: HTML 中带有“ticket”输入的行应如下所示:

<h4>Ticket Number: </h4><input type="text" name="ticket">&nbsp;&nbsp;

所以它name="ticket"不是ticket="ticket",并且<h4>标签应该被关闭。

于 2012-11-18T21:51:30.110 回答
0

1:我注意到转义序列不起作用。在页面显示中忽略所有 \n。

\n不是转义字符。它是换行符(LF ala 换行符)。并且它在 HTML 外部块中被忽略,<PRE>因此要么添加<PRE>(但这会“杀死”标记,或者使用<br />这意味着 exaclty\n但“在 HTML 中”。

PS:不需要的请勿使用?>

于 2012-11-18T21:52:55.447 回答