Forum Moderators: coopster
This is the starting point:
1 mysql table
1 php-form to insert rows (working fine)
1 php-form to delete rows (working fine)
1 php-file where to choose a row and print it on the page (working fine)
I have managed to write the function for the last mentioned file so that it prints the outcome straight into textboxes. (The way I've done it feels a bit strange, any comments on that are welcome)
Needed: good advice on how to get the altered outcome saved. For some reason I haven't found a way how to separate the parts of the function so that it firstly prints out the data in textboxes and when pressed "Submit" it saves the changes.
I've tried to google my way to the right answer, but nothing I try will give me a working answer.
What are the pieces I need to make this work? Here's a shrinked version of the function I'm using.
Thankful for any guidelines.
function edit() {
$id=$_POST[id];if ($id== "") {
$id= $_GET['id'];
$name= $_GET['name'];
$adr = $_GET['adr'];
}
$sql2="select * from table where id='$id'";
$result2=mysql_query($sql2) or die("select fails");
$no=mysql_num_rows($result2);
if ($no==0) {
echo "There is no such id as '$id'";
} else {
echo "'$id':";
}
while($whatyouget = mysql_fetch_array($result2)){
echo "Name:";
echo "<input type=text name=newname value=";
echo $whatyouget['name'];
echo ">";
echo "Adress:";
echo "<input type=text name=newadr value=";
echo $whatyouget['adr'];
echo ">";
echo "<input type=submit value=save>";
}
$sql = "update table set
name='$newname',adr='$newadr' where name='$name'";
$result = mysql_query($sql) or die("insert fails");
echo "The data has been saved";
}
#COLLECT INFO FROM PREVIOUS PAGE, IF AVAILABLE
$id=$_POST['id']; #added apostrophes $name= $_POST['name']; $adr= $_POST['adr']; if ((!$id)&&(!$name)) { #COMING FROM SELF $id= $_GET['id']; $name= $_GET['name']; $adr= $_GET['adr']; } #NO CONFIRMATION OF ACTION, YET, SO PRINT APPROPRIATE FORM
if (!$_GET['doit']) { #THIS FORM DOES ONE OF THREE ACTIONS, DEPENDING ON PREVIOUS PAGE CHOICE
echo "<form action='".$PHP_SELF."' method=get>\n"; switch $_POST['doing'] { case "add": echo "Confirm Adding This Information:<br />\n"; echo "Name: ".$name."<br />\n"; echo "Adress: ".$adr."<br />\n"; echo "<input type='hidden' name='doit' value='add'>\n"; break; case "dele": $sql="select * from table where id='$id'"; $result=mysql_query($sql) or die("Delete select failed: ".mysql_error()); $no=mysql_num_rows($result); if ($no<=0) { #also matches error -1 echo "There is no record id '".$id."'"; } else { while($whatyouget = mysql_fetch_array($result)){ echo "Confirm Deleting This Information, record id '".$id."':<br />\n"; echo "Name: ".$whatyouget['name']."<br />\n"; echo "Adress: ".$whatyouget['adr']."<br />\n"; } echo "<input type='hidden' name='id' value='".$id."'>\n"; echo "<input type='hidden' name='doit' value='dele'>\n"; break; case "edit": $sql="select * from table where id='$id'"; $result=mysql_query($sql) or die("Edit select failed: ".mysql_error()); $no=mysql_num_rows($result); if ($no<=0) { echo "There is no record id '".$id."'"; } else { while($whatyouget = mysql_fetch_array($result)){ echo "Make changes to record id '".$id."':<br />\n"; echo "Name: <input type=text name=name value=".$whatyouget['name'].">\n"; echo "Adress: <input type=text name=adr value=".$whatyouget['adr'].">\n"; } echo "<input type='hidden' name='id' value='".$id."'>\n"; echo "<input type='hidden' name='doit' value='edit'>\n"; mysql_free_result($result); break; } #SAME SUBMIT BUTTON FOR EACH ACTION
echo "<input type=submit value='Confirm'>"; #END CONFIRMATION FORM DISPLAY
} #FORM SENT 'doit' TO ITSELF ON SUBMIT
#DO APPROPRIATE QUERY DEPENDING ON VALUE OF 'doit'
switch $_GET['$doit'] { case "add": $sql = "insert into table (id,name,adr) values (NULL,'$name','$adr')"; $result = mysql_query($sql) or die("Insert failed: ".mysql_error()); echo "The data has been inserted"; break; case "dele": $sql = "delete from table where $id='$id'"; $result = mysql_query($sql) or die("Delete failed: ".mysql_error()); echo "The data has been deleted"; break; case "edit": $sql = "update table set name='$name',adr='$adr' where id='$id')"; $result = mysql_query($sql) or die("Update failed: ".mysql_error()); echo "The data has been updated"; break; } Hope that's helpful ... :)
<edit>clean up first section to match second comment</edit>
[edited by: StupidScript at 7:12 pm (utc) on April 22, 2005]
I'll often use one page for all of this. To get to that page, I'll often use a link that passes my intentions to the main db page.
Often I'll include a db query to build the links, if it's a common admin page:
<form action='dbpage.php' method=post> Name: <input name='name'> Address: <input name='adr'> <input type='hidden' name='doing' value='add'> <input type='submit' value='Add This Entry'> </form> <? $sql="select id,name from table"; $result=mysql_query($sql) or die ("No connection: ".mysql_error()); while($row=mysql_fetch_array($result)) { echo "<a href='dbpage.php?doing=edit&id=".$row['id']."'>Edit ".$row['name']." ¦ "; echo "<a href='dbpage.php?doing=dele&id=".$row['id']."'>Delete ".$row['name']."<br />\n"; } ?> (In this case, the form and links send $_POST[] data to the db page. Operations within dbpage.php will be $_GET[].)
Then, dbpage.php shows the appropriate form, depending on the value of 'doing'.
Then, on dbpage.php, additions, edits or deletions are confirmed (as in the last message) and, when submitted, the db activity takes place.
$id=$_POST['id']; $name= $_POST['name']; $adr= $_POST['adr']; if ((!$id)&&(!$name)) { $id= $_GET['id']; $name= $_GET['name']; $adr= $_GET['adr']; } is better as:
if (isset($_POST['id'])¦¦isset($_POST['name'])) { $id=$_POST['id']; $name= $_POST['name']; $adr= $_POST['adr']; } elseif (isset($_GET['id'])¦¦isset($_GET['name'])) { $id= $_GET['id']; $name= $_GET['name']; $adr= $_GET['adr']; } else { echo "<a href='page1.php'>Go back and pick an action</a>\n"; }
Using the "switch()" function is a little different than what I wrote. For example, I wrote:
switch $_POST['doing'] { and
switch $_GET['doit'] { These should be changed to:
switch ($_POST['doing']) { switch ($_GET['doit']) { with parentheses around the conditions. Sorry!