Jump to content


Photo

Need to store multiple array from form to mysql

php mysql multiple aray

Best Answer Barand, 02 March 2013 - 09:35 AM

So now we construct a query that looks like this

INSERT INTO adult (Emri, Dtlindja, Vendlindja, Pass, DtLeshimit, DtSkadimit)
VALUES
('Adult 1','1964-06-01','PlaceofBirth','BB123654789','2012-02-22','2013-02-21'),
('Adult 2','1985-11-25','PlaceofBirth 2','CC987456321','2012-01-02','2012-01-01')

 

Note that brackets go round each record, so you sprintf format should be

 

$dataREZ[] = sprintf("('%s','%s','%s','%s','%s','%s')",

 

with brackets at start and end

Go to the full post


  • Please log in to reply
13 replies to this topic

#1 kleidi24

kleidi24

    Member

  • Members
  • PipPip
  • 11 posts

Posted 23 February 2013 - 05:19 AM

I have an html form that, via javascript, depending on the user selection, appears some input fields in a row. This is the javascript:
function addInputAdult(num){
inputElementsAdult.innerHTML='';
for(xAdult=0;xAdult<num;xAdult++)inputElementsAdult.innerHTML=inputElementsAdult.innerHTML +'<table width="100%" style="margin:0; padding:0;"><tr><th width="24%" scope="col">Adult '+(xAdult + 1)+' - Name</th><th width="13%" scope="col">Birthdate</th><th width="18%" scope="col">Place of Birth</th><th width="13%" scope="col">Passport Nr</th><th width="13%" scope="col">Issue Date</th><th width="16%" scope="col">Expiry Date</th></tr><tr><td> <input name="adlt[][Emri]" type="text" id="adltEmri" style="width:98%;" /></td><td> <input style="width:95%;" type="text" name="adlt[][Dtlindja]" id="adltDtlindja" /></td><td> <input style="width:90%;" type="text" name="adlt[][Vendlindja]" id="adltVendlindja" /></td><td> <input style="width:90%;" type="text" name="adlt[][Pass]" id="adltPass" /></td><td> <input style="width:90%;" type="text" name="adlt[][DtLeshimit]" id="adltDtLeshimit" /></td><td><input style="width:90%;" type="text" name="adlt[][DtSkadimit]" id="adltDtSkadimit" /></td></tr></table>';
}
So, the user make the selection and the form appears. Now, i want to add the user input details in my mysql database. Can you help me on this please? I'm not "sure" how to do this.
My problem is on getting the informations from the form in a way that those information could be usable and understandable. What i have tried is:
foreach ( $_POST['adlt'] as $adult) {
$getd=implode(",",$adult);
echo $getd;
}
...but it shows all the information not separated and I don't know how to separate informations from each other. Output looks like this:
adult1 Nameadult1Birthdateadult1Passportadult1PlaceofBirthadult1IssueDateadult1expirydateadult2 Nameadult2 Birthdate...and so on
Can you help me on this, please?
Thank you in advance

#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,783 posts
  • LocationCheshire, UK

Posted 23 February 2013 - 08:47 AM

start by using

echo '<pre>',print_r($_GET, true),'</pre>';

to see what is being sent from the form (if your form method is POST then use $_POST above)

moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#3 kleidi24

kleidi24

    Member

  • Members
  • PipPip
  • 11 posts

Posted 23 February 2013 - 05:17 PM

start by using

echo '<pre>',print_r($_GET, true),'</pre>';

to see what is being sent from the form (if your form method is POST then use $_POST above)

Thank you for your reply.
It shows the informations as the users inputs on the form. For our case:
[adlt] => Array
	    (
		    [0] => Array
			    (
				    [Emri] => Adult 1
			    )
		    [1] => Array
			    (
				    [Dtlindja] => 22.02.1910
			    )
		    [2] => Array
			    (
				    [Vendlindja] => Paris
			    )
		    [3] => Array
			    (
				    [Pass] => BD12345698
			    )
		    [4] => Array
			    (
				    [DtLeshimit] => 22.02.2012
			    )
		    [5] => Array
			    (
				    [DtSkadimit] => 21.02.2022
			    )
		    [6] => Array
			    (
				    [Emri] => adult 2
			    )
		    [7] => Array
			    (
				    [Dtlindja] => 02.02.2002
			    )
		    [8] => Array
			    (
				    [Vendlindja] => London
			    )
		    [9] => Array
			    (
				    [Pass] => LK987654321
			    )
		    [10] => Array
			    (
				    [DtLeshimit] => 02.02.2012
			    )
		    [11] => Array
			    (
				    [DtSkadimit] => 01.02.2013
			    )
	    )
What i need is a way to get those informations separatly for each person/adult, and insert them in a row for each one person inserted in the form.
Thanks again.

#4 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,783 posts
  • LocationCheshire, UK

Posted 23 February 2013 - 05:34 PM

That array only contains data for one adult.

If you form will containd data for several adults then you need to change your naming convention

eg
<input name="adlt[$xAdult][Emri]" type="text" id="adltEmri" style="width:98%;" />
<input style="width:95%;" type="text" name="adlt[$xAdult][Dtlindja]" id="adltDtlindja" />

so that the fields are grouped for each value of $xAdult

moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#5 kleidi24

kleidi24

    Member

  • Members
  • PipPip
  • 11 posts

Posted 25 February 2013 - 07:26 AM

That array only contains data for one adult.

If you form will containd data for several adults then you need to change your naming convention

eg

<input name="adlt[$xAdult][Emri]" type="text" id="adltEmri" style="width:98%;" />
<input style="width:95%;" type="text" name="adlt[$xAdult][Dtlindja]" id="adltDtlindja" />

so that the fields are grouped for each value of $xAdult

Hello,
Thank you very much for your help. Now is structured as follow:
[adlt] => Array
	    (
		    [0] => Array
			    (
				    [Emri] => Adult 1
				    [Dtlindja] => BirthDate
				    [Vendlindja] => PlaceofBirth
				    [Pass] => BB123654789
				    [DtLeshimit] => 22.02.2012
				    [DtSkadimit] => 21.02.2013
			    )
		    [1] => Array
			    (
				    [Emri] => Adult 2
				    [Dtlindja] => Birthdate 2
				    [Vendlindja] => PlaceofBirth 2
				    [Pass] => CC987456321
				    [DtLeshimit] =>02.01.2012
				    [DtSkadimit] => 01.01.2012
			    )
	    )
This is a big step ;-) Now, how sql query should look like for inserting those informations? Can you help me on this, too, pleease?
Thanks again!

