Page 1 of 2

PHP Search and edit functions

Posted: Tue Apr 06, 2010 2:33 am
by SpiritWebb
I have created a search field that displays data in MySQL and you can update the information and resend the data to update it. However when I try running it, I keep getting APACHE has encountered an error and must close. Here is the code to it all...any ideas why this is happening? The page to connect to is listrecords.php (http://localhost/test/listrecords.php) and lost when it encounters an error...

listrecords.php

Code: Select all

<?php
$host="localhost"; // Host name 
$username="left blank"; // Mysql username 
$password="**********"; // Mysql password 
$db_name="metrics"; // Database 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";
$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>Update your enteries: </strong> </td>
</tr>

<tr>
<td align="center"><strong>Date</strong></td>
<td align="center"><strong>User ID</strong></td>
<td align="center"><strong>Shift</strong></td>
<td align="center"><strong>Rotation Name</strong></td>
<td align="center"><strong>Rotation Complete</strong></td>
<td align="center"><strong>Total Tapes Required</strong></td>
<td align="center"><strong>Total Tapes Entered</strong></td>
<td align="center"><strong>Total Tapes Removed</strong</td>
<td align="center"><strong>Comments</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['date']; ?></td>
<td><? echo $rows['userid']; ?></td>
<td><? echo $rows['shift']; ?></td>
<td><? echo $rows['rotname']; ?></td>
<td><? echo $rows['rotationcomplete']; ?></td>
<td><? echo $rows['totaltapesreq']; ?></td>
<td><? echo $rows['totalentered']; ?></td>
<td><? echo $rows['totalremoved']; ?></td>
<td><? echo $rows['comments']; ?></td>

// link to update.php and send value of id 
<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>
Update.php

Code: Select all

<?php
$host="localhost"; // Host name 
$username="left blank"; // Mysql username 
$password="*********"; // Mysql password 
$db_name="metrics"; // Database 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");

// get value of id that sent from address bar
$id=$_GET['id'];


// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Date</strong></td>
<td align="center"><strong>User ID</strong></td>
<td align="center"><strong>Shift</strong></td>
<td align="center"><strong>Rotation Name</strong></td>
<td align="center"><strong>Rotation Complete</strong></td>
<td align="center"><strong>Total Tapes Required</strong></td>
<td align="center"><strong>Total Tapes Entered</strong></td>
<td align="center"><strong>Total Tapes Removed</strong</td>
<td align="center"><strong>Comments</strong></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="date" type="text" id="date" value="<? echo $rows['date']; ?>"></td>
<td align="center"><input name="userid" type="text" id="userid" value="<? echo $rows['userid']; ?>" size="15"></td>
<td><input name="shift" type="text" id="shift" value="<? echo $rows['shift']; ?>" size="15"></td>
<td><input name="rotname" type="text" id="date" vale"<? echo $rows['rotname']; ?>" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?

// close connection 
mysql_close();

?>
update_ac.php

Code: Select all

<?php
$host="localhost"; // Host name 
$username="left blank"; // Mysql username 
$password="*********"; // Mysql password 
$db_name="metrics"; // Database 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");

// update data in mysql database 
$sql="UPDATE $tbl_name SET date='$date', userid='$userid', shift='$shift', rotname='$rotname', rotationcomplete='$rotationcomplete', totaltapesreq='$totaltapesreq', totalentered='$totalentered', totalremoved='$totalremoved', comments='$comments' WHERE id='$id'";
$result=mysql_query($sql);

// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='listrecords.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

Re: PHP Search and edit functions

Posted: Tue Apr 06, 2010 12:47 pm
by Chris
I can't see much wrong with your code, besides the fact that you define too many variables for pointless stuff.

I'm guessing there is something wrong with your while loop in listrecords.php, It might be looping through too much in one go. Try limit your SQL query and see what happens.

$sql="SELECT * FROM $tbl_name LIMIT 20";

Re: PHP Search and edit functions

Posted: Tue Apr 06, 2010 9:58 pm
by SpiritWebb
I placed the LIMIT 20 in the code, and I am still getting the same thing. I am so confused now.

Re: PHP Search and edit functions

Posted: Wed Apr 07, 2010 7:54 am
by Chris
What's the error?

Re: PHP Search and edit functions

Posted: Wed Apr 07, 2010 9:01 am
by SpiritWebb
Its not an error given, thats the problem. Its just pops up saying Apache HTTP Server has encountered an error and must close. It only happens when I try to run the listrecords.php script.

I removed the mysql_close() at the bottom, and now I am not getting, however its not populating my table. It builds the table, gives the correct amount of rows in the DB, just not giving the data from MySQL and populating the table now...gah...

Re: PHP Search and edit functions

Posted: Wed Apr 07, 2010 11:39 am
by Chris
I noticed you are placing a comment right in the middle of your HTML:

Code: Select all

// link to update.php and send value of id
Maybe that's causing the table not to show up properly.

Re: PHP Search and edit functions

Posted: Thu Apr 08, 2010 4:56 am
by SpiritWebb
Actually have those taken out, and it still is not working properly.

Re: PHP Search and edit functions

Posted: Thu Apr 08, 2010 9:14 am
by Chris
Do you have short tags enabled? Rather than using "<?" use "<?php"

Code: Select all

<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['date']; ?></td>
<td><?php echo $rows['userid']; ?></td>
<td><?php echo $rows['shift']; ?></td>
<td><?php echo $rows['rotname']; ?></td>
<td><?php echo $rows['rotationcomplete']; ?></td>
<td><?php echo $rows['totaltapesreq']; ?></td>
<td><?php echo $rows['totalentered']; ?></td>
<td><?php echo $rows['totalremoved']; ?></td>
<td><?php echo $rows['comments']; ?></td>
<td align="center"><a href="update.php?id=<?php echo $rows['id']; ?>">update</a></td>
</tr>
<?php
}
?>


Re: PHP Search and edit functions

Posted: Thu Apr 08, 2010 5:31 pm
by SpiritWebb
Nope, as you can see above I am using <?php ?>

Re: PHP Search and edit functions

Posted: Fri Apr 09, 2010 7:53 am
by Chris
SpiritWebb wrote:

Code: Select all

<td><? echo $rows['date']; ?></td>
<td><? echo $rows['userid']; ?></td>
<td><? echo $rows['shift']; ?></td>
<td><? echo $rows['rotname']; ?></td>
<td><? echo $rows['rotationcomplete']; ?></td>
<td><? echo $rows['totaltapesreq']; ?></td>
<td><? echo $rows['totalentered']; ?></td>
<td><? echo $rows['totalremoved']; ?></td>
<td><? echo $rows['comments']; ?></td>