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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with a form and delete function

Status
Not open for further replies.

cs7536

Programmer
Joined
Apr 20, 2003
Messages
130
Location
US
I have a page called viewdb.php that displays images and articles that I posted. Along with the image and article there is a radio select button that is displayed with each article posted.

<input name=&quot;id&quot; type=&quot;radio&quot; value=&quot;id&quot;>

well the idea is to select an article by way of the radio button and then choose an option of an update, delete or view button.

my delete button points to this page called delete.php with the following code:

<?php
$id= &quot;id&quot;;

$db = mysql_connect(&quot;localhost&quot;, &quot;***&quot;, &quot;***&quot;);
mysql_select_db(&quot;userdb&quot;,$db);
mysql_query(&quot;DELETE FROM Portfolio WHERE id=$id LIMIT 1&quot;,$db);
echo &quot;Information Deleted&quot;;
?>

the button that points to the delete page is not a submit button so it does not use a form element. it is a regular button with a tag that directs you to a the delete page with the obove delete code. the idea is to know what article I selected and allow me to update, delete or view that article.

my problem is that when I hit delete it deletes all of my articles not just the one I selected, and if I add the LIMIT 1 to the script then it only deletes 1 article but not the selected one, it deletes the first article displayed on the page.

does anybody have any idea how I can get this working correctly?

Thanks in advance.
Max

Nothing is hard when you realy want to learn it.

Max
 
hi,
$id= &quot;id&quot;;//?? try printing $id and see if it gets the ID value of the selected article.

Print the query before executing and see if it exactly like u want..




--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
If you do not submit you need to pass the ID parameter as a GET string attached to the URL of delete.php

You could also use a session to achieve that.

You can handle the update/delete etc. in one PHP file and post all actions to the same file. All buttons would be submit buttons with different names, e.g. <input type=&quot;submit&quot; name=&quot;delete&quot; value=&quot;Delete&quot;>

In the script then just enclose the appropriate action in an if statement that checks for the button that was used to submit the form. $_POSt['delete'] is set when the button was used to sumit the form.
 
ok I see that makes lots of scenece, on both I will try thm both.. I will post If I have any more problems,, thanks alot..

Max

Nothing is hard when you realy want to learn it.

Max
 
I am still having problems, I got it to work to a certain point. my view.php page looks like this:

<?php
$imagedir= &quot;images/&quot;;
$imagename= &quot;image&quot;;
$select= &quot;$select&quot;;
$Warning= &quot;Warning&quot;;

$db = mysql_connect(&quot;localhost&quot;, &quot;***&quot;,&quot;***&quot;);
mysql_select_db(&quot;userdb&quot;,$db);

$result = mysql_query (&quot;SELECT image, text FROM $select&quot;);

if ($select ==&quot;nothing&quot;){
echo &quot;<strong>$Warning:</strong>&quot;;
echo &quot;<br>&quot;;
echo &quot;You must choose a category first before proceeding. A category to view was not selected.
<br><strong><br>Please click on one of the links below to continue.</strong>&quot;;
}

elseif ($row = mysql_fetch_array($result)) {

do {
echo '
<html>
<body>
<tr><td><input name=&quot;id&quot; type=&quot;radio&quot; value=&quot;id&quot;>
</body>
</html>';
echo &quot;<td>&quot;;
echo &quot;<img src=&quot;.$imagedir.$row[&quot;image&quot;].&quot; width='67' height='100'>&quot;;
echo &quot;<br>&quot;;
echo &quot;<p>&quot;;
echo &quot;<td>&quot;;
echo $row[&quot;text&quot;];

} while($row = mysql_fetch_array($result));

} else {print &quot;Sorry, no article posted at this time!&quot;;
}
?>

and my editpage.php looks like this:

<?php
if($View){
$imagedir= &quot;images/&quot;;
$db = mysql_connect(&quot;localhost&quot;, &quot;***&quot;, &quot;***&quot;);
mysql_select_db(&quot;userdb&quot;,$db);
$result = mysql_query(&quot;SELECT * FROM Portfolio WHERE id=$id&quot;,$db);

echo &quot;<TABLE BORDER=0>&quot;; //this is the thickness of the table border
echo&quot;<TR><TD><B></B><TD><B></B></TR>&quot;;

$myrow = mysql_fetch_array($result);
echo &quot;<tr><td><br><img src=&quot;.$imagedir.$myrow[&quot;image&quot;].&quot; width='288' height='432'>&quot;;
echo &quot;<td><br>&quot;.$myrow[&quot;text&quot;];

}
?>

my view.php page has an edit, delete and view submit button that call the editpage.php by way of a form submit. now like DRJ478 said I put all of the edit code on one page but for this post I am only displaying the view part at this time. Now it calls the approriot part of the code rather it be edit, updat or view, but it is not working the article that I am selecting via the radio button on the view.php page. No matter what radio button I select it is defaulting to the top article displayed and not the one I select.

does any body know what I am doing wrong?

Max

Nothing is hard when you realy want to learn it.

Max
 
And how the edit.php is getting called?
I assume that u have a form tag in ur view.php
how ru passinf the actuall id of the article?

Code:
$result = mysql_query (&quot;SELECT id,image,text FROM $select&quot;);// select id as well

