PMacca_1987 Posted April 16, 2010 Share Posted April 16, 2010 Hi everyone, I am having a problem with the below code. The idea is that I use the foreach loops to output the data of the array which in turn is used in an INSERT statement, the first array is used to determine which column the data is inserted into and the other is used to provide the data to go into that column. There is the same volume of data in each array being passed in, the pseudo for what I am trying to achieve is as follows: loop 1 (columnarray) loop 2 (dataarray) INSERT INTO <tablename> (columnarray[1]) VALUES (dataarray[1]) INSERT INTO <tablename> (columnarray[2]) VALUES (dataarray[2]) and to end the loop when there are no fields remaining. The code below currently inserts some data into the database but just inserts the data array data into the same field on different rows which is incorrect. If you need any other info from my code please give me a shout, this has been frustrating me for a while now! and any help would be appreciated greatly! Thanks in advance <?php ob_start("ob_gzhandler"); session_start(); require "connect.php";?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <?php $queryfield ="SELECT FieldName FROM ".$_SESSION['Username']."FieldNames"; $resultfield = @mysql_query($queryfield, $connection) or die ("Unable to perform query<br>$queryfield"); $row = mysql_fetch_row($resultfield); foreach($row as $FldNames){ foreach($_POST['FieldName'] as $key => $val) { $data = array('description' => $val['description']); $query = "INSERT INTO ".$_SESSION['Username']." (".$FldNames.") VALUES ('".$val."')"; $result = @mysql_query($query, $connection) or die ("Unable to perform query<br>$query"); } } ?> Link to comment https://forums.phpfreaks.com/topic/198751-looping-2-foreach-loops-and-using-the-results-in-one-mysql-query/ Share on other sites More sharing options...
F1Fan Posted April 16, 2010 Share Posted April 16, 2010 It looks like you are treating the $FldNames variable as a string, when it's probably an array. Link to comment https://forums.phpfreaks.com/topic/198751-looping-2-foreach-loops-and-using-the-results-in-one-mysql-query/#findComment-1043077 Share on other sites More sharing options...
deanlearner Posted April 16, 2010 Share Posted April 16, 2010 $name_array = an array of field names $value_array = an array of values ( in your case $fldNames) <?php $name_array = array('field_1','field_2','field_3','field_4'); $value_array = array('value_1','value_2','value_3','value_4'); $sql = "INSERT INTO `table` ("; foreach($name_array as $name){ $sql .= "`".$name."`, "; } $sql = substr($sql,0,-1).") VALUES ("; foreach($value_array as $value){ $sql .= "`".$name."`, "; } $sql = substr($sql,0,-1).")"; ?> Link to comment https://forums.phpfreaks.com/topic/198751-looping-2-foreach-loops-and-using-the-results-in-one-mysql-query/#findComment-1043083 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 deanlearner, those foreach are the same as implode. Link to comment https://forums.phpfreaks.com/topic/198751-looping-2-foreach-loops-and-using-the-results-in-one-mysql-query/#findComment-1043094 Share on other sites More sharing options...
deanlearner Posted April 16, 2010 Share Posted April 16, 2010 I've not even considered implode before, but looking at it, unless im mistaken you will need to include the ` marks in the field name as implode cannot add them as required. Link to comment https://forums.phpfreaks.com/topic/198751-looping-2-foreach-loops-and-using-the-results-in-one-mysql-query/#findComment-1043108 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 Well, for one, you shouldn't be using the back-ticks at all, but especially not in VALUES. $name_array = array('field_1','field_2','field_3','field_4'); $fields = implode('`,`'$name); if (!empty($fields)) $fields = sprintf('`%s`', $fields); Link to comment https://forums.phpfreaks.com/topic/198751-looping-2-foreach-loops-and-using-the-results-in-one-mysql-query/#findComment-1043114 Share on other sites More sharing options...
ChemicalBliss Posted April 16, 2010 Share Posted April 16, 2010 <?php $name_array = array('field_1','field_2','field_3','field_4'); $value_array = array('value_1','value_2','value_3','value_4'); // He meant like this $sql = "INSERT INTO `table` (`".implode("`,`",$name_array)."`) VALUES ('".implode("','",$value_array)."')"; ?> Backticks are part of the mysql standard? Albeit you shouldnt need them, but not that you shouldnt use them. Link to comment https://forums.phpfreaks.com/topic/198751-looping-2-foreach-loops-and-using-the-results-in-one-mysql-query/#findComment-1043116 Share on other sites More sharing options...
deanlearner Posted April 16, 2010 Share Posted April 16, 2010 woops, unless of course you do "INSERT INTO `table (`".implode('`,`',$name_array)."`) VALUES ('".implode("','",$value_array)."')"; which is nice Oops and yeah the back-ticks in the value array was an error on my part Link to comment https://forums.phpfreaks.com/topic/198751-looping-2-foreach-loops-and-using-the-results-in-one-mysql-query/#findComment-1043117 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 Backticks are part of the mysql standard? Albeit you shouldnt need them, but not that you shouldnt use them. It's not really a standard per-say. Using them just allows you to use ridiculous table/column names like `h e l l 0` or `select` (which is a MySQL reserved keyword). Not recommended to name your tables like that. Link to comment https://forums.phpfreaks.com/topic/198751-looping-2-foreach-loops-and-using-the-results-in-one-mysql-query/#findComment-1043126 Share on other sites More sharing options...
PMacca_1987 Posted April 17, 2010 Author Share Posted April 17, 2010 Hello again, Im still working through some of the resolutions here but I thought I would clear up the backticks conversation for you or at least tell you where they came from! I completed the action that I required in php myadmin and then clicked the create PHP button, This gave the code that was shown on the page Thats right guys.. I cheated! Thanks for the help, really appreciate it. Link to comment https://forums.phpfreaks.com/topic/198751-looping-2-foreach-loops-and-using-the-results-in-one-mysql-query/#findComment-1043545 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.