I wonder whether someone could help me please.
I’m trying to put together an administration form where members records can be amended.
The code that I’ve put together is below and I can successfully retrieve the records from a mySQL database, receiving the appropriate messsage if there are no records with the email address provided.
<?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"> </p>
<div align="center"><span class="style9">Search & 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>
However, I’m having problems when I try to change any part of the record. The changes are saved and I receive the correct ‘Record updated’ message but I also receive this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /homepages/2/d333603417/htdocs/development/searchandamend.php on line 14 with line 14 being this line in my code: while ($row = mysql_fetch_array($sql)){ I’ve been working on this for a while now, and I just can’t seem to work out what the problem is.
I just wondered whether someone could perhaps take a look at this please and let me know where I’ve gone wrong.
Many thanks
All, sincere thanks for taking the time to reply to my post.
After looking at this a little more, and taking into account what you all said, I came up with the following which works fine.
Kind regards