Current location: Hot Scripts Forums » Programming Languages » PHP » Another Drop Down Menu Q: PHP & MySQL


Another Drop Down Menu Q: PHP & MySQL

Reply
  #1 (permalink)  
Old 06-06-08, 12:28 AM
StrunkWriter StrunkWriter is offline
New Member
 
Join Date: Jun 2008
Location: Chicago
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Another Drop Down Menu Q: PHP & MySQL

I read all the earlier posts I could find regarding PHP/MySql drop down menus, but I still need help with my code. I'm creating a simple database (kind of a glorified blog really) that includes a form to upload information and a separate form to edit the information. It is the "edit" form I'm having an issue with.

If someone selects a row to edit, they are directed to a form that has contains their information and they can edit the information and clicking submit will update the table. My problem exists with the drop down menu in this form.

Inside the form the drop down selects categories which are kept in a separate table. But, because the categories come from a separate table, it doesn't show the category the user originally selected. I'm not explaining this well. So, here's the code (minus extraneous fields and formatting stuff):

PHP Code:

$story_select $_GET['story_select'];


  if(isset(
$_POST['edit']))
{
$category $_POST['category'];
$headline $_POST['headline'];
$byline $_POST['byline'];
$organization $_POST['organization'];
$article $_POST['article'];
$moreinfurl $_POST['moreinfurl'];
$ReleaseDate $_POST['ReleaseDate'];
$news_candidate $_POST['news_candidate'];
mysql_query("UPDATE news_stories SET category = '$category', headline = '$headline', byline = '$byline', organization = '$organization', moreinfurl = '$moreinfurl', article = '$article', ReleaseDate = '$ReleaseDate', news_candidate = '$news_candidate' WHERE storynum = '$story_select'");

echo 
"story was updated!";
}
else
{

$query  "SELECT * FROM news_stories WHERE storynum='$story_select'";
$result mysql_query($query);
while(
$row mysql_fetch_assoc($result))  {

echo  
"<h1>Edit Article# " .$story_select ."</h1>";
echo 
"<form method=\"post\">";
echo 
"Category: <select name=\"category\">";

  
$query2  "SELECT * FROM news_categories";
  
$result2 mysql_query($query2);

  while(
$row2 mysql_fetch_assoc($result2))  {
  echo 
"<option value=\"{$row2['catnum']}\">{$row2['catname']}</option><br>";
  }
  
  echo 
"</select>";
  echo 
"Headline (100 characters max): <input type=\"text\" name=\"headline\" value=\"{$row['headline']}\"> <br>";
  
 echo 
"<input name=\"edit\" type=\"submit\" id=\"edit\" value=\"Edit\">";
echo 
"</form>";
}

 } 
I hope I expressed this clearly. Any help will be appreciated.

Last edited by StrunkWriter; 06-06-08 at 12:30 AM. Reason: typo
Reply With Quote
  #2 (permalink)  
Old 06-06-08, 04:56 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
We're going to need a little help here understanding what it is you want.

I am assuming the category select box is used to change the category you want the article to appear under?

And you want the news article category to appear selected in the dropdown box?

And I am assuming that $category is the value that would be found in $row2['catname']?

Then you could do it like this:
PHP Code:

<?php
$story_select 
$_GET['story_select'];
if(isset(
$_POST['edit']))
{
 
$category $_POST['category'];
 
$headline $_POST['headline'];
 
$byline $_POST['byline'];
 
$organization $_POST['organization'];
 
$article $_POST['article'];
 
$moreinfurl $_POST['moreinfurl'];
 
$ReleaseDate $_POST['ReleaseDate'];
 
$news_candidate $_POST['news_candidate'];
 
mysql_query("UPDATE news_stories SET category = '$category', headline = '$headline', byline = '$byline', organization = '$organization', moreinfurl = '$moreinfurl', article = '$article', ReleaseDate = '$ReleaseDate', news_candidate = '$news_candidate' WHERE storynum = '$story_select'");
 echo 
"story was updated!";
 }
else
{
 
$query  "SELECT * FROM news_stories WHERE storynum='$story_select'";
 
$result mysql_query($query);
 while(
$row mysql_fetch_assoc($result))
 {
  echo  
"<h1>Edit Article# " .$story_select ."</h1>
         <form method=\"post\">
         Category: <select name=\"category\">"
;
  
$query2  "SELECT * FROM news_categories";
  
$result2 mysql_query($query2);
  while(
$row2 mysql_fetch_assoc($result2))
  {
   if(
$row2['catname']==$category)
   {
    echo 
"<option value=\"{$row2['catnum']}\" selected>{$row2['catname']}</option>";
    }
   else
   {
    echo 
"<option value=\"{$row2['catnum']}\">{$row2['catname']}</option>";
    }
   }
  echo 
"</select>
        Headline (100 characters max): <input type=\"text\" name=\"headline\" value=\"
{$row['headline']}\"> <br>
       <input name=\"edit\" type=\"submit\" id=\"edit\" value=\"Edit\">
       </form>"
;
  }
 }
?>
Another problem I see is you don't have the other fields included in the form.
So when the form is submitted, only the category and headline are submitted.

These fields aren't included in the form:
PHP Code:

