Jump to content

How to work with this "array"?


zid

Recommended Posts

Hi,

having a form that have unique name value

 

so a submitted post would look like this if i run print_r($_POST):

Array ( [19_y] => 1 [19_c] => this is a text message [18_y] => 1 [18_c] => this is another text message [17_n] => 1 [16_dn] => 1 [16_c] => [submit_questions] => )

Usually i have static values on the posts like $_POST['username'];

But this is a survey so one can create a bunch of questions and each object like 19_y is uniqe.

 

How can I bind this to something that I can post into mysql?

Link to comment
Share on other sites

I commented out the escaping so can run this code.

 

 

$data = Array ("19_y" => "1", "19_c" => "this is a text message", "18_y" => "1", "18_c" => "this is another text message", "17_n" => "1", "16_dn" => "1", "16_c" => "submit_questions");
$columns = implode(", ",array_keys($data));
$escaped_values = array_map('trim', array_values($data));
//$escaped_values = array_map('mysqli_real_escape_string', array_values($escaped_values));
$values  = implode(", ", $escaped_values);
$sql = "INSERT INTO `tablename`($columns) VALUES ($values)";
echo $sql;

 

results:

INSERT INTO `tablename`(19_y, 19_c, 18_y, 18_c, 17_n, 16_dn, 16_c) VALUES (1, this is a text message, 1, this is another text message, 1, 1, submit_questions)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.