Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete from database problem!!!!!

Status
Not open for further replies.

miroiti

Programmer
Jun 20, 2009
18
EG
hi all
i was trying to delete a record from a data base based on the value of hidden filed i made.but this is not performed in a proper way....so how can i do this
here is what i get!!!

<?

$query = "SELECT title, url FROM managelinks";
$queryResult = mysql_query($query);

"<div align=\"center\"><form name=\"makechange\" id=\"f1\" method=\"get\" action=\"" ";
'<table id="t" border=3>';

while ($links = mysql_fetch_array($queryResult))
{
echo'<tr><td><input type="textbox" name="ti" value="'.$links->title.'" readonly="readonly"></td>';
echo'<td><input type="textbox" name="ur" value="'.$links->url.'" readonly="readonly"></td>';
echo'<td><input type="submit" name="Edit" value="Edit"></td>';
echo'<td><input type="submit" name="delete" value="Delete"> <INPUT TYPE="hidden" name="delete" VALUE="'.$links->title.'"></td>';
echo'</br>';
echo '</tr>';
}
echo'</table></form></div>';

if ($_GET[delete])
{


$query3 = "delete from managelinks where title='+document.f1.item("delete").value+'";//here the problem

$queryResult3 = mysql_query($query3);
if($queryResult3)
{
echo"Deleted ok";
}
}
?>
 
Hi

[ul]
[li]You are mixing PHP and JavaScript[/li]
[li]Better use [tt]autoincrement[/tt] field to identify records[/li]
[li]The concatenation operator in PHP is [tt].[/tt] ( dot )[/li]
[li]Use distinct [tt]name[/tt]s for your [tt]form[/tt] fields[/li]
[li]Never trust the data received from the client side[/li]
[li]In the future please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.[/li]
[/ul]
Code:
[navy]$query3[/navy] [teal]=[/teal] [green][i]"delete from managelinks where title='"[/i][/green][teal].[/teal][COLOR=darkgoldenrod]mysql_real_escape_string[/color][teal]([/teal][navy]$_GET[/navy][teal][[/teal][green][i]'delete'[/i][/green][teal]]).[/teal][green][i]"'"[/i][/green][teal];[/teal]

Feherke.
 
thanks for ur guidlines but what u told me to do doesnot work as i wanted,,i wanna delete the record what i pressed to delete but this way i tried before it deleted the last record in db ,doesnot recognize which row i select to delete..

hope u get my problem
thanks in ur advance
 
Hi

Well, if you have more than one item in that [tt]form[/tt], then yes, will not work as you want.

First of all, let us turn back to my second sentence :
Feherke said:
[ul][li]Better use [tt]autoincrement[/tt] field to identify records[/li][/ul]
Would that be possible ?

Feherke.
 
:D:D
i make the primarykey of table is the "title" but the problem is that the delete value have the whole array of the all titles that i have in the table so when i tried to delete it deletes one by one from the last record in the array....
 
Hi

That is because all that theory is wrong.

Let us concentrate on the HTML part for now. Supposing that our data looks like this :

[tt] id [gray]|[/gray] title
[gray]----+-------[/gray]
1 [gray]|[/gray] One
2 [gray]|[/gray] Two
3 [gray]|[/gray] Three[/tt]

Some possible ways to go :
Code:
[b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]""[/i][/green][b]>[/b]One   [b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"hidden"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"mid"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"1"[/i][/green][b]><input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Delete"[/i][/green][b]></form>[/b]
[b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]""[/i][/green][b]>[/b]Two   [b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"hidden"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"mid"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"2"[/i][/green][b]><input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Delete"[/i][/green][b]></form>[/b]
[b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]""[/i][/green][b]>[/b]Three [b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"hidden"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"mid"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"3"[/i][/green][b]><input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Delete"[/i][/green][b]></form>[/b]
Code:
[b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]"?mid=1"[/i][/green][b]>[/b]One   [b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Delete"[/i][/green][b]></form>[/b]
[b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]"?mid=2"[/i][/green][b]>[/b]Two   [b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Delete"[/i][/green][b]></form>[/b]
[b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]"?mid=3"[/i][/green][b]>[/b]Three [b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Delete"[/i][/green][b]></form>[/b]
Code:
[b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]""[/i][/green][b]>[/b]
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"hidden"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"mid"[/i][/green][b]>[/b]
One   [b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Delete"[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"this.form.mid.value='1'"[/i][/green][b]>[/b]
Two   [b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Delete"[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"this.form.mid.value='2'"[/i][/green][b]>[/b]
Three [b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Delete"[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"this.form.mid.value='3'"[/i][/green][b]>[/b]
[b]</form>[/b]
Code:
[b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]""[/i][/green][b]>[/b]
One   [b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"delete_1"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Delete"[/i][/green][b]>[/b]
Two   [b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"delete_2"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Delete"[/i][/green][b]>[/b]
Three [b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"delete_3"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Delete"[/i][/green][b]>[/b]
[b]</form>[/b]

Feherke.
 
but i didnot fill the table with my hand i select from db and fill it automatically the while loop make that so i canot put a diffrent values and names for the hidden field
Code:
 while ($links = mysql_fetch_array($queryResult)) 
  {
   echo'<tr><td><input type="textbox" name="ti" value="'.$links->title.'" readonly="readonly"></td>';
   echo'<td><input type="textbox" name="ur" value="'.$links->url.'" readonly="readonly"></td>';
   echo'<td><input type="submit" name="Edit" value="Edit"></td>';            
   echo'<td><input type="submit" name="delete" value="Delete"> <INPUT TYPE="hidden"  name="delete" VALUE="'.$links->title.'"></td>';
echo'</br>';
echo '</tr>';   
  }
 
i get what u said but i still cannot accomplish this
any suggestionsssss!!!!!!!!!!!!!!!!
 
Hi

Posting that code again is pointless. As I wrote, its theory is wrong.

First let us decide about the HTML part, then will generate it from PHP.

In meantime a question which can influence the decision : Will there be other actions beside Delete ?


Feherke.
 
what u mean with any action beside delete...?
 
yes i have 2 buttons else,edit and add
the add i finish and edit has the same idea of the delete
 
Hi

And what should the Edit buttons do ?
[ul]
[li]Get you to another page with the edit [tt]form[/tt] ?[/li]
[li]Just submit the current [tt]form[/tt] and the edited values will be in the initially [tt]readonly[/tt] [tt]input[/tt]s ?[/li]
[/ul]


Feherke.
 
actually, i develop php under drupal if u heared before ,all what i need to get the current value that i want to delete or edit not the all values in the array as i have in this code. then i'll complete what i wish

thanks for ur time
 
thanks for ur time and effort with me ,but it didn't related to drupal development,it is pure php and javascript
 
i suggest that you study a CRUD application to see how they work. i posted a very simple app in this forum which is reposted in here
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top