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 8750549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:53:11+00:00 2026-06-13T12:53:11+00:00

I am trying to post the value chosen for a dropdown menu into my

  • 0

I am trying to post the value chosen for a dropdown menu into my database table. But for some reason its not inputting the value into the database. I am trying to post cat_id into my database. So i use the code below to geenrate my dropdown list from values i alrady have in the database. Then below i have the function that inserts the info into the database. But for some reason its not working. I am suppose to put what is in select name="" right?

<select name="cat[<?=$row['pk_id']?>]">
              <?php $cat = dbConnect("SELECT * FROM category");
                    if(empty($row['cat_id'])){
                    ?>
                    <option value="">Select Category</option>
                    <?php
                    }
 
              ?>
                       
                  
              <?php while($cat_r = mysql_fetch_array($cat)){ 
              
              if($row['cat_id'] == $cat_r['cat_id']){
              ?>
              <option value="<?=$cat_r[cat_id]?>" selected="selected"><?=stripslashes($cat_r[cat_name])?></option>
              <?php
              continue;
              }
              
              ?>
             <option value="<?=$cat_r[cat_id]?>"><?=stripslashes($cat_r[cat_name])?></option>
<?php }  ?>
</select>

Here is my insert to MySQL

dbConnect("INSERT INTO post_info(add_to_random, show_home, source, display_vote_page, cat_id) values(1,1,1,0,cat[.$row['pk_id'].])");

Did i put something wrong here for the value for cat_id? I put cat[.$row[‘pk_id’].]) which is the select name="" for that dropdown list.

Code ported from comment:

if($_POST and $_POST['action'] == 'submit'){ 
  foreach($_POST as $k=>$v){
    $$k = $v;
  } 
  foreach($cat as $k=>$v){
    if($v =='') continue;
    dbConnect("UPDATE twit_info set cat_id=" . $v . " where pk_id =". $k ); 
  }
  if(count($pkid)>0){
    $pid = implode(',',$pkid); 
    dbConnect("UPDATE twit_info set add_to_vote = 1, display_vote_page = 1 where pk_id in(". $pid .")"); 
  }
}
  • 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-13T12:53:12+00:00Added an answer on June 13, 2026 at 12:53 pm

    So in your foreach loop, you are extracting all post keys into global variables via the variable variable $$k (I’ll get to this in a second). In your dbConnect() call, the quoting is incorrect. You should concatenate in $cat.

    dbConnect("
      INSERT INTO post_info
        (add_to_random, show_home, source, display_vote_page, cat_id) 
        values(1,1,1,0, '" . mysql_real_escape_string($cat[$row['pk_id']]) . "')" );
    

    I have added a call to mysql_real_escape_string(). This is necessary at a minimum, to protect all your queries from SQL injection. Your other UPDATE statements are also vulnerable at this point and you MUST perform some escaping on them as well.

    Regarding the extraction of $_POST into global variables – I highly recommend against this. You are in effect imitating the behavior of register_globals which is considered very dangerous. The danger comes in that it is possible for anyone to post any key to your form, in addition to the ones you actually expect to receive, potentially initializing another variable in your script to a value sent via $_POST when your script doesn’t expect it.

    Although I really just recommend operating on $_POST directly, rather than extracting to global variables, if you must extract them to globals, I advise you to use a whitelist of acceptable $_POST keys:

    // Make an array of allowed keys
    $good_keys = ('action', 'cat', 'otherformkey');
    foreach($cat as $k=>$v){
      // Only extract if it is one of the allowed keys
      if($v =='' || !in_array($k, $good_keys) continue;
    
      // Cast to an integer
      $v = intval($v);
      $k = intval($k);
      // Non-integer strings will cast to zero, so don't do the db action.
      if ($v > 0 && $k > 0) {
        dbConnect("UPDATE twit_info set cat_id=" . $v . " where pk_id =". $k ); 
      }
      // For string values which are quoted in the SQL (unlike the int values above)
      // escape them with mysql_real_escape_string()
      // $v = mysql_real_escape_string($v)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to post into a database table, 3 different bits of information,
I need some help sorting my score value's of my sql database <table border
I am trying to customize a script and need to get a POST value
I am trying to use the return value(response) from the callback outside the jQuery.post
Trying to post JSON data to Spring controller.. Not working at all JSP Code:
I am trying to post the value of the textbox and have that same
So, I'm trying to post the value of an element with an id of
I'll keep this nice and short. I'm trying to post a value to a
Trying to post information for nested parameters to a rails app and having some
i am trying to post data via HTTP Post using name value key pair.

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.