PHP Search and edit functions

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

PHP Search and edit functions

Post 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";
}

?>
Image

Image
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: PHP Search and edit functions

Post 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";
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: PHP Search and edit functions

Post by SpiritWebb »

I placed the LIMIT 20 in the code, and I am still getting the same thing. I am so confused now.
Image

Image
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: PHP Search and edit functions

Post by Chris »

What's the error?
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: PHP Search and edit functions

Post 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...
Image

Image
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: PHP Search and edit functions

Post 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.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: PHP Search and edit functions

Post by SpiritWebb »

Actually have those taken out, and it still is not working properly.
Image

Image
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: PHP Search and edit functions

Post 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
}
?>

Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: PHP Search and edit functions

Post by SpiritWebb »

Nope, as you can see above I am using <?php ?>
Image

Image
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: PHP Search and edit functions

Post 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>
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “Advanced Help and Support”