Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,171 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,260 people online right now. Registration is fast and FREE... Join Now!




Editing mysql database using PHP

 
Reply to this topicStart new topic

Editing mysql database using PHP

dmurkerson24
25 Aug, 2008 - 08:01 AM
Post #1

New D.I.C Head
*

Joined: 25 Jun, 2008
Posts: 4

Hey guys I'm builing some php pages and I've run into a snag. I have a page that lets an admin of the site view entries in the database and then if they click on the edit button it takes them to another page that populates a form with the correct information for them to now edit. My question is I want the admin to either insert a new row in the database (if they happen to change the date of the data) or just update the data (if they change anything but the date). so my php looks something like this:

CODE
//store the original date
$orig_date = $_GET['Date']; //Where the date is being passed through the URL from the previous page

//now process the form
if((isset($_POST['MM_insert'])) && ($_POST['MM_insert'] == "Edit_Topic"))
{
    //store the data from the form fields
global $orig_date;  
$topic_title = htmlentities($_POST['topic_title'], ENT_QUOTES);
   $topic_date = htmlentities($_POST['topic_date'], ENT_QUOTES);
   $topic_description = htmlentities($_POST['topic_description'], ENT_QUOTES);
   $topic_image1 = htmlentities($_POST['topic_image1'], ENT_QUOTES);
   $topic_alttag = htmlentities($_POST['topic_alttag1'], ENT_QUOTES);
   $topic_image2 = htmlentities($_POST['topic_image2'], ENT_QUOTES);
   $topic_alttag2 = htmlentities($_POST['topic_alttag2'], ENT_QUOTES);
   $topic_sound_file = htmlentities($_POST['topic_sound_file'], ENT_QUOTES);
   $topic_video_file = htmlentities($_POST['topic_video_file'], ENT_QUOTES);

   //Create a SQL statement
   if($orig_date != $topic_date)
   {
        $topic_query = "INSERT INTO topic (Title, Date, Description,  Image, AltTag, Image2, AltTag2, SoundFile, VideoFile) VALUES ('$topic_title', '$topic_date', '$topic_description', '$topic_image1', '$topic_alttag', '$topic_image2', '$topic_alttag2', '$topic_sound_file', '$topic_video_file')";
    }
    else
       if($orig_date == $topic_date)
       {
            $topic_query = "UPDATE topic SET Title = '$topic_title', Date = '$topic_date', Description = '$topic_description', Image = '$topic_image1', AltTag = '$topic_alttag', Image2 = '$topic_image2', AltTag2 = '$topic_alttag2', SoundFile = '$topic_sound_file', VideoFile = '$topic_video_file' WHERE Date = '$orig_date'";
        }

     //select the database and execute SQL query
     mysql_select_db($database_uBeatConn);
     $topic_result = mysql_query($topic_query) or die(mysql_error());
}


so my problem seems to be that $orig_date is not keeping it's value throughout the life of the if statement and therefore my code will always create a new row in the database instead of updating when necessary. Any light that can be shed on this will be much appreciated. As stated above the date of the topic to update is passed through the URL from the page where the database entries are viewed.
User is offlineProfile CardPM
+Quote Post

pemcconnell
RE: Editing Mysql Database Using PHP
25 Aug, 2008 - 08:03 AM
Post #2

D.I.C Regular
Group Icon

Joined: 5 Aug, 2008
Posts: 403



Thanked: 38 times
Dream Kudos: 75
My Contributions
Just looked at this very quickly, but the only place where the value of $orig_date seems to be able to get changed is when you call global $orig_date; - i would take that line out as you may have orig_date already assigned to a different value
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:19AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month