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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:36:39+00:00 2026-05-30T21:36:39+00:00

code updated i have a table called vote with three fields ans_1,ans_2,ans_3 query strings

  • 0

code updated

i have a table called vote with three fields ans_1,ans_2,ans_3
query strings number is 2 or 3 according to answers the admin is going to save
so they look like this ?1=aaa&2=bbb or ?1=aaa&2=bbb&3=ccc
my point is to save every query string in a column so i use the code below but it keeps using the last value of the query string only

$queries = $_SERVER['QUERY_STRING'];
$answers = explode("&",$queries );
$num = count($answers);
foreach($answers as $val){
$chars= strlen($val);
$test = substr($val,2,$chars-2);
for($x=1; $x<=$num; $x++){
    $Q = "update vote set ans_'$x' = '$test' where Vote_ID = '1'";
    $R = mysql_query($Q);
    if($R) { echo "done"; } else { echo mysql_errno(); }    
    }
}
  • 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-05-30T21:36:40+00:00Added an answer on May 30, 2026 at 9:36 pm

    If you have dynamic columns for which you are substituting $x, do not enclose $x in quotes:

    $Q = "update vote set ans_$x = '$test' where Vote_ID = '1'";
    

    Please be sure to escape the contents of $_SERVER['QUERY_STRING'] with mysql_real_escape_string().

    $test = mysql_real_escape_string($test);
    

    The proper way to parse a query string in PHP is with parse_str(), rather than attempting to explode() on the &.

    $queryvars = array();
    $parse_str($_SERVER['QUERY_STRING'], $queryvars);
    foreach ($queryvars as $key=>$value) {
       // do the loop
    }
    

    However, since you are grabbing the whole query string, and not filtering any specific variables, why not just use $_GET?

    $x = 0;
    foreach ($_GET as $key=>$value) {
       // do the loop...
       $test = mysql_real_escape_string($value);
       $Q = "update vote set ans_'$x' = '$test' where Vote_ID = '1'";
       $x++;
    }
    

    Update

    To help you understand why your code isn’t working, I’ll modify it here. However, this is not the preferred method of performing this task. Using foreach($_GET) as above is much better. Indenting the loop properly will help reveal the problem:

    $queries = $_SERVER['QUERY_STRING'];
    $answers = explode("&",$queries );
    $num = count($answers);
    
    // Your foreach loops over the available querystring params:
    // Start by initializing $x to 0
    $x = 0;
    foreach($answers as $val){
      $chars= strlen($val);
      $test = substr($val,2,$chars-2);
    
      // You are already inside the foreach loop, so
      // you don't want to start another loop which uses the same value for $test
      // on each iteration.  Instead $x was set to 0 before the outer foreach...
      // There is no need for an inner loop.
      //for($x=1; $x<=$num; $x++){
        // On first iter here, $x is 0. Increments at the end of the loop iter.
        $Q = "update vote set ans_$x = '$test' where Vote_ID = '1'";
        $R = mysql_query($Q);
        if($R) {
          echo "done"; 
        } else { 
          echo mysql_errno(); 
        }
        // On each iteration, increment $x here.
        $x++;
      //} // the inner for loop, commented out...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called 'sample' with fields as 'sample_id, 'order_id', order_email_id', 'review_request', 'coupon_sent'.
I have some code to update a database table that looks like try {
Okay I have updated my code quite a bit. I am getting a new
Okay I have updated my code a little, but I am still not exactly
I have this function in my Javascript Code that updates html fields with their
I have a table called as language which has a column called as lang_code.
I have a table with about 450,000 row. The table needs some derived fields
I have a data table. Each row of the table has a commandButton called
I have a table called Card that I am auditing with a shadow table
I have a table called order which has many order_items , each order_items is

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.