#6 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,783 posts
  • LocationCheshire, UK

Posted 25 February 2013 - 10:06 AM

First you need a table, so I am assuming

CREATE TABLE adult (
	    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	    Emri VARCHAR(50),
	    Dtlindja DATE,
	    Vendlindja VARCHAR(50),
	    Pass VARCHAR(12),
	    DtLeshimit DATE,
	    DtSkadimit DATE
    );

Your dates will need reformatting to yyyy-mm-mdd format and string values need to be sanitized against SQL injection.

So now we construct a query that looks like this

INSERT INTO adult (Emri, Dtlindja, Vendlindja, Pass, DtLeshimit, DtSkadimit)
VALUES
('Adult 1','1964-06-01','PlaceofBirth','BB123654789','2012-02-22','2013-02-21'),
('Adult 2','1985-11-25','PlaceofBirth 2','CC987456321','2012-01-02','2012-01-01')

Here's the code

$db = new mysqli(HOST,USERNAME,PASSWORD,'test');

$data = array();			   
foreach ($adlt as $a) {
    /**
    * sanitize string data and format dates
    */
    $data[] = sprintf("('%s','%s','%s','%s','%s','%s')",
			    $db->real_escape_string($a['Emri']),
			    date('Y-m-d', strtotime($a['Dtlindja'])),
			    $db->real_escape_string($a['Vendlindja']),
			    $db->real_escape_string($a['Pass']),
			    date('Y-m-d', strtotime($a['DtLeshimit'])),
			    date('Y-m-d', strtotime($a['DtSkadimit']))
			    );
}

