Click to See Complete Forum and Search --> : Editing DB Content


scottyrob
09-08-2006, 01:35 AM
Hi there. I have this code



<?php require("index_top.php"); require("index_left.php"); ?>

<?php echo "<div class=\"caption\">Awards Admin</div><br><br>"; ?>

Add a new award below<br><br><br><br><br><br>



<?php

require("files/db_connect.php");

$result = @mysql_query("SELECT * FROM Awards WHERE ID = ".$_GET["ID"]);

while ($row = mysql_fetch_assoc($result))
{

?>

<form name="form1" method="post" action="">
<table width="571" border="0" cellspacing="0" cellpadding="0" align="left">
<tr>
<td><div align="right">ID:</div></td>
<td><div align="center">
<input type="text" name="textfield2" value="<?php echo $row['ID'] ?>">
</div></td>
</tr>
<tr>
<td><div align="right">Image URL: </div></td>
<td><div align="center">
<input type="text" name="textfield3" value="<?php echo $row['Image'] ?>">
</div></td>
</tr>
<tr>
<td><div align="right">URL Of Image when Clicked: </div></td>
<td><div align="center">
<input type="text" name="textfield4" value="<?php echo $row['Image_URL'] ?>">
</div></td>
</tr>
<tr>
<td><div align="right">Badge Name: </div></td>
<td><div align="center">
<input type="text" name="textfield6" value="<?php echo $row['Name'] ?>">
</div></td>
</tr>
<tr>
<td><div align="right">Badge Info:</div></td>
<td><div align="center">
<input type="text" name="textfield5" value="<?php echo $row['Info'] ?>">
</div></td>
</tr>
<tr>
<td><div align="right">Badge Awarded To:</div></td>
<td><div align="center">
<input type="text" name="textfield7" value="<?php echo $row['People'] ?>">
</div></td>
</tr>
</table>
</form>
<?php } ?>
<?php require("index_right.php"); require("index_bottom.php"); ?>


How can i make it so that i can make changes to the text in the boxes, then update the DB with it? You can see what ive got so far... www.loddonexplorers.co.uk/New/awards_edit.php?ID-30

Thanks,
Scott

cridley
09-08-2006, 03:40 AM
The simplest way is to provide a submit button to the form and give the form an action, ie a file to request with the post variables, say processform.php :

<form name="form1" method="post" action="">
Then you'd have a file processform.php which would go something like :
<?php

$ID = $_POST['textfield2'];
$image = $_POST['textfield3'];
$imageURL = $_POST['textfield3'];
// and so on for each element

//then after validation, update db
$result = mysql_query("UPDATE Awards SET Image=$image,Image_URL=$imageURL.....) WHERE ID=$ID ")

//or however you intend to update

//now redirect to original page?
header("location:originalpage.php");
// where originalpage.php is the page you want displayed
?>

A nicer way would be the http request method (javascript) as you wouldn't have to refresh the page and maybe that way you can ditch the submit button and have it update on the onchange events of the fields. For how to do that (http request) type in 'w3c ajax tutorial' in google.

scottyrob
09-08-2006, 10:13 AM
This update script dosent work (when submitted from another page)


<?php

require("files/db_connect.php");


$ID = $_POST['ID'];
$Image = $_POST['Image'];
$image_URL = $_POST['Image_URL'];
$Name = $_POST['Name'];
$Info = $_POST['Info'];
$People = $_POST['People'];


//then after validation, update db
$update = mysql_query("UPDATE Awards SET ID=$ID,Image=$Image,Image_URL=$image_URL,Name=$Name,Info=$info,People=$People WHERE ID=$ID ");

mysql_query($update);
echo "Record Updated";
mysql_close();

cridley
09-08-2006, 10:37 AM
For a start, you need to enclose string in single quotes, and you're setting ID to itself, which is unnecessary:

$update = mysql_query("UPDATE Awards SET ID=$ID,Image=$Image,Image_URL=$image_URL,Name=$Name,Info=$info,People=$People WHERE ID=$ID "); Becomes :
$update = mysql_query("UPDATE Awards SET Image='$Image',Image_URL='$image_URL',Name='$Name',Info='$info',People='$People' WHERE ID=$ID ");

Not sure which fields are string so I done 'em all (except ID which i assume to be an integer) .

cridley
09-08-2006, 10:38 AM
I do see that I didn't put the quotes in the original reply, so my bad, I guess...