...

View Full Version : updating (changing) mysql db with html table



zeroval
08-20-2007, 05:35 PM
I wish to use one html-page to view and change entries in a db. Here is the code I got so far, but when clicking "save", it will just display the original data without updating.



<?php
include('Connections/db.php');
$id = '1';
mysql_select_db($db);
// Update the database if form posted
if ($save) {
echo "updated <p>" ;
$update = mysql_query("UPDATE distributoren Set Firma = $txtFirma, Land = $txtLand WHERE id = $id") or die("SQL Update failed");
}
$query_distributoren = "SELECT * FROM distributoren WHERE id = $id";

$distributoren = mysql_query($query_distributoren) or die(mysql_error());
$row_distributoren = mysql_fetch_assoc($distributoren);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Details</title>
</head>

<body>
<form method="post" action="<?php echo $PHP_SELF ?>">
<input type="hidden" name="txtId" value="<? echo $id; ?>">

Firma
<input type="text" name="txtFirma"value="<?php echo $row_distributoren['Firma']; ?>"/>
<br />
Land
<input type="text" name="txtLand" value="<?php echo $row_distributoren['Land']; ?>" />
<br />

<input type="submit" name="save" value="Save">
</form>

</body>
</html>
<?php
mysql_free_result($distributoren);
?>


What's wrong? Thanks.

ess
08-20-2007, 05:44 PM
You can redirect the page to itself once you have updated the database.

for example

<?php
if(isset($_POST['save'])){
// start saving data into data base
// and if successful, redirect page to itself to refresh page.
header("location: " . $_SERVER['PHP_SELF']);
exit();
}
?>

Inigoesdr
08-20-2007, 10:58 PM
echo "updated <p>" ; // after this line add:
$txtFirma = $_POST['txtFirma'];
$txtLand = $_POST['txtLand'];
You may want to echo() the query before you execute it too to make sure that it's properly formatted and possibly run it from phpMyAdmin.

You can redirect the page to itself once you have updated the database.

There's no reason to do that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum