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;
Community
  • 1
  • 1
user1715120
  • 11
  • 1
  • 1
  • 3

4 Answers4

6
foreach ($_POST as $key => $value) {
    echo "$key = $value<br>";
}
Glavić
  • 42,781
  • 13
  • 77
  • 107
  • 1
    Just code explains very little. OP is obviously new to PHP programming. – mario Oct 02 '12 at 18:22
  • 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. – user1715120 Oct 02 '12 at 18:35
  • 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? – Glavić Oct 02 '12 at 18:37
  • 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. – user1715120 Oct 02 '12 at 18:40
  • What is the database table structure? p.s. in your question there is nothing about saving to DB... Update your question. – Glavić Oct 02 '12 at 18:47
  • my apologies. I have updated the question and added the table structure. – user1715120 Oct 02 '12 at 18:54
2

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

poudigne
  • 1,694
  • 3
  • 17
  • 40
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);
Teena Thomas
  • 5,139
  • 1
  • 13
  • 17
0

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

ForceMan
  • 25
  • 1
  • 7