0

Possible Duplicate:
PHP $_POST print variable name along with value

I am just wondering how to store $_POST variables in an array.

I have several $_POST variables, as follows:

$_POST['cCode'];
$_POST['sSubject'];
$_POST['lect'];
$_POST['rRoom'];
$_POST['dDay'];
$_POST['sTime'];
$_POST['eTime'];

How can I access them all at the same time using a foreach()?

I know how to access one, like this:

   $data = $_POST['cCode'];
    foreach($data as $code){
        echo $code;
    }

Then I want to save it into mysql..

Here is my database:

DROP TABLE IF EXISTS `ocs_database`.`schedule`;
CREATE TABLE  `ocs_database`.`schedule` (
  `schedID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `courseCode` varchar(30) NOT NULL,
  `subjectName` varchar(45) NOT NULL,
  `roomName` varchar(45) NOT NULL,
  `lecturerName` varchar(45) NOT NULL,
  `day` varchar(45) NOT NULL,
  `startTime` varchar(45) NOT NULL,
  `endTime` varchar(45) NOT NULL,
  PRIMARY KEY (`schedID`)
) ENGINE=InnoDB AUTO_INCREMENT=82 DEFAULT CHARSET=latin1;
0

4 Answers 4

6
foreach ($_POST as $key => $value) {
    echo "$key = $value<br>";
}
Sign up to request clarification or add additional context in comments.

6 Comments

Just code explains very little. OP is obviously new to PHP programming.
However, if I want to save the data from each $_POST into a database how will I do this? I have this query: INSERT INTO schedule (courseCode, subjectName, roomName, lecturerName, day, startTime, endTime) VALUES() how will I put the data into the VALUES()? I'm still new at this PHP thing.
You have only one $_POST. $_POST is array of submitted data. Fetch data that you need, and save it to DB. Where is the problem? What have you tried?
From the $_POST's that I have mentioned earlier, I want to save them all at the same time into my DB with the help of foreach.
What is the database table structure? p.s. in your question there is nothing about saving to DB... Update your question.
|
2

$_POST itself is an array, so you don't need to store it in an other array. do as glavic said.

Comments

1

$array = $_POST; as $_POST itself is an array. just in case, you want $array for future use. and access it using foreach() as you have shown. you can use array_values($_POST), if its only values that you're concerned with. so something like,

    $values = array_values($_POST);
    var_dump($values);

Comments

0

You can used like this for view all variables in $_POST array. print_r($_POST)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.