You can click on the Author link to add multiple authors, what i am trying to do is echo out each author, but with the code i have posted above it doesn’t work, it returns nothing.
The above page is merely an example, to show you what i am trying to do. I fire this code on button click. what am i doing wrong?
So moving on, my first step is to try and get this code working using the array, i am trying to execute this code:
class People {
public function insertAuthor(){
$authArray = $_POST['author'];
$PCorder = 0;
foreach($authArray as &$author):
//Check if Pname exists
$query = "SELECT * FROM People WHERE Pname = '".$author."'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0):
$row = mysql_fetch_array($result);
$Pid = $row['Pid'];
$sql = "INSERT INTO PeopleCon(Pid, PCorder, PCdateadded, PCdeleted) VALUES (".$Pid.", ".$PCorder++.", now(), 0)";
//die($sql);
$result = mysql_query($sql);
endif;
endforeach;
}
}
So basically for EVERY author that is added i want to check if the name exists, if so INSERT into PeopleCon. This code does not throw back any errors, but the INSERT does not work…
Post elements are allowed to be arrays using the syntax
<input name="author[]" ... >
You can even be specific if you need to preserve the key that goes there…
<input name="author[<?= $i ?>]" ... >
Having iterators be part of a variable’s name (i.e. $author1, $author2, $author3 … or even $array[‘element1’], $array[‘element2’] … ) is INCREDIBLY BAD PRACTICE (I’m frankly surprised no one in this thread has commented on this either).
Build the array right and you’ll be able to iterate over it as intended.
Well, if on the top line of your postback handler the var_dump( $_POST ); is outputting nothing, then the values are not even getting through - there must be something wrong with your html form.