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

  • Home
  • SEARCH
  • 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 8143371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:06:24+00:00 2026-06-06T13:06:24+00:00

Please I need your help with grabbing rows from a table and dynamic text

  • 0

Please I need your help with grabbing rows from a table and dynamic text fields, then inserting it inti another table. I’m trying to do is grab some certain state_id from the table below, combine with data from <input type='text' name='constituency[]' /> text box displayed on the same page and then insert into table

                    +---------+--------------------------+
                    |state_id |  state   |   senatorial  |  
                    |--- -----|----------|---------------|
                    |    1    |  utah    |    A-North    | 
                    |    2    |  utah    |    B-South    |  
                    +---------+----------+---------------+

The HTML i’m using to grab information. constituency is a dynamic text field and occur more than once so the need to have it in an array

                    <input type='hidden' name="state_id[]"   value="<?php echo $row['state_id'];?>" />
                    <input type='text'   name='constituency[]' />

Query I’m using to INSERT into table

                    <?php
$constituencys = $_POST['constituency']      ; 
        $state_id = $_POST['state_id']; 


         if(isses($_POST['constituency']))
   {
$constituencys = $_POST['constituency']      ;   
                      foreach($constituencys as          $constituency) {  
     $state_id[$i]= $state_id[$i];
                      $query = mysql_query(" 
         INSERT INTO `constituency` (
                    `constituency_id`,
                    `state_id`,
                    `constituency_name`
                    ) VALUES (
                    NULL,
                    '".$state_id[$i]."',
                    '".$constituency."'
                    "); 
                       }



                    if(mysql_affected_rows()>0){
                    echo "Inserted Successfully";
                    }else{
                    echo "No Row Inserted";
                    echo mysql_error() ;
                    echo"<br>Error:".mysql_error();
                   echo"<br>Query:".$query; 
      } } ?>

Table I’m inserting to looks like this

                    +----------------+-------------+----------------------+
                    |constituency_id |  state_id   |   constituency_name  |  
                    +----------------+-----   -----+----------------------+

When I run the code “No Row Inserted”
is displayed on the screen

Please help me.
I most appreciate it, thank you.

ERROR SHOWN

    Error: You have an error in your mysql syntax; check the manual that corresponds to your mysql server for the right syntax to use near " at line 2

Query:
  • 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-06T13:06:25+00:00Added an answer on June 6, 2026 at 1:06 pm
    <?php
    $constituencys = $_POST['constituency']; 
    $state_id = $_POST['state_id']; 
    
    $i = 0;
    while($i<count($constituencys)){
        $constituencys[$i]= $constituencys[$i];
        $state_id[$i]= $state_id[$i];
        $query = "INSERT INTO `constituency` ( 
            `constituency_id`, `state_id`, `constituency_name`
            ) VALUES (
            NULL,
            '".$state_id[$i]."',
            '".$constituencys[$i]."'
            "; 
    
        mysql_query($query);          
        $i++;  
    }
    
    if(mysql_affected_rows()>0){
        echo "Inserted Successfully";
    } else {
        echo "No Row Inserted";
        echo "<br><br>Error: " . mysql_error();
        echo "<br><br>Query: " . $query;
    } 
    ?>
    

    Try this code first, update your post with results.

    UPDATE

    It looks like you may have ‘dirty’ input. Make sure you sanitize it for mysql before inserting it, by using mysql_real_escape_string. Try the following code.

    <?php
    $constituencys = $_POST['constituency']; 
    $state_id = $_POST['state_id']; 
    
    $i = 0;
    while($i<count($constituencys)){
        $constit = mysql_real_escape_string($constituencys[$i]);
        $state = mysql_real_escape_string($state_id[$i]);
        $query = "INSERT INTO `constituency` ( 
            `constituency_id`, `state_id`, `constituency_name`
            ) VALUES (
            NULL,
            '$state',
            '$constit')
            "; 
    
        mysql_query($query);          
        $i++;  
    }
    
    if(mysql_affected_rows()>0){
        echo "Inserted Successfully";
    } else {
        echo "No Row Inserted";
        echo "<br><br>Error: " . mysql_error();
        echo "<br><br>Query: " . $query;
    } 
    ?>
    

    NOTICE: At the end of your query, you’re missing a close parentheses. It has been added in the second example. Ensure it is there before you continue your tests.

    <?php
    $constituencys = $_POST['constituency']; 
    $state_id = $_POST['state_id']; 
    
    $query = "INSERT INTO `constituency` (`constituency_id`, `state_id`, `constituency_name`) VALUES";
    
    for($i=0; $i<count($constituencys); $i++){
        $constit = mysql_real_escape_string($constituencys[$i]);
        $state = mysql_real_escape_string($state_id[$i]);
        $query .= "(NULL, '$state', '$constit'), "; 
    }
    $query = substr(trim($query), 0, -1); //trims then removes trailing comma
    mysql_query($query);          
    
    if(mysql_affected_rows()>0){
        echo "Inserted Successfully";
    } else {
        echo "No Row Inserted";
        echo "<br><br>Error: " . mysql_error();
        echo "<br><br>Query: " . $query;
    } 
    ?>
    

    Assuming that your HTML inputs are set up as an array correctly, this should work. It also tidies your code and helps with unwanted lag. Every millisecond counts. 🙂 If this doesn’t work, please post returned error and query again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need your help and please give me some advice. From programming pearls I
I need your help please. I have a text file contains lines of lists,
Please i need your help with how to alternate background colours for my comments
Please I need your help with this log-in script. When a user registers, a
Please I need your help with my script. I'm trying to insert values into
Please i need your help with how to include INDEXES in my tables, i've
I've reached the end of my Linq rope. Need your help! Heres my table
Please, I need your help. I have a Follow button. When the button is
Please i need your help. I'm performing SUM() on a column for course_unit, but
please I need your help with a Linq expression: I have nested objects with

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.