Can $ _POST distinguish between two elements with the same name?

New to PHP and reading through Robin Nixon PHP, MySQL, Javascript book. I am having problems with an example of inserting and deleting data using a PHP script, in particular, with the way the author uses $ _POST.

An example is a fairly simple record / deletion of book entries with multiple inputs. Here is the code:

if (isset($_POST['delete']) && isset($_POST['isbn'])) { $isbn = get_post('isbn'); $query = "DELETE FROM classics WHERE isbn='$isbn'"; if (!mysql_query($query, $db_server)) echo "DELETE failed: $query<br />" . mysql_error() . "<br /><br />"; } if (isset($_POST['author']) && isset($_POST['title']) && isset($_POST['category']) && isset($_POST['year']) && isset($_POST['isbn'])) { $author = get_post('author'); $title = get_post('title'); $category = get_post('category'); $year = get_post('year'); $isbn = get_post('isbn'); $query = "INSERT INTO classics VALUES" . "('$author', '$title', '$category', '$year', '$isbn')"; if (!mysql_query($query, $db_server)) echo "INSERT failed: $query<br />" . mysql_error() . "<br /><br />"; } echo <<<_END <form action="sqltest.php" method="post"><pre> Author <input type="text" name="author" /> Title <input type="text" name="title" /> Category <input type="text" name="category" /> Year <input type="text" name="year" /> ISBN <input type="text" name="isbn" /> <input type="submit" value="ADD RECORD" /> </pre></form> _END; $query = "SELECT * FROM classics"; $result = mysql_query($query); if (!$result) die ("Database access failed: " . mysql_error()); $rows = mysql_num_rows($result); for ($j = 0 ; $j < $rows ; ++$j) { $row = mysql_fetch_row($result); echo <<<_END <pre> Author $row[0] Title $row[1] Category $row[2] Year $row[3] ISBN $row[4] </pre> <form action="sqltest.php" method="post"> <input type="hidden" name="delete" value="yes" /> <input type="hidden" name="isbn" value="$row[4]" /> <input type="submit" value="DELETE RECORD" /></form> _END; } mysql_close($db_server); function get_post($var) { return mysql_real_escape_string($_POST[$var]); } ?> 

When you reference an element in $ _POST with if (isset ($ _ POST ['delete']) && isset ($ _ POST ['isbn'])), where delete and isbn are used as names several times, as $ _POST knows which element refers to the delete? I assume that since you can only delete one entry at a time, the element in the array will automatically point to the one that is already installed. However, how does the second condition isset ($ _ POST ['isbn']) know for which isbn element to check? Does && make $ _POST ['isbn'] "inherit" the correct string?

Thanks for the help! And apologies for any possible abuse of the wokab. Still used to everything.

+4
source share
2 answers

Your question is actually well thought out. And the example given in the book seems to me inaccurate. I assume that in subsequent chapters he will look at using arrays in $_POST values. But anyway, here is the key to the functionality of the whole script:

 for ($j = 0 ; $j < $rows ; ++$j) { $row = mysql_fetch_row($result); echo <<<_END <pre> Author $row[0] Title $row[1] Category $row[2] Year $row[3] ISBN $row[4] </pre> <form action="sqltest.php" method="post"> <input type="hidden" name="delete" value="yes" /> <input type="hidden" name="isbn" value="$row[4]" /> <input type="submit" value="DELETE RECORD" /></form> _END; } 

See what <form action="sqltest.php" method="post"> ? And do you see this closure </form> ? And note that they are displayed every time a for ($j = 0 ; $j < $rows ; ++$j) loop occurs for ($j = 0 ; $j < $rows ; ++$j) ? For each line there is one individual element of the form. This is dirty code, but it works. When one click on submit in each individual listing, the packaging form responds and analyzes the variables enclosed inside it.

As I said, sloppy code. But it works. And this is because if you have 30 ISBNs listed in this program, 30 individual wrapped <form> elements will spit out. Uggh! Seriously, if the book will not address arrays later, so to access this palm of code encoding, find a new book.

+4
source

Since there are several forms, input elements of only one form are entered.

Basically, sqltest.php receives only one $_POST array containing ['delete'] and ['isbn'] with the corresponding values ​​only once.

You can verify this using print_r($_POST) in sqltest.php .

+1
source

All Articles