billy_111 Posted August 8, 2010 Author Share Posted August 8, 2010 Perfect! We got there in the end! Thanks again Link to comment https://forums.phpfreaks.com/topic/210052-why-wont-this-for-loop-insert/page/2/#findComment-1096733 Share on other sites More sharing options...
billy_111 Posted August 9, 2010 Author Share Posted August 9, 2010 Sorry to post again but i found another issue with this :-\ The method only works if an author exists. So if i enter just NEW authors the code does not work, therefore does not insert.. However, if i enter an existing author and then enter NEW authors the code works.. Any ideas what the problem might be? Thanks Link to comment https://forums.phpfreaks.com/topic/210052-why-wont-this-for-loop-insert/page/2/#findComment-1096925 Share on other sites More sharing options...
wildteen88 Posted August 9, 2010 Share Posted August 9, 2010 Change echo "Generated SQL Query:<pre>$sql</pre><hr />"; $result = mysql_query($sql); // If there are Authors left within the $authArray // Add them to the Pname table if(count($authArray) > 0) { // call insertPersons method for remaining authors $this->insertPersons($authArray); // now we call this method again and insert the remaining auhtors into PeopleCon $this->insertAuthor($authArray, $PCorder); } } } } To echo "Generated SQL Query:<pre>$sql</pre><hr />"; $result = mysql_query($sql); } // If there are Authors left within the $authArray // Add them to the Pname table if(count($authArray) > 0) { // call insertPersons method for remaining authors $this->insertPersons($authArray); // now we call this method again and insert the remaining auhtors into PeopleCon $this->insertAuthor($authArray, $PCorder); } } } Link to comment https://forums.phpfreaks.com/topic/210052-why-wont-this-for-loop-insert/page/2/#findComment-1097074 Share on other sites More sharing options...
billy_111 Posted August 9, 2010 Author Share Posted August 9, 2010 Hi, That's worked. I made some amendments and managed not to mess the function up This is the code: public function insertAuthor($authArray, $PCorder=0) { $query = sprintf('SELECT Pid, Pname FROM People WHERE Pname IN(\'%s\') ORDER BY Pid ASC', implode('\',\'', $authArray)); $result = mysql_query($query); $maxquery = "SELECT MAX(CPid) as max FROM ConfPaper WHERE CPRid = ".$_GET['CPRid']; $maxresult = mysql_query($maxquery); $max = mysql_fetch_array($maxresult); $CPid = $max['max']; if($result && mysql_num_rows($result) > 0) { $sqlValues = array(); while(list($PId, $PName) = mysql_fetch_row($result)) { if(in_array($PName, $authArray)) { $sqlValues[] = sprintf("(%d, 1, ".$CPid.", %d, now(), 0)", $PId , $PCorder++ ); // Author already exists within the Pname table // remove user from $authArray $key = array_search($PName, $authArray); unset($authArray[$key]); } } $sql = "INSERT INTO PeopleCon(Person_id, PCHid, Pid, PCorder, PCdateadded, PCdeleted) VALUES \n"; $sql .= implode(",\n", $sqlValues); $result = mysql_query($sql); } // If there are Authors left within the $authArray // Add them to the Pname table if(count($authArray) > 0) { $this->insertPersons($authArray); // call insertPersons method for remaining authors $this->insertAuthor($authArray, $PCorder); // insert the remaining auhtors into PeopleCon } } Just want to thank you for your help to get this working. I will now try to work out a method so i can edit/update authors, will post a new thread if i get stuck anywhere.. By the way, feel free to criticize the new bits of code i have added just in case i'm doing something the wrong way :-\ Thanks Regards Link to comment https://forums.phpfreaks.com/topic/210052-why-wont-this-for-loop-insert/page/2/#findComment-1097156 Share on other sites More sharing options...
billy_111 Posted August 16, 2010 Author Share Posted August 16, 2010 Hey, I'm trying to do the upload method, see this code: public function updateAuthor($authArray, $PCorder=0) { $query = sprintf('SELECT Pid, Pname FROM People WHERE Pname IN(\'%s\') ORDER BY Pid ASC', implode('\',\'', $authArray)); $result = mysql_query($query); $deletequery = "DELETE t1, t2 FROM People AS t1 LEFT JOIN PeopleCon AS t2 ON t1.Pid = t2.Person_id WHERE t1.Pname IN('".implode('\',\'', $authArray)."')"; $delete = mysql_query($deletequery); if($result && mysql_num_rows($result) > 0) { $sqlValues = array(); while(list($PId, $PName) = mysql_fetch_row($result)) { if(in_array($PName, $authArray)) { $sqlValues[] = sprintf("(%d, 1, ".$_GET['ID'].", %d, now(), 0)", $PId , $PCorder++ ); // Author already exists within the Pname table // remove user from $authArray $key = array_search($PName, $authArray); unset($authArray[$key]); } } $sql = "INSERT INTO PeopleCon(Person_id, PCHid, Pid, PCorder, PCdateadded, PCdeleted) VALUES \n"; $sql .= implode(",\n", $sqlValues); $result = mysql_query($sql); } // If there are Authors left within the $authArray // Add them to the Pname table if(count($authArray) > 0) { People::insertPersons($authArray); // call insertPersons method for remaining authors $this->insertAuthor($authArray, $PCorder); // insert the remaining auhtors into PeopleCon } } They way i'm doing it is to delete the existing Authors and then re-insert.. Is this the correct way to do it? It does sort of work, but it doesn't do the re-insert, can you assist? My front end i call the method like this: $p = new Conferences(); $authors = array_filter(array_map('mysql_real_escape_string', $_POST['author'])); $p->updateAuthor($authors); Any ideas what i'm doing wrong? Thanks Link to comment https://forums.phpfreaks.com/topic/210052-why-wont-this-for-loop-insert/page/2/#findComment-1099923 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.