if ($select ==&quot;nothing&quot;){
        echo &quot;<strong>$Warning:</strong>&quot;;
        echo &quot;<br>&quot;;
        echo &quot;You must choose a category first before proceeding. A category to view was not selected.
        <br><strong><br>Please click on one of the links below to continue.</strong>&quot;;
}

elseif ($row = mysql_fetch_array($result)) {

do {
  echo '
<html>
    <body>
            <tr><td><input name=&quot;id&quot; type=&quot;radio&quot; value=&quot;<? echo $row[id]; ?>&quot;><!-- the value ur gettin frm DB
    ...
    ....


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Code:
How do you retrieve the id value? If you retrieve the value using $id and not by $_POST['id'] make sure register_globals
 is ON
 
I am not understanding how to utilize the $_POST['id'] I read php.net but can not understand it, can someone please explain so that I can understand base on the code that I have posted.

Thanks
Max

Nothing is hard when you realy want to learn it.

Max
 
You can see that what is encoded is $id, the literal, as urlencoded string - %24 is a dollar sign.
That is an indication that the variable is not substituted i.e.
1. it's not in a <? ?> PHP section.
2. or you echo the whole thing out and have it it single quotes.
Code:
echo '<input name=&quot;id&quot; type=&quot;radio&quot; value=&quot;$id&quot;>';
will not work because PHP does not substitute variables within single quoted strings.
For a full echo statemenmt you need
Code:
echo '<input name=&quot;id&quot; type=&quot;radio&quot; value=&quot;'.$id.'&quot;>';
Concatenate the variable and the rest of the print/echo statement.

Spookie's code also works:
Code:
<input name=&quot;id&quot; type=&quot;radio&quot; value=&quot;<? echo $row[id]; ?>&quot;>

 
Hello DRJ478 and all the other developers that answered my request, I appreciate the guidance that you are giving me to assist me in resolving my php issue here.

DRJ478

I read carefully what you said above and what sppok said as well, I tried tem both but the results are still nothing I must e doing something wrong sme where and can figure it out. It is fustrating cause I have working on this issue for 3 weeks now.

Nothing is hard when you realy want to learn it.

Max
 
Let's see your latest code.
I'll have another look.

Don't be discouraged!
 
<?php
$imagedir= &quot;images/&quot;;
$imagename= &quot;image&quot;;
$select= &quot;$select&quot;;
$Warning= &quot;Warning&quot;;


$db = mysql_connect(&quot;localhost&quot;, &quot;***&quot;,&quot;***&quot;);
mysql_select_db(&quot;user&quot;,$db);

$result = mysql_query (&quot;SELECT id, image, text FROM $select&quot;);

//echo &quot;<TABLE BORDER=1>&quot;;

if ($select ==&quot;nothing&quot;){
echo &quot;<strong>$Warning:</strong>&quot;;
echo &quot;<br>&quot;;
echo &quot;You must choose a category first before proceeding. A category to view was not selected.
<br><strong><br>Please click on one of the links below to continue.</strong>&quot;;
}

elseif ($row = mysql_fetch_array($result)) {

do {
echo '<input name=&quot;id&quot; type=&quot;radio&quot; value=&quot;'.$id.'&quot;>';

$title = &quot;$select&quot;;

$_SESSION['pagetitle'] = $title;
//$num = $row[&quot;id&quot;];
//$_SESSION['id'] = $id;

echo &quot;<tr><td>&quot;;
echo &quot;<img src=&quot;.$imagedir.$row[&quot;image&quot;].&quot; width='67' height='100'>&quot;;
//echo &quot;<hr>&quot;; //add a line devider code
echo &quot;<br>&quot;;
echo &quot;<p>&quot;; // Skip a line and go to next
//echo &quot;<li>&quot;; adds a bullet dot
//echo &quot;<ul>&quot;; Add a indent before the text
echo &quot;<td>&quot;;
echo $row[&quot;text&quot;];

echo &quot;<TD><a href=view.php?action=view&id=&quot;.$row['id'].&quot;>View</a><p>&quot;;
echo &quot;<br>&quot;;
//echo &quot;<a href=addedit.php?action=edit&id=&quot;.$row['id'].&quot;>Edit</a><p>&quot;;
echo &quot;<a href=delete.php?action=delete&id=&quot;.$row['id'].&quot;>Delete</a><p>&quot;;

/* echo (&quot;<br>&quot;); go to next line */

} while($row = mysql_fetch_array($result));

} else {print &quot;Sorry, no article posted at this time!&quot;;
}
?>

now notice towards the lower part of the code I have created a code that does exactly what I need, but that id by way of links, I need that out come to be done by way of a button. now does the method setting have anything to do with it, I have it set on default.

Nothing is hard when you realy want to learn it.

Max
 
DRJ478

I got it, I used the &quot;'.$is.'&quot;
and I created a Variable that $id = $row[&quot;id&quot;]; and that did tha trick I had not defined a variable and that was the problem but also when I did do this before I did not Concatenate the id on the radio script..

Thanks alot, now I know what to do and what nbot todo..

Thank you everybody that assist me in working through this.
Musch appreciated.
Max

Nothing is hard when you realy want to learn it.

Max
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top