$byline $_POST['byline'];
$organization $_POST['organization'];
$article $_POST['article'];
$moreinfurl $_POST['moreinfurl'];
$ReleaseDate $_POST['ReleaseDate'];
$news_candidate $_POST['news_candidate']; 
And this is the update query:
PHP Code:

mysql_query("UPDATE news_stories SET category = '$category', headline = '$headline', byline = '$byline', organization = '$organization', moreinfurl = '$moreinfurl', article = '$article', ReleaseDate = '$ReleaseDate', news_candidate = '$news_candidate' WHERE storynum = '$story_select'"); 

So when you submit the form and the table is updated, the excluded fields will contain NULL values and any data that was in them will be lost or the update query won't function at all.

And you aren't checking to see if all the variables have any values before you update the table.
You are only checking to see if the form was submitted.
So if the user were to erase a value in a field then that field would be empty and the data in the table would be over written with a NULL value.

You also should make sure any value submitted doesn't contain and data that could be used to harm your database.
__________________
Jerry Broughton

Last edited by job0107; 06-06-08 at 05:20 AM.
Reply With Quote
  #3 (permalink)  
Old 06-06-08, 12:15 PM
StrunkWriter StrunkWriter is offline
New Member
 
Join Date: Jun 2008
Location: Chicago
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
re. fields not in form

Yah, I cut some of the code out because I didn't want to bore you with un-needed details. I figured after the form field for "headline" y'all could see I know how create a basic form -- so I just cut the rest out when I was posting.

I want the old values to show in every field, so the end user can see what exists before they decide to change it. If they don't edit a field, it will "update" the same information back into the table.

But it's the showing old values part I'm having issue with. How can I have the drop down contain all the options, but have it's "default" value show as the information already contained in the main table?

I guess another possibility is to show the whole form filled out and put an "edit" button next to each field. When the user clicks the edit button they will get a "miniform" to change that particular field -- without changing the other data. What do you think? Is this a "safer" solution for my user or a bigger pain in the butt for them?
Reply With Quote
  #4 (permalink)  
Old 06-06-08, 07:05 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
You have got a lot of ideas, you just need to pick one.
I showed you how to select the item in the dropdown box when the form information matches the database information.

Look, you are loading the data from the record fetched from the news_stories table
and I am assuming you are loading the form inputs with this data.

And you are querying the news_categories table and using the results to populate the select box.

I know the
news_stories table contains a category field.
And the
news_categories table contains a catname field.
And I am assuming that the value in the category field
came from the catname field. So as you can see, I am comparing
the category field with the catname field and
marking that select option as selected, which selects it when displayed.

Now as long as you are loading the form from the database and you are updating the database every time you submit the form.
You just need to reload the form from the database every time you submit the form.

PHP Code:

<?php
$story_select
=!empty($_GET["story_select"])?$_GET["story_select"]:"";
if(isset(
$_POST['edit']))
{
 
$category $_POST["category"];
 
$headline $_POST["headline"];
 
$byline $_POST["byline"];
 
$organization $_POST["organization"];
 
$article $_POST["article"];
 
$moreinfurl $_POST["moreinfurl"];
 
$ReleaseDate $_POST["ReleaseDate"];
 
$news_candidate $_POST["news_candidate"];
 
mysql_query("UPDATE news_stories SET category = '$category', headline = '$headline', byline = '$byline', organization = '$organization', moreinfurl = '$moreinfurl', article = '$article', ReleaseDate = '$ReleaseDate', news_candidate = '$news_candidate' WHERE storynum = '$story_select'");
 echo 
"story was updated!<p>";
 }
if(
$story_select)
{
 
$result mysql_query("SELECT * FROM news_stories WHERE storynum='$story_select'");
 while(
$row mysql_fetch_assoc($result))
 {
  echo  
"<h1>Edit Article# ".$story_select."</h1>
         <form action='?story_select="
.$story_select."' method='post'>
         Category: <select name='category'>"
;
  
$result2 mysql_query("SELECT * FROM news_categories";
  while(
$row2 mysql_fetch_assoc($result2))
  {
   if(
$row2["catname"]==$row["category"])
   {
    echo 
"<option value=\"{$row2['catnum']}\" selected>{$row2['catname']}</option>";
    }
   else
   {
    echo 
"<option value=\"{$row2['catnum']}\">{$row2['catname']}</option>";
    }
   }
  echo 
"</select>
        Headline (100 characters max): <input type='text' name='headline' value=\"
{$row['headline']}\"> <br>
       <input name='edit' type='submit' id='edit' value='Edit'>
       </form>"
;
  }
 }
else{
header("Location: login.php");}
?>
__________________
Jerry Broughton

Last edited by job0107; 06-06-08 at 07:33 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
AnyLink Drop Down Menu - Smarty Error? shadyy510 JavaScript 2 05-08-08 11:44 AM
Xml / Dom / Css Mark_SC.SE JavaScript 0 06-29-05 08:05 AM
PHP multi-dimensional array sorting issue aqw PHP 2 06-24-05 11:09 PM
Value from HTML drop down menu into php variable? flososic PHP 5 06-11-05 05:10 AM
using javascript in php for image changing with drop down menu developer_x PHP 0 02-13-05 08:26 AM


All times are GMT -5. The time now is 07:45 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.