0

I want to build a mysql statement with the keys i get from the POST url. Is that possible? I have a dropdownlist in my html, so I am able to select different tables with different sizes.

So insted of (firstname, lastname), maybe i could get it from the array($keys). I'm very new to all this php. and it's for a school project.

<?php
//array with keys
$keys = array();

foreach($_POST as $key=>$value)
{
   array_push($keys, "$key=$value");
}

//
$sql="INSERT INTO $dbTable(firstname, lastname)
VALUES
('$_POST[firstname]','$_POST[lastname]')";
?>
2
  • 2
    No one got educated by asking someone else to do their homework. They just got a passing grade and continued on uneducated. That said, the answer is yes! You really can do it. Now, go try! :-) Commented Mar 18, 2014 at 23:28
  • First of all this is not 'homework'. Its a freestyle assignment. This is just a small part, and its where i got stuck. And why should i not ask a question if i don't know the answer. Do you want me to ask it when i know the answer. Thank you for being useless... Commented Mar 19, 2014 at 6:38

1 Answer 1

3

I would recommend taking a look at how to access post parameters. A great resource is http://www.php.net/manual/en/reserved.variables.post.php. Once you have the post variables take a look at how to concatenate here http://www.php.net/manual/en/language.operators.string.php. Something to be aware of any time you are directly putting variables into SQL though is SQL injection. Take a look at the wiki for it here http://en.wikipedia.org/wiki/SQL_injection.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 For php.net - Reading the manual is always a good idea when you're new.
Thank you. I think http_build_query is the sort of thing i am looking for, not completely sure. This is all still very new to me.
Unless I misunderstood your question http_build_query does the opposite of what you are trying to do. Check out the docs here us1.php.net/http_build_query. What that does is converts an object into a serialized string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.