/**********
* insert the data
*/
$sql = "INSERT INTO adult (Emri, Dtlindja, Vendlindja, Pass, DtLeshimit, DtSkadimit)
	    VALUES \n" . join(",\n", $data);

$db->query($sql);

moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#7 kleidi24

kleidi24

    Member

  • Members
  • PipPip
  • 11 posts

Posted 25 February 2013 - 10:21 AM

I'll give a try and reply you. But, i'm sure that will be perfect. Thank you again. Have e nice day.

#8 kleidi24

kleidi24

    Member

  • Members
  • PipPip
  • 11 posts

Posted 01 March 2013 - 08:47 AM

Hello Barand,

   I have tried what you sugessted but seems that i'm making something wrong since i'm getting this error:

Fatal error: Call to undefined method mysqli::mysql_real_escape_string() in D:\VertrigoServ\www\union\rezervo\rezervo.php on line 144

...and the lines 143-150 looks like this:

		$dataREZ[] = sprintf("'%s','%s','%s','%s','%s','%s'",
					$dbREZ->mysql_real_escape_string($a['Emri']),
					date('Y-m-d', strtotime($a['Dtlindja'])),
					$dbREZ->mysql_real_escape_string($a['Vendlindja']),
					$dbREZ->mysql_real_escape_string($a['Pass']),
					date('Y-m-d', strtotime($a['DtLeshimit'])),
					date('Y-m-d', strtotime($a['DtSkadimit']))
					);

I have tried with real_escape_string as you suggested but the error is the same. Also, i have tried to remove  mysql_real_escape_string and it works, but the field is blank on the db. Any suggestion?

 

Thank you!



#9 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,783 posts
  • LocationCheshire, UK

Posted 01 March 2013 - 09:46 AM

I am using

$db->real_escape_string($a['Emri'])

 

You are trying to use

$dbREZ->mysql_real_escape_string($a['Emri'])

 

Spot the difference?


moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#10 kleidi24

kleidi24

    Member

  • Members
  • PipPip
  • 11 posts

Posted 01 March 2013 - 10:41 AM

I am using

 

$db->real_escape_string($a['Emri'])

 

You are trying to use

 

$dbREZ->mysql_real_escape_string($a['Emri'])

 

Spot the difference?

 

Yep, but i have make some changes on your suggested code and the full code looks like this now:

		define('HOST', 'localhost');
		define('DBNAME', asdfg');
		define('USERNAME', 'root');
		define('PASSWORD', 'gfdsa');
		$dbREZ = new mysqli(HOST,USERNAME,PASSWORD,DBNAME);
		$dataREZ = array();			   
		foreach ($_POST['adlt'] as $a) {
		/**
		* sanitize string data and format dates
		*/
		$dataREZ[] = sprintf("'%s','%s','%s','%s','%s','%s'",
					$dbREZ->mysql_real_escape_string($a['Emri']),
					date('Y-m-d', strtotime($a['Dtlindja'])),
					$dbREZ->mysql_real_escape_string($a['Vendlindja']),
					$dbREZ->mysql_real_escape_string($a['Pass']),
					date('Y-m-d', strtotime($a['DtLeshimit'])),
					date('Y-m-d', strtotime($a['DtSkadimit']))
					);
	}
	$sqlSHTOADLT = "INSERT INTO rezervimet_emrat (rezEmEmri, rezEmDatelindja, rezEmVendlindja, rezEmPass, rezEmDtLeshimit, rezEmDtSkadimit, rezEmRezID, rezEmOfertaDC, rezEmOferta)
			VALUES \n(" . join(",\n", $dataREZ).", $row[0], $ofertaDC, $ofertaKodi)";
	$dbREZ->query($sqlSHTOADLT);

Thanks



#11 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,783 posts
  • LocationCheshire, UK

Posted 01 March 2013 - 11:16 AM

:facewall:

 

You are still using mysql_real_escape_string() instead of real_escape_string()


moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#12 kleidi24

kleidi24

    Member

  • Members
  • PipPip
  • 11 posts

Posted 02 March 2013 - 07:59 AM

:facewall:

 

You are still using mysql_real_escape_string() instead of real_escape_string()


Nice emoticon :-P Sorry for my incompetence :-/

I have made the changes an now it looks like this:

$dataREZ[] = sprintf("'%s','%s','%s','%s','%s','%s'",
					$dbREZ->real_escape_string($a['Emri']),
					date('Y-m-d', strtotime($a['Dtlindja'])),
					$dbREZ->real_escape_string($a['Vendlindja']),
					$dbREZ->real_escape_string($a['Pass']),
					date('Y-m-d', strtotime($a['DtLeshimit'])),
					date('Y-m-d', strtotime($a['DtSkadimit']))
					);
	}

