4

New to PHP and reading through Robin Nixon's PHP, MySQL, Javascript book. I am having trouble with an example of inserting and deleting data using a PHP script, specifically with how the author uses $_POST.

The example is a pretty simple add records/delete records of books with multiple inputs. Here's 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 refer to an element in $_POST with if (isset($_POST['delete']) && isset($_POST['isbn'])), where delete and isbn are used as names multiple times, how does $_POST know which element to reference to delete? I assume that since you can only delete one record at a time, the element in the array will automatically point to the one that's already set. However, how does the second condition of isset($_POST['isbn']) know which "isbn" element to check for? Does the && make the $_POST['isbn'] "inherit" the correct row?

Thanks for the help! And apologies for any possible misuse of the vocab. Still getting used to everything.

Ryan Kim
  • 51
  • 3
  • 2
    Off topic comment: you're reading an outdated book. [`mysql_*` functions should not be used anymore](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php#answer-12860140). – Joseph Silber Jan 27 '13 at 06:00
  • 2
    On a positive note: If you are a novice PHP programmer & knew to ask a question about the coding of this sample in a “professional” book you are already way ahead of most people I know programming. Good work! Just find another book. Seriously. – Giacomo1968 Jan 27 '13 at 06:10

2 Answers2

4

Your question is actually well thought out. And the example given in the book seems quite sloppy to me. I am assuming in later chapters he will delve into the use of 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 that <form action="sqltest.php" method="post">? And see that closing </form>? And note that they are being rendered each time the for ($j = 0 ; $j < $rows ; ++$j) loop happens? There is one individual form element for each line. That is messy code, but it works. When one clicks submit on each individual listing, the wrapping form responds & parses the variables nested inside it.

Like I said, sloppy code. But works. And it’s because if you have 30 ISBNs listed this program will spit out 30 individually wrapped <form> items. Uggh! Seriously if the book does not address arrays later on in a way that addresses this face-palm of a coding mess find a new book.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • Wow... totally missed that. A lot of the examples in this book seem a little convoluted. Thanks for the help! – Ryan Kim Jan 27 '13 at 06:14
  • 2
    No problem. Also, [I looked up the book on Amazon](http://www.amazon.com/Learning-PHP-MySQL-JavaScript-Step-By-Step/dp/0596157134). It’s been a while since I checked out any book on programming anything since I feel Google searches & places like StackOverflow are better sources of info. And I have been doing PHP for 10-ish years. But man, I am shocked at how many positive reviews there are of this book. The handful of negative reviews focus on issues like the ones brought up here. But wow, a solid 4 stars for sloppy coding like this? Wow. – Giacomo1968 Jan 27 '13 at 06:17
  • Yeah, maybe it was a bad choice. I've also been working on a small project and using this site among others to fill me in when I get stuck. Hopefully I'll get it right. Please let me know if you have any recs! – Ryan Kim Jan 27 '13 at 06:21
  • @RyanKim, my best advice to you is to do some searches for handling arrays in `$_POST` values & maybe cleaning up this sloppy code by reworking the logic into arrays and using `mysqli_` as the preferred connection method. Good luck! – Giacomo1968 Jan 27 '13 at 06:38
1

Since there are multiple forms, the input elements of only one form are submitted.

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

You can check this out by using print_r($_POST) in sqltest.php.

ShuklaSannidhya
  • 8,572
  • 9
  • 32
  • 45