Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8341997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:38:48+00:00 2026-06-09T05:38:48+00:00

I have a problem where I have a fairly basic crud application website. I’m

  • 0

I have a problem where I have a fairly basic crud application website. I’m having trouble with the edit part where I have text inputs and select dropdowns on the page. For all of the text inputs, I’m able to make the value equal to what was already in the database for that row and so, if I go through the edit.php and don’t edit some fields, the value is still kept and not wiped clean for the fields I didn’t touch. Unfortunately, I’m using Jquery to populate some of the select/dropdowns, so this approach doesn’t work.

Example Scenario:

I’m a user, add a business with the first category being Restaurants and the subcategory being American Food and then a second category being Home Services and the subcategory Home Repair. Then I click on the “Save” button and my category’s are inserted. Then I realize I forgot to add my name and phone number to my listing, so I edit the listing, this time inserting just my name and phone number and leaving all the other fields blank. With what I have now, the categories and subcategories I saved the first time are now either set to 0 or blank.

My solution to this was to populate some hidden fields (named beforecat1, beforesubcat1, and so on) that stored the values of what the fields were before, and then if the selects weren’t touched, then insert the values of the hidden fields. Unfortunately, somethings not working.

<? 
include('../config.php'); 
if (isset($_GET['id']) ) { 
   $id = (int) $_GET['id']; 
   if (isset($_POST['submitted'])) { 
       foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); }

   if(isset($_REQUEST['cselect1'])){
       $cselect1 = $_REQUEST['cselect1'];
       $query="SELECT cat FROM cat WHERE id='$cselect1'";
       $result = mysql_query ($query);
       while($catselect=mysql_fetch_array($result)){
           $catselect1 = $catselect['cat'];
       }
   } else {
   $catselect1 = $_REQUEST['beforecat1'];
   }
   if(isset($_REQUEST['cselect2'])){
   $cselect2 = $_REQUEST['cselect2'];

   $query="SELECT cat FROM cat WHERE id='$cselect2'";
   $result = mysql_query ($query);
   while($catselect=mysql_fetch_array($result)){
      $catselect2 = $catselect['cat'];
   }
   } else {
   $catselect2 = $_REQUEST['beforecat2'];
   }
   if(isset($_REQUEST['cselect3'])){
   $cselect3 = $_REQUEST['cselect3'];

   $query="SELECT cat FROM cat WHERE id='$cselect3'";
   $result = mysql_query ($query);
   while($catselect=mysql_fetch_array($result)){
   $catselect3 = $catselect['cat'];
   }
   } else {
   $catselect3 = $_REQUEST['beforecat3'];
   }

   $sql = "UPDATE `company` SET  `name` =  '{$_POST['name']}' ,  `phone` =  '{$_POST['phone']}' ,  `cat1` =  '$catselect1' ,  `cat2` =  '$catselect2' ,  `cat3` =  '$cselect3' ,  `zipcode` =  '{$_POST['zipcode']}' ,  `city` =  '{$_POST['city']}' ,  `address` =  '{$_POST['address']}' ,  `address2` =  '{$_POST['address2']}' ,  `website` =  '{$_POST['website']}' ,  `product1` =  '{$_POST['product1']}' ,  `product2` =  '{$_POST['product2']}' ,  `product3` =  '{$_POST['product3']}' ,  `product4` =  '{$_POST['product4']}' ,  `product5` =  '{$_POST['product5']}' ,  `product6` =  '{$_POST['product6']}' ,  `product7` =  '{$_POST['product7']}' ,  `subcat1` =  '{$_POST['sselect1']}' ,  `subcat2` =  '{$_POST['sselect2']}' ,  `subcat3` =  '{$_POST['sselect3']}'   WHERE `id` = '$id' "; 
   mysql_query($sql) or die(mysql_error());

   $sql = "UPDATE `company_secondary` SET  `company_description` =  '{$_POST['description']}' ,  `since` =  '{$_POST['since']}' ,  `smoking` =  '{$_POST['select3']}' ,  `delivery` =  '{$_POST['select5']}' ,  `alcohol` =  '{$_POST['select6']}' ,  `kids` =  '{$_POST['select1']}' ,  `wheelchair` =  '{$_POST['select2']}' ,  `twitter` =  '{$_POST['twitter']}' ,  `facebook` =  '{$_POST['facebook']}' ,  `youtube` =  '{$_POST['youtube']}' ,  `premium` =  '{$_POST['premium']}' ,  `creditcards` =  '{$_POST['select4']}' ,  `outdoor` =  '{$_POST['select7']}' ,  `featured` =  '{$_POST['featured']}' ,  `shortdesc` =  '{$_POST['shortdesc']}' WHERE company_id = '$id' "; 
   mysql_query($sql) or die(mysql_error());

   echo "Edited Row<br/>";
   echo "<a href='allbiz.php'>Back To Listing</a>"; 
} 

$row = mysql_fetch_array ( mysql_query(“SELECT * FROM company WHERE id = ‘$id’ “));

?>

And here’s my part of my form:

<form action='' method='POST'> 
<p><b>Name:</b><br /><input type='text' name='name' value='<?= $row['name'] ?>' /> 
<p><b>Phone:</b><br /><input type='text' name='phone' value='<?= stripslashes($row['phone']) ?>' />
<?php
    $cat1 = stripslashes($row['cat1']);
    $cat2 = stripslashes($row['cat2']);
    $cat3 = stripslashes($row['cat3']);
    $subcat1 = stripslashes($row['subcat1']);
    $subcat2 = stripslashes($row['subcat2']);
    $subcat3 = stripslashes($row['subcat3']);
