Page 2 of 2

Re: PHP Search and edit functions

Posted: Fri Apr 09, 2010 5:24 pm
by SpiritWebb
oh was looking at it as a whole. Not those tiny parts. Will look at it shortly

Re: PHP Search and edit functions

Posted: Sat Apr 10, 2010 12:00 am
by SpiritWebb
UPDATE: It worked. Thanks Chris. I would have never saw that with the <? and needed the php! Your a life-saver...

Re: PHP Search and edit functions

Posted: Sat Apr 10, 2010 1:52 am
by SpiritWebb
Alright, so everything is working...however when you click update, change the value and click submit, it says thanks! When you go back to look, it never updated...still shows original data...gah...so frustrating. Here are the two pages its using:

update.php

Code: Select all

<?php


// Connect to server and select database.
mysql_connect("*********************", "****", "*****") or die(mysql_error()); 
mysql_select_db("metrics") or die(mysql_error());

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


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

$rows = mysql_fetch_array($result2);
?>
<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="9"><strong>You are updating <?php echo $rows['rotname']; ?>:</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="<?php echo $rows['date']; ?>"></td>
<td align="center"><input name="userid" type="text" id="userid" value="<?php echo $rows['userid']; ?>" size="15"></td>
<td align="center"><input name="shift" type="text" id="shift" value="<?php echo $rows['shift']; ?>" size="15"></td>
<td align="center"><input name="rotname" type="text" id="rotname" value="<?php echo $rows['rotname']; ?>" size="15"></td>
<td align="center"><input name="rotationcomplete" type="text" id="rotationcomplete" value="<?php echo $rows['rotationcomplete']; ?>" size="15"></td>
<td align="center"><input name="totaltapesreq" type="text" id="totaltapesreq" value="<?php echo $rows['totaltapesreq']; ?>" size="15"></td>
<td align="center"><input name="totalentered" type="text" id="totalentered" value="<?php echo $rows['totalentered']; ?>" size="15"></td>
<td align="center"><input name="totalremoved" type="text" id="totalremoved" value="<?php echo $rows['totalremoved']; ?>" size="15"></td>
<td align="center"><input name="comments" type="text" id="comments" value="<?php echo $rows['comments']; ?>" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
update_ac.php

Code: Select all

<?php


// Connect to server and select database.
mysql_connect("*********************", "****", "*****") or die(mysql_error()); 
mysql_select_db("metrics") or die(mysql_error());

// update data in mysql database 
$sql="UPDATE tad 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 "Metrics updated successfully";
echo "<BR>";
echo "<a href='listrecords.php'>Go back</a>";
}

else {
echo "ERROR";
}

?>
If I try and put $ next the variables up top, it says undefined. If I leave the $ off, it says it updates successfully, however the data never changed.

Re: PHP Search and edit functions

Posted: Sat Apr 10, 2010 6:30 pm
by Chris
You are going to need to use the $ otherwise it won't now what variable you are trying to put in there..

I'm not really one for spoon feeding, please read over this.. I'm not an expert on MySQL, and because I'm not I always use `'s when I'm asking for information in my SQL, because there are a lot of reserved words in SQL, this will prevent things from going wrong, here's an example:

Code: Select all

mysql_query("UPDATE `table` SET update = '". time() ."'"); // This will not update your field if it's called update

mysql_query("UPDATE `table` SET `update` = '". time() ."'"); // This should work fine (as far as I know)
 
Anyway here's your code. If your SQL doesn't work, your script will die and return the error MySQL is having.

Code: Select all

// Make sure the information is good to go, this will disable people from tampering with your database
foreach( $_POST as $key => $value )
{
    $_POST[$key] = addslashes(htmlentities($value));
}

// update data in mysql database 
mysql_query("UPDATE `tad` SET
            `date` = '{$_POST['date']}',
            `userid` = '{$_POST['userid']}',
            `shift` = '{$_POST['shift']}',
            `rotname` = '{$_POST['rotname']}',
            `rotationcomplete` = '{$_POST['rotationcomplete']}',
            `totaltapesreq` = '{$_POST['totaltapesreq']}',
            `totalentered` = '{$_POST['totalentered']}',
            `totalremoved` = '{$_POST['totalremoved']}',
            `comments` = '{$_POST['comments']}'
            WHERE `id` = '". addslashes(htmlentities($_GET['id'])) ."'") or die( mysql_error() );

echo "Metrics update succesfully<br /> <a href=\"listrecords.php\">Go Back</a>";