[英]php mySQL returns an empty query
我正在嘗試創建一個“計費歷史記錄”頁面,用戶可以在其中查看具有表格格式購買歷史的表格。 我正在嘗試向每行添加一個鏈接,以便用戶可以看到每個訂單的完整發票詳細信息。
當他們單擊鏈接時(我已經在此腳本之后包括了概述頁面的腳本),該腳本應該執行查詢,但我只是得到一個空消息查詢。
誰能發現錯誤?
謝謝!!
歐仁妮
<?php
include("mainfile.php");
include(XOOPS_ROOT_PATH."/header.php");
$host="localhost"; // Host name
$username="user"; // Mysql username
$password="pass"; // Mysql password
$db_name="db"; // Database name
$tbl_name="table"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$TicketID=$_GET['TicketID'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE TicketID='$TicketID'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
// Get a specific result from the "example" table
$result = mysql_query($Sql) or die(mysql_error());
echo "<table width='100' border='1' cellpadding='0' cellspacing='0' id='records'>";
print "<h3 align='center'><strong>Billing History</strong></h3><p>";
echo "<tr>
<th width='110' align='center'>Billing Date</th>
<th width='80' align='center'>Ticket #</th>
<th align='center'>Project Title </th>
<th width='80' align='center'>Total GBP</th>
</tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr height='22' width='100' bordercolor='343434' align='center'><td>";
echo $row['date'];
echo "</td><td> ";
echo $row['TicketID'];
echo "</td><td> ";
echo $row['project'];
echo "</td><td> ";
echo $row['grandTotal'];
}
echo "</table>";
include(XOOPS_ROOT_PATH."/footer.php");
?>
這是一個腳本,它顯示完整的計費歷史記錄,並且包含與執行查詢的腳本的鏈接(上述)。
<?php
include("mainfile.php");
include(XOOPS_ROOT_PATH."/header.php");
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="db name"; // Database name
$tbl_name="tbl name"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name WHERE uid='";
$sql=$sql . $xoopsUser->uid("s") . "' AND Paid='Y'";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>
<tr>
<td align="center"><strong>Billing Date</strong></td>
<td align="center"><strong>Invoice Number</strong></td>
<td align="center"><strong>Description</strong></td>
<td align="center"><strong>Total GBP</strong></td>
<td align="center"><strong>View</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['date']; ?></td>
<td><? echo $rows['TicketID']; ?></td>
<td><? echo $rows['project']; ?></td>
<td><? echo $rows['grandTotal']; ?></td>
<td align="center"><a href="http://website.co.uk/site/viewInvoice.php?TicketID=<? echo $rows['TicketID']; ?>">View</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
include(XOOPS_ROOT_PATH."/footer.php");
?>
改變這個:
// Get a specific result from the "example" table
$result = mysql_query($Sql) or die(mysql_error());
對此:
$result = mysql_query($sql) or die(mysql_error());
在viewInvoice.php中,還可以查看其他人關於安全性等的評論。
檢查這些行:
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE TicketID='$TicketID'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
// Get a specific result from the "example" table
$result = mysql_query($Sql) or die(mysql_error());
您正在用mysql_query
的返回值覆蓋$result
變量,該返回值獲取了不存在的參數: $Sql
PHP變量名稱區分大小寫!
改變這個
<a href="http://website.co.uk/site/viewInvoice.php?TicketID=<? echo $rows['TicketID']; ?>">View</a></td>
對此
<?php
echo "href=http://website.co.uk/site/viewInvoice.php?TicketID=" . $rows['TicketID'] . ">View</a>";
?>
<?php
include("mainfile.php");
include(XOOPS_ROOT_PATH."/header.php");
$host="localhost"; // Host name
$username="user"; // Mysql username
$password="pass"; // Mysql password
$db_name="db"; // Database name
$tbl_name="table"; // Table name
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
// get value of id that sent from address bar
$TicketID=$_GET['TicketID'];
// Retrieve data from database
$sql="SELECT * FROM ' ".$tbl_name."' WHERE `TicketID`='".$TicketID."'";
$result = mysql_query($sql);
echo "<table width='100' border='1' cellpadding='0' cellspacing='0' id='records'>";
print "<h3 align='center'><strong>Billing History</strong></h3><p>";
echo "<tr>
<th width='110' align='center'>Billing Date</th>
<th width='80' align='center'>Ticket #</th>
<th align='center'>Project Title </th>
<th width='80' align='center'>Total GBP</th></tr>";
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($result)) {
// Print out the contents of each row into a table
echo "<tr height='22' width='100' bordercolor='343434' align='center'><td>";
echo $row['date'];
echo "</td><td> ";
echo $row['TicketID'];
echo "</td><td> ";
echo $row['project'];
echo "</td><td> ";
echo $row['grandTotal'];
}
echo "</table>";
include(XOOPS_ROOT_PATH."/footer.php");
?>
第二個腳本:
<?php
include("mainfile.php");
include(XOOPS_ROOT_PATH."/header.php");
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="db name"; // Database name
$tbl_name="tbl name"; // Table name
// Connect to server and select database.
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
$sql="SELECT * FROM `".$tbl_name."` WHERE `uid`='".$xoopsUser->uid("s"). "' AND `Paid`='Y'";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>
<tr>
<td align="center"><strong>Billing Date</strong></td>
<td align="center"><strong>Invoice Number</strong></td>
<td align="center"><strong>Description</strong></td>
<td align="center"><strong>Total GBP</strong></td>
<td align="center"><strong>View</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['date']; ?></td>
<td><? echo $rows['TicketID']; ?></td>
<td><? echo $rows['project']; ?></td>
<td><? echo $rows['grandTotal']; ?></td>
<td align="center"><a href="http://website.co.uk/site/viewInvoice.php?TicketID=<? echo $rows['TicketID']; ?>">View</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
include(XOOPS_ROOT_PATH."/footer.php");
?>
對不起 愚蠢的我。 試試看..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.