?>
<input type='hidden' value='<?php echo $cat1;?>' name='beforecat1'/>
<input type='hidden' value='<?php echo $cat2;?>' name='beforecat2'/>
<input type='hidden' value='<?php echo $cat3;?>' name='beforecat3'/>
<input type='hidden' value='<?php echo $subcat1;?>' name='beforesubcat1'/>
<input type='hidden' value='<?php echo $subcat2;?>' name='beforesubcat2'/>
<input type='hidden' value='<?php echo $subcat3;?>' name='beforesubcat3'/>
<p><b>Cat1:</b><br />
<?php
   $query="SELECT * FROM cat";
   $result = mysql_query ($query);
   echo"<select name='cselect1' id='cat1'><option value='0'>Please Select A Category</option>";
   // printing the list box select command
   while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
        echo "<option value=\"".htmlspecialchars($catinfo['number'])."\">".$catinfo['cat']."</option>";

   }
   echo"</select>";
?>
<?php
   $query="SELECT * FROM subcat";
   $result = mysql_query ($query);
   echo"<select name='sselect1' id='subcat1'><option value=''>Sub Category</option>";
   echo $subcat1;
   echo"'>$sucat1</option>";
   // printing the list box select command
   while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
       echo "<option value='".htmlspecialchars($catinfo['subcat'])."' class='".$catinfo['catnumber']."'>".$catinfo['subcat']."</option>";
   }

   echo"</select>";
 ?>


 <?php
  $query="SELECT * FROM cat";
  $result = mysql_query ($query);
  echo"<select name='cselect2' id='cat2'><option value='0'>Please Select A Category</option>";
  // printing the list box select command
  while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
      echo "<option value=\"".htmlspecialchars($catinfo['number'])."\">".$catinfo['cat']."</option>";

  }
  echo"</select>";
 ?>

 <?php
    $query="SELECT * FROM subcat";
    $result = mysql_query ($query);
    echo"<select name='sselect2' id='subcat2'><option value=''>Sub Category</option>";
    echo $subcat1;
    echo"'>$sucat1</option>";
 // printing the list box select command
    while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
        echo "<option value='".htmlspecialchars($catinfo['subcat'])."' class='".$catinfo['catnumber']."'>".$catinfo['subcat']."</option>";
    }

 echo"</select>";
 ?>

 <?php
 $query="SELECT * FROM cat";
 $result = mysql_query ($query);
 echo"<select name='cselect3' id='cat3'><option value='0'>Please Select A Category</option>";
 // printing the list box select command
 while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
     echo "<option value=\"".htmlspecialchars($catinfo['number'])."\">".$catinfo['cat']."</option>";

 }
 echo"</select>";
?>
<?php
 $query="SELECT * FROM subcat";
 $result = mysql_query ($query);
 echo"<select name='sselect3' id='subcat3'><option value=''>Sub Category</option>";
 // printing the list box select command
 while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
 echo "<option value='".htmlspecialchars($catinfo['subcat'])."' class='".$catinfo['catnumber']."'>".$catinfo['subcat']."</option>";
 }

 echo"</select>";
?>

What’s going wrong? Thanks for all help, this is a problem thats been bugging me for a long time. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:38:50+00:00Added an answer on June 9, 2026 at 5:38 am

    I think a solution for you may be to set the default, selected value for your <select> to whatever the value currently is, when pulled from the DB.

    Let’s expand out this one:

     if(isset($_REQUEST['cselect1'])){
       $cselect1 = $_REQUEST['cselect1'];
       $query="SELECT cat FROM cat WHERE id='$cselect1'";
       $result = mysql_query ($query);
       while($catselect=mysql_fetch_array($result)){
           $catselect1 = $catselect['cat'];
       }
      } else {
        $catselect1 = $_REQUEST['beforecat1'];
      }
    

    While I’m not 100% sure what you’re doing, It looks like your grabbing a value based on the id submitted, and setting the var $catselect1 to the categories name?

    We can use either, but since the crux of this comes down to $catselect1 let’s use it.

    So down here, in this area:

    <?php
    $query="SELECT * FROM cat";
    $result = mysql_query ($query);
    echo"<select name='cselect1' id='cat1'><option value='0'>Please Select A Category</option>";
    // printing the list box select command
    while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
         echo "<option  value=\"".htmlspecialchars($catinfo['number'])."\">".$catinfo['cat']."</option>";
    
    }
    echo"</select>";
    ?>
    

    In your while, we need to compare the $catselect1 value with what I believe is your category name, with $catinfo[‘cat’] (which is also a name??) and add a condition like:

    if (  $catselect1 == $catinfo['cat'] )
      //this one is selected, because the values match!
      echo "<option default selected value=\"".htmlspecialchars($catinfo['number'])."\">".$catinfo['cat']."    </option>";
    } else {
      //do normally
      echo "<option  value=\"".htmlspecialchars($catinfo['number'])."\">".$catinfo['cat']."     </option>";
    }
    

    So when your form is loaded up, your selects will default to the value set in the DB, and will pass that same value if they’re resubmitted.

    Hopefully that makes some sense!

    PS: I do prefer this method for simple true/false echos:

     echo ( $comparison == $comparison ) ? 'true' : 'false';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a wordpress theme, and have run into a fairly basic problem.
Question I have a fairly static website with just a few basic PHP usage.
I have a fairly basic problem using the facebook php SDK in conjunction with
I have what I'm guessing is a fairly basic regular expression problem: // Example
This is a fairly common problem to which I have yet to find an
I'm fairly new to using MSSQL and have run into a weird problem. Given
I'm a beginner at SQL and have this fairly easy conditional problem: Every installation
I am fairly new to PHP OOP, the problem that I am have is
This is a slightly original variation on a common problem. I have a fairly
I have problem with show or hide form in Window Form Application. I start

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.