PDA

View Full Version : My code


Predator
04-01-2007, 06:36 PM
you asked that you wanted the page to return to normal after updating. Heres how i got it to go back to the old page after it update.
I used a meta refresh.

<?php
require_once("Connections/connection.php"); // database connection

////////////////////////////////////////////////
$data2 = $_POST['data2'];
$data3 = $_POST['data3'];
$data4 = $_POST['data4'];
$submit = $_POST['submit'];
$del = $_GET['del'];
$upd = $_GET['upd'];
$update_id = $_POST['update_id'];
////////////////////////////////////////////////

///////////////////////////////////////////////
$query = sprintf("SELECT * FROM table1 where field1_id='$upd'");
$result = @mysql_query($query);
$rowUpdate = mysql_fetch_array($result); // contains all the database data
///////////////////////////////////////////////

if ($submit && data2 && data3 && data4 && ! $update_id){

/////////////////////////////////////////////
$query = sprintf("INSERT into table1 (field2, field3, field4) values ('$data2', '$data3', '$data4')"); // inserts the data into the appropriate fields
mysql_query($query) or die (mysql_error()); //runs the query if all data is put in or kills it.
/////////////////////////////////////////////

}elseif(submit && $update_id){

/////////////////////////////////////////////
$query = sprintf("UPDATE table1 set field2='$data2', field3='$data3', field4='$data4' where field1_id = '$update_id'"); // inserts the data into the appropriate fields
$result = @mysql_query($query); //runs the query.
/////////////////////////////////////////////
echo ("<meta http-equiv=\"Refresh\" content=\"1; URL=http://localhost/Test/tnt.php\"/>");
}

// delete record
if ($del) {
/////////////////////////////////////////////
$query = sprintf("DELETE FROM table1 where field1_id='$del'"); // inserts the data into the appropriate fields
mysql_query($query) or die (mysql_error()); //runs the query if all data is put in or kills it.
/////////////////////////////////////////////
}


///////////////////////////////////////////////
$query = sprintf("SELECT * FROM table1");
$result = @mysql_query($query);
$row = mysql_fetch_array($result); // contains all the database data
///////////////////////////////////////////////

/*do{
echo $row['field3']."<br>"; // target certain data

}while ($row = mysql_fetch_array($result))*/

?>

<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table width="200" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>data2</td>
<td>data3</td>
<td>data4</td>
</tr>
<tr>
<td><input name="data2" type="text" id="data2" value="<?php echo $rowUpdate['field2']; ?>" /></td>
<td><input name="data3" type="text" id="data3" value="<?php echo $rowUpdate['field3']; ?>" /></td>
<td><input name="data4" type="text" id="data4" value="<?php echo $rowUpdate['field4']; ?>" /></td>
</tr>
<tr>
<td colspan="3"><div align="center">
<input type="reset" name="Submit4" value="Reset" />
<input type="submit" name="Submit" value="submit" />
<input name="submit" type="hidden" id="submit" value="1" />
<input name="update_id" type="hidden" id="update_id" value="<?php echo $rowUpdate['field1_id']; ?>" />
</div></td>
</tr>
</table>
</form>
<p>&nbsp;</p>
<table width="396" height="68" border="1" cellpadding="0" cellspacing="0">
<tr>
<td width="42">&nbsp;</td>
<td width="42">ID</td>
<td width="82">data2</td>
<td width="82">data3</td>
<td width="82">data4</td>
<td width="52">Delete</td>
</tr>
<?php do{ ?>
<tr>
<td><input type="submit" name="Submit3" value="Update" onClick="document.location.href='tnt.php?upd=<?php echo $row['field1_id'] ?>'"/></td>
<td><?php echo $row['field1_id']; ?></td>
<td><?php echo $row['field2']; ?></td>
<td><?php echo $row['field3']; ?></td>
<td><?php echo $row['field4']; ?></td>
<td>
<input type="submit" name="Submit2" value="Delete" onClick="document.location.href='tnt.php?del=<?php echo $row['field1_id'] ?>'"/> </td>
</tr>
<?php }while ($row = mysql_fetch_array($result)); ?>
</table>
</body>
</html>


Im not sure if this has been submitted yet but just to show what i done.

davidj
04-01-2007, 06:38 PM
why do you want to do this

davidj
04-01-2007, 06:42 PM
are you after redirecting to a new page when the update completes

Predator
04-02-2007, 09:28 AM
yes when the update completes i am making it redirect to the original page.

davidj
04-02-2007, 10:46 AM
i applaude you for coming up with an ingenious method

a big high five from me for that...

however there is a function included with php that will do a better job

instead of this..

echo ("<meta http-equiv=\"Refresh\" content=\"1; URL=http://localhost/Test/tnt.php\"/>");


do this...


header("redirect:tnt.php");
exit;


nice way yes? Much cleaner.

PS: your the kind of student i like to teach.

Predator
04-02-2007, 10:59 AM
Thank you, just my way of saying thank you for the things you have taught me.

Yes i think you way is much easier and much cleaner, i haven't heard of redirect function in php, something else i have learned from you. :)

lol where my gold medal ? jkin. :D