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
https://forums.phpfreaks.com/topic/288430-how-to-work-with-this-array/
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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.