Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Counting the number of variables in querystring with PHP

Options
  • 08-02-2005 8:47pm
    #1
    Registered Users Posts: 7,868 ✭✭✭


    for my site, i have an admin section. in this, i have the option to edit parts of the tables in the MySQL database. the thing is, each table has a different number of columns so it is hard to build the sql statements as they have to be dynamic. for example, if i want ot add a product to my shop, i have to first choose the table ie electronics, then retrieve the values and put them into text boxes. the problem aises on the next page when i am building the "INSERT" statement. if theres, say, 7 columns, i have to build the appropriate insert statement, but if theres 8 then its different. what im looking for is a way to tell how many variables are sent using get or post and dynamically build the sql statements like so:

    sql="INSERT INTO $_GET["tablename"] ( varying number of headers ) Values ( varying number of values );

    i tried using $_SERVER["argv"] and $_SERVER["argc"] but all i ever get is argc=1 and argv is a single line string containing all the different variables as they are in the querystring ie page.php?name=blah&code=3&blah=blah .

    hope thats understandable. thanx in advance for any help.


Comments

  • Closed Accounts Posts: 6 Saw Doctor


    What about using the count statement when you first query the database. This will tell you how many results you get from the first query and from that you will know how many you should insert later.

    It goes like:

    select count(columnname) from tablename where such and such;

    This will return the number of entries present in the specified column name for whatever condition you specify.

    Hope this helps


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    Look at parse_str to parse the query string ($_SERVER
    Having said that, isn't the data already parsed out into $_GET or $_POST (or $_REQUEST which contains the former two).

    You could install phpMyAdmin and use it to modify your databases or download it and read the source to learn from it.


  • Registered Users Posts: 7,868 ✭✭✭The_B_Man


    im sure i could put a counter variable on the previous page and send it in a hidden field in the form. the problem is when i am building the insert statement, i need to have the exact names of the fields ie:
    insert into table (name, age, address) values ($_GET["age"], $_GET["name"],$_GET["address"] );

    so my code should be:
    sql="insert into table (" // now fill in the bracket

    for (i=0 ; i < $_GET["num_fields"]; i++)
    {
    //fill in rest of the bracket
    }
    sql=sql . ) Values (; // now add values
    for (i=0 ; i < $_GET["num_fields"]; i++)
    {
    // fill in $_GET["num_fields"] amount of variables in form '$_GET["name"]','$_GET["age"],'$_GET["address"]'
    }
    sql=sql . ); //close the sql statement

    so basically i need the number of inputs and also their exact names as they will be different from table to table ie in electronics it might be $_GET["DVD Region"] for example and the same array element might be $_GET["isbn"] in the books section.

    ill try the above suggestions and get back to you. thanx.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    The following is offered as is as I can't be arsed to test it:
    [PHP]foreach ($_GET as $k => $v) {
    if ($k != "tablename") {
    $s_Fields .= $k.", ";
    $s_Values .= "'".$v."', ";
    }
    }
    $s_SQL = "INSERT INTO ".$_GET["tablename"]." (".substr($s_Fields, 0, -2);
    $s_SQL .= ") VALUES (".substr($s_Values, 0, -2).")";[/PHP]


  • Registered Users Posts: 7,868 ✭✭✭The_B_Man


    thanx, ill try it asap. i never used substr() before so hopefully ill figure it out.


  • Advertisement
  • Registered Users Posts: 7,868 ✭✭✭The_B_Man


    thanx Corinthian, ur a genius. worked like a charm.


Advertisement