Jump to content

Need to store multiple array from form to mysql


Go to solution Solved by Barand,

Recommended Posts

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

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.

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

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!

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);

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!

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

: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.

 

 

  • Solution

 

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

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.