• User Newbie

    Gestione categorie

    :ciauz:

    Sto cercando di creare un sistema per la gestione delle categorie in un sito... solo che ovviamente non funziona 😄 mi aiutate a trovare l'errore?

    Allora, le tabelle nel db sono:

    category:
    id (primary key)
    name

    photocategory:
    categoryid (primary key)
    imgid (primary key)

    poi ho: cats.php:

    
    <h1>Gestione Categorie</h1>
    <ul>
    <?php
    include('include/database.php');
    $cats = @mysql_query('SELECT id, name FROM category');
    if (!$cats){
        exit ('<p>Error retrieving categories from database!<br />'. 'Error: ' . mysql_error(). '</p>');
    }
    while ($cat = mysql_fetch_array($cats)) {
        $id = $cat['id'];
     $name = htmlspecialchars($cat['name']);
     echo "<li>$name ".
          "<a href='editcat.php?id=$id'>Edit</a>| ".
       "<a href='deletecat.php?=$id'>Delete</a>";
    }
    ?>
    </ul>
    <p><a href="newcat.php">Add a new category</a></p>
    <p><a href="index.php">Return to home page</a></p>
    
    

    newcat.php:

    
    <title>Aggiungi Categoria</title>
    </head>
    <body>
    <?php
    if (isset($_POST['name'])):
    include("include/database.php");
    $name = $_POST['name'];
    $sql = "INSERT INTO category SET name='$name'";
    if (@mysql_query($sql)) {
       echo '<p>New category added</p>';
       } else {
       echo '<p>Error adding new category: ' .
       mysql_error() . '</p>';
    }
    ?>
    <p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add another category</a></p>
    <p><a href="cats.php">Return to categoty</a></p>
    <?php else: ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p>Enter the new category:</p>
    <label>Name: <input type="text" name="name" /></label><br />
    <input type="submit" value="SUBMIT" />
    </form>
    <?php endif; ?>
    
    

    editcat.php

    
    <title>Modifica categorie</title>
    </head>
    <body>
    <?php
    include('include/database.php');
     
    if (isset($_POST['name'])):
       $name = $_POST['name'];
       $id = $_POST['id'];
       $sql = "UPDATE category SET name='$name' WHERE id='id'";
       if (@mysql_query($sql)) {
       echo '<p>Category details updated.</p>';
       } else {
       echo '<p>Edrror updating category details: ' .
       mysql_error() . '</p>';
       }
    ?>
    <p><a href="cats.php">Return to category list</a></p>
    <?php else:
    $id = $_GET['id'];
    $cat = @mysql_query("SELECT name FROM category WHERE id='$id'");
    if (!$cat) {
    exit('<p>Error fetching category details: ' . mysql_error() . '</p>');
    }
    $cat = mysql_fetch_array($cat);
    $name = $cat['name'];
    $name = htmlspecialchars($name);
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p>Modifica la categoria:</p>
    <label>Nuovo nome: <input type="text" name="name" value="<?php echo $name; ?>" /></label><br />
    <input type="hidden" name="id" value="<?php echo $id; ?>" />
    <input type="submit" value="SUBMIT" /></p>
    </form>
    <?php endif; ?>
    
    

    e infine deletecat.php:

    
    <title>Cancella categorie</title>
    </head>
    <body>
    <?php
    include('include/database.php');
    $id = $_GET['id'];
    $ok1 = @mysql_query("DELETE FROM photocategory WHERE categoryid='$id'");
    $ok2 = @mysql_query("DELETE FROM category WHERE id='$id'");
    if ($ok1 and $ok2) {
       echo 'Category deleted successfully!';
       }
    else {
       echo '<p>Error deleting category from database! <br />';
       'Error: ' . mysql_error() . '</p>';
    }
    ?>
    <p><a href="cats.php">Return to category list</a></p>
    
    

    Innanzi tutto come errore mi dice

    Notice: Undefined index: id

    sia in deletecat.php che in editcat.php

    e poi cmq nn fa quello che dovrebbe fare! Cioè, aggiunge e basta, ma non edita e non cancella!

    HELP!


  • User Newbie

    :microsoft: