Forum Moderators: phranque

Message Too Old, No Replies

Delete table row without deleting data from db?

         

criTiKAL

4:33 pm on Sep 13, 2008 (gmt 0)

10+ Year Member



How do I go about doing this? I have a table displaying information from the database. I figured out how to delete it from the database, but I also want a button where it stays in the database and just deletes the table on the site. Could anybody help me here?

Thanks in advance.
-Brian

criTiKAL

4:36 pm on Sep 13, 2008 (gmt 0)

10+ Year Member



I am also trying to build an edit code. What I am building is similar to a forum, but its not the exact same. But I want to create an "edit post" page, so I can update information, and so on. Could anyone point me to tutorials or something?

LifeinAsia

3:44 pm on Sep 16, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



As far as not showing, but not deleting from the DB, just add an extra field called Display or something like that. If Display is set to ON or 1 (or however you want to set it up), then display the data. Otherwise, do not display.

No idea for tutorials, but it's a pretty basic concept: read the data one the edit page, then on the submit page update the data in the DB based on what the user entered.

criTiKAL

12:51 am on Sep 24, 2008 (gmt 0)

10+ Year Member



Ok, I understand the display deal, but how would I code that into my site.. Here is what I use now:

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row

?>
<tr bgcolor="#006600">
<td><div align="center" class="style1"><? echo $rows['id']; ?></div></td>
<td><div align="center" class="style1"><? echo $rows['conpo']; ?></div></td>
<td><div align="center" class="style1"><? echo $rows['datesched']; ?></div></td>
<td><div align="center" class="style1"><? echo $rows['contractor']; ?></a><BR>
</div></td>
<td align="center"><div align="center" class="style1"><? echo $rows['containersize']; ?></div></td>
<td align="center"><div align="center" class="style1"><? echo $rows['type']; ?></div></td>
<td align="center"><div align="center" class="style1"><? echo $rows['received']; ?></div></td>
<td align="center"><div align="center" class="style1"><? echo $rows['completed']; ?></div></td>
<td align="center"><form action="editpost.htm?id=<? echo $rows['id'] ?>" method="post" name="form985687231" class="style4">
<div align="left"><input type="submit" name="PostRequest" id="PostRequest" value="Edit">
</div>
</form></td>
<td align="center"><form action="newWin.htm" method="post" name="form1211111">
<strong> <a href="newWin.htm">
<input type="button" name="PostRequest3" id="PostRequest3" value="Delete"/>
</a></strong>
</form></td>
<td align="center"><form action="delete.php?id=<? echo $rows['id']; ?>" method="post" name="form123412123421">
<strong>

<input type="submit" name="delete_this" id="delete_this" value="Perm Delete" onclick="return verify()"/>
</strong>
</form></td>
</tr>

<?php
// Exit looping and close connection
}
mysql_close();
?>
</table>

^^ I don't know how to make the tables in php.. '-.- or the if function may be easier to use..

Also, for the edit. How do I get my edit page to pull the data from the database? And then on the update page, when I update it, it makes a new post rather than updating the correct post, is that because I did not pull the id from the url? If so, how do I do this?

[edited by: criTiKAL at 1:19 am (utc) on Sep. 24, 2008]

criTiKAL

12:52 am on Sep 24, 2008 (gmt 0)

10+ Year Member



On the edit page, I got all of the fields placed on there and correct, but I cannot get it to update the correct post.

The code I posted is a code of the table where it displays the info from that row, if that makes sense.. Can someone help me?

BTW, ty lifeinasia.

LifeinAsia

4:29 pm on Sep 24, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



When you update the table with the edited data, you have to use UPDATE, not INSERT.

Since that is a database issue, you're mor elikely to get a response if you post in the Database section.

criTiKAL

9:07 pm on Sep 24, 2008 (gmt 0)

10+ Year Member



k, how do I make it read what row id # it needs to update? I got it to post the id # in the url (example: address/update.php?id=19) but I dont know how to get the update page to pull the id# (19) from the url to update id# 19, and not update #1 or w/e..

LifeinAsia

9:58 pm on Sep 24, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You'll use something like "WHERE id=19" at the end of your UPDATE statement.

Here's the basic logic:
- SQL for update page -
SELECT Name
FROM YourTable
WHERE ID=19

