从mysql数据库中丢失数据

时间:2014-09-02 14:57:44

标签: php mysql

我有一个php脚本用来显示数据的mysql数据库...我遇到的问题是,在看似随机的情况下它会错过结果而我看不到确定原因的模式它可能会这样做......

检查数据库时,所有数据都显示正常。

这是我的初始搜索页面

<?php
include 'connect.php';

//set variable
$option = '';

// Get the county names from database - no duplicates - Order A-Z
$query = "SELECT DISTINCT tradingCounty FROM offers ORDER BY tradingCounty ASC";

// execute the query, $result will hold all of the Counties in an array
$result = mysqli_query($con,$query);


 while($row = mysqli_fetch_array($result)) {
    $option .="<option>" . $row['tradingCounty'] . "</option>";
}




echo "<html xmlns='http://www.w3.org/1999/xhtml'>";
echo "<title>HSB - Latest Offers</title>";
echo "<style type='text/css'>;
body {
    background-color: #FFF;
}
#wrapper {
    background-color: #FFF;
    height: auto;
    width: 1000px;
    margin-right: auto;
    margin-left: auto;
    font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif;
}
</style>
</head>

<body>
<div id='wrapper'>
  <p><img src='images/header.jpg' width='400' height='100' alt='header' /></p>
  <HR/>
  Select an area from the menu below to view any offers in that area.
  <form id='filter' name='filter' method='post' action='resultssimple.php'>
        <p><label>County</label></p>
        <select name='result' id='result'>' . $option . '</select>
        <input name='' type='submit' />
        </form>
</div>
</body>
</html>";



?>

这是我的结果页面

<?
include 'connect.php';

//Get the details from previous page
$SelectedCounty = $_POST["result"];

// Select offers linked to selected county from form
$result = mysqli_query($con,"SELECT * FROM offers WHERE tradingCounty ='" . $SelectedCounty . "'ORDER BY categoryIdName ASC;");

// PREVIOUS ATTEMPTS - ALL WRONG - GGGGRRRRRRRRRRRR !!!!!!!!
//------------------------------------------------------------
//$result = mysqli_query($con,"SELECT * FROM offers WHERE tradingCounty LIKE" . $SelectedCounty);
//$result = mysql_query("SELECT * FROM pdetails WHERE uid='" . $inputname . "';");
//"SELECT * FROM `offers` WHERE `tradingCounty` LIKE
//$result = mysqli_query($con,"SELECT * FROM offers;");
//$result = mysql_query("SELECT * FROM pdetails WHERE uid='" . $inputname . "';");
//$result = mysqli_query("SELECT * FROM offers WHERE tradingCounty=" . $SelectedCounty);


//check to see if results is set - error if not.
if(!$result)
{
    die("<p>Error in listing tables: ". mysql_error()."</p>");
}

//Show all records for selected county
echo ("<p><h2>Showing Latest Offers In : " . $SelectedCounty . "</h2></p>");
echo ("<p><a href='offers.php' target='_self'>back to search menu</a></p>");

/*
echo ("<table border='1'>");
echo ("<tr>");
echo ("<td>ID</td><td>Category</td><td>Business Name</td><td>Business Address</td><td>Address2</td><td>Address3</td><td>Town</td><td>County</td><td>Post Code</td><td>Telephone</td><td>URL</td><td>Email</td><td>Discount / Special Offer</td><td>valid from</td>");
*/

while($row = mysqli_fetch_row($result))
{
    echo ("<div style=' background-color: #EFF5FF; color: #06C; padding: 5px; float: left; border: 1px dotted #06C; margin: 10px; width: 300px; height: 300px; text-align: center; >");
    // echo ("" . $row[0] . "");
    // echo ("</br>");
    echo ("<strong>" . $row[1] . "</strong>");  
    echo ("<hr/>");
    // echo ("</br>");
    echo ("" . $row[2] . "");
    echo ("</br>");
    echo ("" . $row[3] . "");
    echo ("</br>");
    // echo ("" . $row[4] . "");
    // echo ("</br>");
    // echo ("" . $row[5] . "");
    // echo ("</br>");
    echo ("" . $row[6] . "");
    echo ("</br>");
    echo ("" . $row[7] . "");
    echo ("</br>");
    echo ("" . $row[8] . "");
    echo ("</br>");
    echo ("" . $row[9] . "");
    echo ("</br>");
    // echo ("" . $row[10] . "");
    // echo ("</br>");
    echo ("" . $row[11] . "");
    echo ("</br>");
    echo ("<hr/>");
    echo ("<strong>" . $row[12] . "</strong>");
    echo ("</br>");
    echo ("</div>");

/*  echo("<tr>");
    echo("<td>" . $row[0] . "</td>" . "<td>" . $row[1] . "</td>" . "<td>" . $row[2] . "</td>" . "<td>" . $row[3] . "</td>" . "<td>" . $row[4] . "</td>" . "<td>" . $row[5] . "</td>" . "<td>" . $row[6] . "</td>" . "<td>" . $row[7] . "</td>" . "<td>" . $row[8] . "</td>" . "<td>" . $row[9] . "</td>" . "<td>" . $row[10] . "</td>" . "<td>" . $row[11] . "</td>" . "<td>" . $row[12] . "</td>" . "<td>" . $row[13] . "</td>");
    echo("</tr>");
*/  
}
// echo("</table>");
?>
我可以看到here

2 个答案:

答案 0 :(得分:0)

他们是否遗失,或者他们被未转义的HTML字符遮挡。检查浏览器的“查看源”选项,看看它们是否确实存在。我会特别注意数据中的字符,例如浏览器可能误认为HTML开放字符的Less Than字符。

您可能需要转义输出,以便浏览器不会尝试渲染它:

echo ("" . htmlspecialchars($row[2]) . "");

另外,我建议你永远不要直接从用户那里获取输入并将其放入SQL查询中而不首先转义它。您正在为SQL注入攻击做好准备。

请参阅以下内容:

http://php.net/manual/en/mysqli.real-escape-string.php

答案 1 :(得分:0)

不知道它是否有帮助,但在这一行:

$result = mysqli_query($con,"SELECT * FROM offers WHERE tradingCounty ='" . $SelectedCounty . "'ORDER BY categoryIdName ASC;");

看起来你在最后一个双引号之前有一个额外的分号(;)。我不认为应该在那里。

您还可以存储在数组中返回的所有内容,并在迭代它时回显所有内容以查看它返回的内容。然后,如果缺少某些东西,请转到您的数据库并查看该行。

$tempArray = array();
while($row = mysqli_fetch_row($result)) {
    $tempArray = $row;
}

foreach($tempArray as $value) {
    echo $value . '<br>';
}