2012-01-15 125 views
0

我想知道是否有人可以幫助我。PHP保存記錄錯誤消息

我想把管理表格放在一起,可以修改成員記錄。

我放在一起的代碼如下,我可以成功從mySQL數據庫檢索記錄,如果沒有提供的電子郵件地址的記錄,就會收到相應的消息。

<?php 
mysql_connect ("host","user","password") or die (mysql_error()); 
mysql_select_db ("database"); 

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    if (isset($_POST['search'])) { 
     $searchemailaddress = $_POST['searchemailaddress']; 
     $sql = mysql_query("select * from userdetails where emailaddress like '$searchemailaddress'"); 
    if (mysql_num_rows($sql) == 0) 
     $msg = 'There are no member records set up with this email address, please try again!'; 
    } 
    while ($row = mysql_fetch_array($sql)){ 
     $emailaddress = $row['emailaddress']; 
     $forename = $row['forename']; 
     $surname = $row['surname']; 
     $subscriptionexpiration = $row['subscriptionexpiration']; 
    } 
    } 
    if (isset($_POST['update'])) { 
     $emailaddress = $_POST['emailaddress']; 
     $forename = $_POST['forename']; 
     $surname = $_POST['surname']; 
     $subscriptionexpiration = $_POST['subscriptionexpiration']; 
     $sql = mysql_query("UPDATE `userdetails` SET `emailaddress` = '$emailaddress', `forename` = '$forename',`surname` = '$surname',`subscriptionexpiration` = '$subscriptionexpiration' LIMIT 1"); 
     $msg = 'This members records have been successfully updated!'; 
    } 
?> 
<html> 
<head> 
<title>Search the Database</title> 
<style type="text/css"> 
<!-- 
.style1 {font-family: Calibri 
} 
.style9 { font-family: Calibri; 
    font-size: 24px; 
    background-color: #78AFC5; 
} 
.style7 { 
    font-family: Calibri; 
    font-size: 16px; 
    background-color: #FFFFFF; 
} 
--> 
</style> 
</head> 
<body> 

    </p> 
    <p class="style1">&nbsp;</p> 
<div align="center"><span class="style9">Search &amp; Amend User Records </span></div> 
<p class="style7"><span class="style10"> 
    <?php 
if (isset($msg)) // this is special section for 
// outputing message 
{ 
?> 
</span> 
<p class="style7"> 
    <?=$msg?> 
    <p class="style7"><span class="style10"> 
    <?php 
} 
?> 
    </span> 
    <form action="searchandamend.php" method="post"> 
    <table width="393" border="1"> 
    <tr> 
     <td width="161"><span class="style1">Search:</span></td> 
     <td width="207"><span class="style1"> 
     <input name="searchemailaddress" type="text" id="searchemailaddress" size="25"/> 
     </span></td> 
    </tr> 
    <tr> 
     <td><span class="style1">Email Address:</span></td> 
     <td><span class="style1"> 
     <input name="emailaddress" type="text"value="<?php echo $emailaddress;?>" size="25"/> 
     </span></td> 
    </tr> 
    <tr> 
     <td><span class="style1">First Name:</span></td> 
     <td><span class="style1"> 
     <input name="forename" type="text" value="<?php echo $forename;?>" size="20"/> 
     </span></td> 
    </tr> 
    <tr> 
     <td><span class="style1">Surname:</span></td> 
     <td><input name="surname" type="text" value="<?php echo $surname;?>" size="20"/></td> 
    </tr> 
    <tr> 
     <td><span class="style1">Subscription Expiry Date:</span> </td> 
     <td><input name="subscriptionexpiration" type="text" id="subscriptionexpiration" value="<?php echo $subscriptionexpiration;?>" size="10"/></td> 
    </tr> 
    </table> 
    <p><br/> 
    <input type="submit" name="search" value="Search Records"> 
    <input name="update" type="submit" value="Update Record"> 
</p> 
</form> 
</body> 
</html> 

但是,當我嘗試更改記錄的任何部分時遇到問題。更改已保存,並且我收到了正確的「記錄更新」消息,但我也收到此錯誤: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /homepages/2/d333603417/htdocs/development/searchandamend.php on line 14第14行是我的代碼中的此行:while ($row = mysql_fetch_array($sql)){我一直在研究這一段時間,現在我只能似乎無法解決問題所在。

我只是想知道是否有人可以看看這個請讓我知道我哪裏出了問題。

非常感謝

+1

好像'[ '搜索']'是,當你執行更新設置$ _ POST均勻。 – Cyclonecode 2012-01-15 15:14:26

+0

'mysql_real_escape_string()'包含在SQL查詢字符串中的變量。你在這裏有一個巨大的安全漏洞! – 2012-01-15 16:47:40

回答

0

所有,抽空誠摯的感謝回覆我的帖子。

看了這多一點,並考慮到你所說的,我想出了以下的工作正常。

if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
    if (isset($_POST["search"])) 
    { 
$searchemailaddress = $_POST['searchemailaddress']; 
$result = mysql_query("select * from userdetails where emailaddress like '$searchemailaddress'"); 
if (mysql_num_rows($result) == 0) 
$msg = 'There are no member records with the email address you have provided, please try again!'; 
else 
while ($row = mysql_fetch_array($result)){ 
$emailaddress = $row['emailaddress']; 
$forename = $row['forename']; 
$surname = $row['surname']; 
$subscriptionexpiration = $row['subscriptionexpiration']; 
$msg = 'Record retrieved!'; 
} 
} 

if (isset($_POST["update"])) 
    { 
$emailaddress = $_POST['emailaddress']; 
$forename = $_POST['forename']; 
$surname = $_POST['surname']; 
$result = mysql_query("UPDATE `userdetails` SET `emailaddress` = '$emailaddress', `forename` = '$forename',`surname` = '$surname', `subscriptionexpiration` = '$subscriptionexpiration' LIMIT 1"); 
$msg = 'This members record has been successfully updated!'; 
} 
} 

親切的問候

+0

@ IRHM-嘗試縮進你的代碼,這將使它更容易閱讀。如果這是正確的解決方案,你應該接受你自己的答案。 – Cyclonecode 2012-01-16 08:04:31

0

是否有可能連接沒有被成功創建,你正試圖從返回的布爾提取和陣列?

1
if (mysql_num_rows($sql) == 0) 

您未打開支架!

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    if (isset($_POST['search'])) { 
     $searchemailaddress = $_POST['searchemailaddress']; 
     $sql = mysql_query("select * from userdetails where emailaddress like '$searchemailaddress'"); 
     if (mysql_num_rows($sql) == 0) 
      $msg = 'There are no member records set up with this email address, please try  again!'; 
     } 
    while ($row = mysql_fetch_array($sql)){ 
     $emailaddress = $row['emailaddress']; 
     $forename = $row['forename']; 
     $surname = $row['surname']; 
     $subscriptionexpiration = $row['subscriptionexpiration']; 
    } 
} 

} 這使得isset($_POST['search'])如果塊到while前關閉。

你可能想是這樣的:

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    if (isset($_POST['search'])) { 
     $searchemailaddress = $_POST['searchemailaddress']; 
     $sql = mysql_query("select * from userdetails where emailaddress like '$searchemailaddress'"); 
     if (mysql_num_rows($sql) == 0) { 
      $msg = 'There are no member records set up with this email address, please try  again!'; 
     } 
     while ($row = mysql_fetch_array($sql)) { 
      $emailaddress = $row['emailaddress']; 
      $forename = $row['forename']; 
      $surname = $row['surname']; 
      $subscriptionexpiration = $row['subscriptionexpiration']; 
     } 
    } 
}