- your update page (I don't program in PHP, so it may be a little different) -
<form action="finalpage.php" method="post">
<input type="hidden" name="ID" value="19">
<input type="text" name="Name" value="<? echo $rows['Name']; ?>">
<input type="submit">
</form>

- SQL for finalpage.php -
UPDATE YourTable SET
Name=[FORM.Name]
WHERE ID=[FORM.ID]

(Like I said, I don't know PHP, so substitute whatever is in the square brackets for how you pass form parameters to your database in PHP.)

phranque

11:31 pm on Sep 24, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], criTiKAL!

you should put the id in a hidden input parameter in the form so the script handling the edit/update can get the id from the POST request.

criTiKAL

5:11 am on Sep 25, 2008 (gmt 0)

10+ Year Member



Ok, everything is working, except it will not update the fields because the id # is duplicate.. How do I bypass that?

criTiKAL

5:13 am on Sep 25, 2008 (gmt 0)

10+ Year Member



LOL, I took the id out of the update deal, and it changed updated every row, so I definately need it to just update that 1 id # lol.

criTiKAL

5:56 am on Sep 25, 2008 (gmt 0)

10+ Year Member



OK, EVERYTHING IS WRKING except for the duplicate id's

phranque

6:30 am on Sep 25, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



what is the error you get?

LifeinAsia

5:25 pm on Sep 25, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Please post your SQL statements.

criTiKAL

12:01 am on Sep 26, 2008 (gmt 0)

10+ Year Member



This is after I hit Update Request.

I made it display the array.


This is without trying to update the id #.

-----------------------------------------------
Array
(
[conpo] => 2133125
[datesched] => 123123
[unitnum] => 12314123
[Type] =>
[containersize] => 20\' 50/50
[doors] => Doors Off
[contractor] => 123123
[orderedby] => 12312312
[jobcontact] => 123123123
[phone] => 3123123
[jobname] => 12312
[address] => 231231
[city] => 2312312
[directions] => 3123123
[comments] => 123123123
[conrentalfee] =>
[delfee] => 123123
[pickupfee] => 123123123123
[relfee] => 123123
[extra] => 123123123
[datedelivered] => 2312312
[eqdel] => 3123123
[Submit_btn] => Update Request
)

Duplicate entry '0' for key 1
-----------------------------------------------

This is what I get for trying to update the id #.

-----------------------------------------------

Array
(
[id] => 31
[conpo] => 2133125
[datesched] => 123123
[unitnum] => 12314123
[Type] =>
[containersize] => 20\' 50/50
[doors] => Doors Off
[contractor] => 123123
[orderedby] => 12312312
[jobcontact] => 123123123
[phone] => 3123123
[jobname] => 12312
[address] => 231231
[city] => 2312312
[directions] => 3123123
[comments] => 123123123
[conrentalfee] =>
[delfee] => 123123
[pickupfee] => 123123123123
[relfee] => 123123
[extra] => 123123123
[datedelivered] => 2312312
[eqdel] => 3123123
[Submit_btn] => Update Request
)

Duplicate entry '31' for key 1

LifeinAsia

12:09 am on Sep 26, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Please post your SQL *statements*.

criTiKAL

6:08 am on Sep 26, 2008 (gmt 0)

10+ Year Member



// get data that sent from form\
$id=$_GET['id'];

$conpo=$_POST['conpo'];

$datesched=$_POST['datesched'];

$unitnum=$_POST['unitnum'];

$type=$_POST['type'];

$containersize=$_POST['containersize'];

$doors=$_POST['doors'];

$cc=$_POST['cc'];

$received=$_POST['received'];

$completed=$_POST['completed'];

$other=$_POST['other'];

$contractor=$_POST['contractor'];

$orderedby=$_POST['orderedby'];

$jobcontact=$_POST['jobcontact'];

$phone=$_POST['phone'];

$jobname=$_POST['jobname'];

$address=$_POST['address'];

$city=$_POST['city'];

$directions=$_POST['directions'];

$conrentalfee=$_POST['conrentalfee'];

$delfee=$_POST['delfee'];

$pickupfee=$_POST['pickupfee'];

$relfee=$_POST['relfee'];

$extra=$_POST['extra'];

$datedelivered=$_POST['datedelivered'];

$eqdel=$_POST['eqdel'];

$comments=$_POST['comments'];

$datetime=date("d/m/y h:i:s"); //create date time

$sql="UPDATE forum_question SET id='$id', conpo='$conpo', comments='$comments', datesched='$datesched', unitnum='$unitnum', type='$type', containersize='$containersize', doors='$doors', cc='$cc', received='$received', completed='$completed', other='$other', contractor='$contractor', orderedby='$orderedby', jobcontact='$jobcontact', phone='$phone', jobname='$jobname', address='$address', city='$city', directions='$directions', conrentalfee='conrentalfee', delfee='$delfee', pickupfee='$pickupfee', relfee='$relfee', extra='$extra', datedelivered='$datedelivered', eqdel='$eqdel'";

$result=mysql_query($sql) or die(mysql_error());

if($result){

echo "Successful!<BR>";

echo "<a href=forum_main.php>Back to Container Requests</a>";

}

else {

echo "ERROR";

}

mysql_close();

phranque

10:52 am on Sep 26, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



that should be more like:
UPDATE forum_question SET conpo='$conpo', ... , eqdel='$eqdel' WHERE id='$id'

criTiKAL

2:38 pm on Sep 26, 2008 (gmt 0)

10+ Year Member



wallah! it worked. Thank you.

LifeinAsia

3:33 pm on Sep 26, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Yes, very important to use the WHERE clause with UPDATE statements. And *especially* with DELETE statements!