But i have a problem with insert query. In this case:

	$sqlSHTOADLT = "INSERT INTO rezervimet_emrat (rezEmEmri, rezEmDatelindja, rezEmVendlindja, rezEmPass, rezEmDtLeshimit, rezEmDtSkadimit)
	    VALUES \n" . join(",\n", $data);

...it didn't shows any error but when i echo the query, it shows this warning:

Warning: join() [function.join]: Invalid arguments passed in D:\VertrigoServ\www\qwerty\rezervo\rezervo.php on line 154
INSERT INTO rezervimet_emrat (rezEmEmri, rezEmDatelindja, rezEmVendlindja, rezEmPass, rezEmDtLeshimit, rezEmDtSkadimit) VALUES 

...if i change the query and made it like this:

$sqlSHTOADLT = "INSERT INTO rezervimet_emrat (rezEmEmri, rezEmDatelindja, rezEmVendlindja, rezEmPass, rezEmDtLeshimit, rezEmDtSkadimit, rezEmRezID, rezEmOfertaDC, rezEmOferta)			VALUES \n(" . join(",\n", $dataREZ).", $row[0], $ofertaDC, $ofertaKodi)";

...does not appear any error, too, but it didn't insert anything.The query looks like this:

INSERT INTO rezervimet_emrat (rezEmEmri, rezEmDatelindja, rezEmVendlindja, rezEmPass, rezEmDtLeshimit, rezEmDtSkadimit, rezEmRezID, rezEmOfertaDC, rezEmOferta) VALUES (Name1 Surname1','1986-08-24','Somewhere','BD123654789','2009-08-25','2019-08-24', 'Name2 Surname2','1981-11-28','SomewhereElse','QW987654321','2008-05-05','2018-05-04', 207, 140, O-UNT-323)

What i'm doing wrong?

Thank you.


 



#13 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,783 posts
  • LocationCheshire, UK

Posted 02 March 2013 - 09:35 AM   Best Answer

So now we construct a query that looks like this

INSERT INTO adult (Emri, Dtlindja, Vendlindja, Pass, DtLeshimit, DtSkadimit)
VALUES
('Adult 1','1964-06-01','PlaceofBirth','BB123654789','2012-02-22','2013-02-21'),
('Adult 2','1985-11-25','PlaceofBirth 2','CC987456321','2012-01-02','2012-01-01')

 

Note that brackets go round each record, so you sprintf format should be

 

$dataREZ[] = sprintf("('%s','%s','%s','%s','%s','%s')",

 

with brackets at start and end


moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#14 kleidi24

kleidi24

    Member

  • Members
  • PipPip
  • 11 posts

Posted 04 March 2013 - 07:19 AM

Mr Barand,

    Thank you very very very much. It works now.

Thanks again and have a nice day.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com