kleidi24 Posted February 23, 2013 Share Posted February 23, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/ Share on other sites More sharing options...
Barand Posted February 23, 2013 Share Posted February 23, 2013 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) Quote Link to comment https://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/#findComment-1414413 Share on other sites More sharing options...
kleidi24 Posted February 23, 2013 Author Share Posted February 23, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/#findComment-1414486 Share on other sites More sharing options...
Barand Posted February 23, 2013 Share Posted February 23, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/#findComment-1414491 Share on other sites More sharing options...
kleidi24 Posted February 25, 2013 Author Share Posted February 25, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/#findComment-1414802 Share on other sites More sharing options...
Barand Posted February 25, 2013 Share Posted February 25, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/#findComment-1414823 Share on other sites More sharing options...
kleidi24 Posted February 25, 2013 Author Share Posted February 25, 2013 I'll give a try and reply you. But, i'm sure that will be perfect. Thank you again. Have e nice day. Quote Link to comment https://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/#findComment-1414826 Share on other sites More sharing options...
kleidi24 Posted March 1, 2013 Author Share Posted March 1, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/#findComment-1415809 Share on other sites More sharing options...
Barand Posted March 1, 2013 Share Posted March 1, 2013 I am using $db->real_escape_string($a['Emri']) You are trying to use $dbREZ->mysql_real_escape_string($a['Emri']) Spot the difference? Quote Link to comment https://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/#findComment-1415822 Share on other sites More sharing options...
kleidi24 Posted March 1, 2013 Author Share Posted March 1, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/#findComment-1415835 Share on other sites More sharing options...
Barand Posted March 1, 2013 Share Posted March 1, 2013 You are still using mysql_real_escape_string() instead of real_escape_string() Quote Link to comment https://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/#findComment-1415847 Share on other sites More sharing options...
kleidi24 Posted March 2, 2013 Author Share Posted March 2, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/#findComment-1415983 Share on other sites More sharing options...
Solution Barand Posted March 2, 2013 Solution Share Posted March 2, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/#findComment-1415994 Share on other sites More sharing options...
kleidi24 Posted March 4, 2013 Author Share Posted March 4, 2013 Mr Barand, Thank you very very very much. It works now. Thanks again and have a nice day. Quote Link to comment https://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/#findComment-1416454 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.