Page 1 of 1

Global array problem?

Posted: Wed Mar 10, 2010 2:13 pm
by mindzee
I thought that books are supposed to help you, but i got stuck on one example and can't figure it out what's the problem here. Ofcourse this is my edited version, but i tried the original one and it is having the same problem.So mayby someone could help out me?
The remove button doesn't remove anything.
It seems that when the code get to test if $_POST['delete'] is set, it isn't set, but if i try to post the form to other page it get's the $_POST['delete'], so i'm thinking that it's something wrong with the code flow, something needs to change positions mayby :?



Code: Select all

 
<?php    
    require_once("login.php"); // prisijungimo kintamieji
    
    //Rysio sukurimas
    $db_connect = mysql_connect($db_hostname, $db_username, $db_password); 
    
    if (!$db_connect) {
        die("Nepavyko sukurti rysio." . mysql_error());
    }
    // Prisijungimas prie duomenu bazes
    mysql_select_db($db_database, $db_connect)
        or die("Nepavyko prisijungti prie duomenu bazes" . mysql_error());
?>
<html>
    <head>
        <title>Testas</title>
    </head>
    <body>
        <?php
            if (isset($_POST['author']) && isset($_POST['title']) &&
                isset($_POST['category']) && isset($_POST['year'])  && isset($_POST['isbn'])) {    
                
                $author = $_POST['author'];
                $title = $_POST['title'];
                $category = $_POST['category'];
                $year = $_POST['year'];
                $isbn = $_POST['isbn'];
                
                if (isset($_POST['delete']) && $isbn != "") { 
                    $db_query = "DELETE FROM classics WHERE isbn='$isbn'";
                    
                    if (!mysql_query($db_query, $db_connect)) {
                        echo "DELETE failed: $db_query<br />" . mysql_error();
                        echo "<br /><br />";
                    }
                } else {
                    $db_query = "INSERT INTO classics VALUES" .
                    "('$author', '$title', '$category', '$year', '$isbn')";
                    
                    if (!mysql_query($db_query, $db_connect)) {
                        echo "INSERT failed: $db_query<br />" . mysql_error();
                        echo "<br /><br />";
                    }
                }
            }
            
        ?>
        <form action="db_test.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>
        <?php
            // Uzklausos suformavimas ir siuntimas
            $db_query = "SELECT * FROM classics";
            $q_result = mysql_query($db_query);
            
            if (!$q_result) {
                die("Nepavyko nuskaityti duomenu." . mysql_error());
            }
            
            $rows = mysql_num_rows($q_result);
            
            for ($i = 0; $i < $rows; $i++) {
                $row = mysql_fetch_row($q_result);
                echo  "<pre><br />
  Author: $row[0]
   Title: $row[1]
Category: $row[2]
    Year: $row[3]
    ISBN: $row[4]";
                     
                echo "<form action='db_test.php' method='post'>";
                echo "<input type='hidden' name='delete' value='yes' />";
                echo "<input type='hidden' name='isbn' value='$row[4]' />";
                echo "<input type='submit' value='Delete Record' />";
                echo " </pre></form>";
            }
        ?>
    </body>
</html>
 
<?php
// Rysio uzdarymas
    mysql_close($db_connect);
?>
 

Re: Global array problem?

Posted: Wed Mar 10, 2010 4:32 pm
by requinix

Code: Select all

<?php
if (isset($_POST['author']) && isset($_POST['title']) &&
    isset($_POST['category']) && isset($_POST['year'])  && isset($_POST['isbn'])) {    
 
 
    if (isset($_POST['delete']) && $isbn != "") {
        $db_query = "DELETE FROM classics WHERE isbn='$isbn'";
To delete, the form must have given the author, title, category, year, and ISBN.

Code: Select all

echo "<form action='db_test.php' method='post'>";
echo "<input type='hidden' name='delete' value='yes' />";
echo "<input type='hidden' name='isbn' value='$row[4]' />";
echo "<input type='submit' value='Delete Record' />";
echo " </pre></form>";
Yours does not.