RON_ron Posted November 27, 2012 Share Posted November 27, 2012 I want to send 25 students records in to the mySQL in one go. I used a for loop to do this. This is what I've tried so far and no success. Could someone help? require_once("accessLocal.php"); for (i=0; i<25; i++) { $student+$i = mysql_real_escape_string($_POST['student'+$i]); $subjectID+$i = mysql_real_escape_string($_POST['subjectID'+$i]); $query = sprintf("UPDATE studentbackup13 SET code='%s' WHERE name ='%s'", mysql_real_escape_string($subjectID+$i), mysql_real_escape_string($student+$i)); $result = mysql_query($query); } Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/ Share on other sites More sharing options...
Muddy_Funster Posted November 27, 2012 Share Posted November 27, 2012 first of all, you would only run mysql_real_escape_string() once on a variable, not twice as you are doing second of all, mysql_real_escape_string() is, as the name suggests, only any use at sanitising strings, so it's useless against integers. third of all, you should take the +$i out of the square brackets for the $_POST array call finaly this is not an efficent way to do things, you may want to think about a different approach - you should never run queries inside of loops Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395442 Share on other sites More sharing options...
Iluvatar+ Posted November 27, 2012 Share Posted November 27, 2012 ... you should never run queries inside of loops... Why? Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395450 Share on other sites More sharing options...
Muddy_Funster Posted November 27, 2012 Share Posted November 27, 2012 (edited) Because it's a gross and needless waste of resources and possably bandwidth, also in the event of larger more complex queires can result in bans from certain hosting companies (can be percieved as a DoS style attack) not to mention that in certain cases, when multitreading is enabled with a low simultanious connection limit, you can end up filling the connection limit with what should be a single transaction, locking out any other transactions. Edt : Oh, yeah, and there has only ever been a single occasion I have ever come accross where a looped query was required, and this is nowhere close to that. Edited November 27, 2012 by Muddy_Funster Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395454 Share on other sites More sharing options...
PFMaBiSmAd Posted November 27, 2012 Share Posted November 27, 2012 You also need to make your form field an array, so that you can simply iterate over the submitted form data as an array. See this link - http://us3.php.net/manual/en/faq.html.php#faq.html.arrays Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395485 Share on other sites More sharing options...
Iluvatar+ Posted November 27, 2012 Share Posted November 27, 2012 I see, i was just wondering because there has been a case or two were i have resulted in using query in a loop. Do you think you should generate a single query in a loop to execute it once complete? Come to think of it i used recently to update multiple fields on the bases of an array of passed id's. /* Ticket Updates */ public function ChangeCaseStatus($status,$ticketIds) { empty($this->errors); // clear errors for($i=0;$i<count($ticketIds);$i++) { if(!mysql_query('UPDATE '.$this->defaultTable.' SET status="'.$status.'" WHERE id='.$ticketIds[$i])) { $this->errors[$i] = $ticketIds[$i].' [status : '.$status.'] '.mysql_error(); } } return $this->notification(count($ticketIds),'Marked '.$status); } Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395534 Share on other sites More sharing options...
Muddy_Funster Posted November 27, 2012 Share Posted November 27, 2012 that is still running it in a loop, so if you have 50 ticket id's in that array you are hitting the DBS with 50 query requests, when you should be using only 1. As I don't have the first clue what you are doing this for, this is more of an extreme "catch all" solution for this type scenrio : (i'm assuming your working with id numbers here...) $record = array(); for($i=0;$i<25;$i++){ $record[$i] = $_POST['student']+$i.",".$_POST['subject']+$i; } $appendSQL = "VALUES "; $values = implode ('), (', $record); $appendSQL .="({$values})"; $sql = <<<SQL DROP TABLE IF EXISTS temp_table_update_dataset; CREATE TEMPORARY TABLE temp_table_update_dataset (studentID int, subjectID int); INSERT INTO temp_table_update_dataset (studentID, subjectID) {$appendSQL}; UPDATE studentbackup13 INNER JOIN temp_table_update_dataset ON (temp_table_update_dataset.studentID = studentbackup13.name) SET studentbackup13.code = temp_table_update_dataset.subjectID SQL; $performUpdate = mysql_query($sql) or die (mysql_error()); that's just off the top of my head, so it's likely not going to work without some refinement, but it will at least give you the idea of using a single query request rather than looping. Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395567 Share on other sites More sharing options...
Iluvatar+ Posted November 27, 2012 Share Posted November 27, 2012 Cheers dude, ill be making the modifications asp Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395571 Share on other sites More sharing options...
RON_ron Posted November 27, 2012 Author Share Posted November 27, 2012 (edited) is this a good way of doing it? $ar1 = array(1,2,3,4,5); $ar2 = array(9,8,7,6,5); $n=0; foreach ($ar1 as $thing) { $bigar[$n][1] = $thing; $n++; } $n=0; foreach ($ar2 as $thing) { $bigar[$n][2] = $thing; $n++; } foreach ($bigar as $part) { //echo $part[1].$part[2]; $query = sprintf("UPDATE studentbackup13 SET code='%s' WHERE name ='%s'", mysql_real_escape_string($part[1]), mysql_real_escape_string($part[2])); $result = mysql_query($query); } Edited November 27, 2012 by RON_ron Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395640 Share on other sites More sharing options...
RON_ron Posted November 27, 2012 Author Share Posted November 27, 2012 could someone help me out? I'm getting an error for these 3 lines foreach ($ar1 as $thing) { foreach ($ar2 as $thing) { foreach ($bigar as $part) { Warning: Invalid argument supplied for foreach() Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395686 Share on other sites More sharing options...
RON_ron Posted November 27, 2012 Author Share Posted November 27, 2012 in my submit form I have manditory fields and some optional fields. I want to create an array of only all the non empty inputs. Doesn't work. $a1 = array(); for (i=0; i<25; i++) { if ($_POST['dataA']+$i != "") { $a1[] = ['dataA']+$i; $a1[$key] = ['dataA']+$i; } } Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395698 Share on other sites More sharing options...
requinix Posted November 27, 2012 Share Posted November 27, 2012 It's no wonder it doesn't work: 1. No $ on the variable in the for loop 2. You're using addition (+) when you probably mean concatenation (.) 3. You're doing the addition in the wrong place anyways 4. You're trying to add two items to $a1 for every $i 5a. You're probably not using PHP 5.4 5b. If you are you're trying to add a number to an array 6. You pulled $key out of mid air I'm going to guess that your inputs are named "dataA1", "dataA2", and so on. Change that so they're all called "dataA[]". Or if you really want the numbers, "dataA[1]" and "dataA[2]". That way $_POST["dataA"] will be an array - something easier to deal with. As for the code, it's DOA. $a1 = array(); if (isset($_POST["dataA"]) && is_array($_POST["dataA"])) { foreach ($_POST["dataA"] as $key => $value) { if (is_string($value) && $value != "") { $a1[$key] = $value; } } } Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395706 Share on other sites More sharing options...
RON_ron Posted November 27, 2012 Author Share Posted November 27, 2012 (edited) Thank you requinix. I'm getting an error and I dodn't know how to fix it? (Warning: Invalid argument supplied for foreach()) Here's what I'm trying to achieve; 1. DataA This is the students ID and DataB is the student name. 2. In my form there are 25 rows to enter the student IDs and student names. When the submit button is clicked, it brings all the data to this php (below). 3. I want this PHP to be able to search the student name and update the ID accordingly for all the 25 students. //all input data is DataA and DataB. $ar1 = array(); if (isset($_POST['DataA']) && is_array($_POST['DataA'])) { foreach ($_POST['DataA'] as $key => $value) { if (is_string($value) && $value != "") { $ar1[$key] = $value; } } } $ar2 = array(); if (isset($_POST['DataB']) && is_array($_POST['DataB'])) { foreach ($_POST['DataB'] as $keys => $values) { if (is_string($values) && $values != "") { $ar2[$keys] = $values; } } } $n=0; //Warning: Invalid argument supplied for foreach() foreach ($ar1 as $thing) { $bigar[$n][1] = $thing; $n++; } $n=0; //Warning: Invalid argument supplied for foreach() foreach ($ar2 as $thing) { $bigar[$n][2] = $thing; $n++; } foreach ($bigar as $part) { //Warning: Invalid argument supplied for foreach() $query = sprintf("UPDATE db1 SET code='%s' WHERE name ='%s'", mysql_real_escape_string($part[2]), mysql_real_escape_string($part[1])); $result = mysql_query($query); } Edited November 27, 2012 by RON_ron Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395731 Share on other sites More sharing options...
Pikachu2000 Posted November 27, 2012 Share Posted November 27, 2012 When I run the code you posted, it does not produce that error. Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395746 Share on other sites More sharing options...
Pikachu2000 Posted November 27, 2012 Share Posted November 27, 2012 You already have a thread open for this same problem. Stop double posting. Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395747 Share on other sites More sharing options...
requinix Posted November 27, 2012 Share Posted November 27, 2012 (edited) Is that your actual code? Do a var_dump() of $ar1, $ar2, and $bigar before their respective loops and make sure they're arrays. Because somehow they're not, even though I don't see how that's possible with the code you've shown*. * Well yes, $bigar=null. But the other two I don't know. Edited November 27, 2012 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395751 Share on other sites More sharing options...
RON_ron Posted November 27, 2012 Author Share Posted November 27, 2012 (edited) the POST values doesn't seem to be going in to the arrays? Any idea why? $ar1 = array(); if (isset($_POST['DataA']) && is_array($_POST['DataA'])) { foreach ($_POST['DataA'] as $key => $value) { if (is_string($value) && $value != "") { $ar1[$key] = $value; } } } $ar2 = array(); if (isset($_POST['DataB']) && is_array($_POST['DataB'])) { foreach ($_POST['DataB'] as $keys => $values) { if (is_string($values) && $values != "") { $ar2[$keys] = $values; } } } $n=0; foreach ($ar1 as $thing) { $bigar[$n][1] = $thing; $n++; } $n=0; foreach ($ar2 as $thing) { $bigar[$n][2] = $thing; $n++; } foreach ($bigar as $part) { $query = sprintf("UPDATE db1 SET code='%s' WHERE name ='%s'", mysql_real_escape_string($part[2]), mysql_real_escape_string($part[1])); $result = mysql_query($query); } Edited November 27, 2012 by RON_ron Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395752 Share on other sites More sharing options...
RON_ron Posted November 27, 2012 Author Share Posted November 27, 2012 (edited) OUTPUT string(3) "ar1" string(3) "ar2" What does this mean? if (isset($_POST['DataA']) && is_array($_POST['DataA'])) { foreach ($_POST['DataA'] as $key => $value) { if (is_string($value) && $value != "") { $ar1[$key] = $value; } } } echo var_dump(ar1)."<br>"; $ar2 = array(); if (isset($_POST['DataB']) && is_array($_POST['DataB'])) { foreach ($_POST['DataB'] as $keys => $values) { if (is_string($values) && $values != "") { $ar2[$keys] = $values; } } } echo var_dump(ar2)."<br>"; Edited November 27, 2012 by RON_ron Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395757 Share on other sites More sharing options...
Barand Posted November 28, 2012 Share Posted November 28, 2012 OUTPUT string(3) "ar1" string(3) "ar2" What does this mean? You have the word "string" followed by a 3 then a 3 character string value. Now can you work out what it means? Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395760 Share on other sites More sharing options...
RON_ron Posted November 28, 2012 Author Share Posted November 28, 2012 oh shit!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395761 Share on other sites More sharing options...
RON_ron Posted November 28, 2012 Author Share Posted November 28, 2012 (edited) the output shows that those are arrays. array(0) { } array(0) { } Any idea why isn't the code working? Edited November 28, 2012 by RON_ron Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395762 Share on other sites More sharing options...
requinix Posted November 28, 2012 Share Posted November 28, 2012 What does the HTML for the form look like? Did you rename the fields like I said? Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395763 Share on other sites More sharing options...
RON_ron Posted November 28, 2012 Author Share Posted November 28, 2012 yes i did. all which comes to $ar1 are dataA and $ar2 are dataB Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395767 Share on other sites More sharing options...
requinix Posted November 28, 2012 Share Posted November 28, 2012 For kicks, how about posting the HTML for the form? Do a View Source on the page and copy/paste the relevant part(s). Quote Link to comment https://forums.phpfreaks.com/topic/271270-add-none-empty-data-into-the-array/#findComment-1395773 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.