Visualizzazione dei risultati da 1 a 6 su 6

Discussione: Gestione categorie

  1. #1

    Gestione categorie

    Salve salvino!

    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:

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

    codice:
    <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 '
    
    New category added</p>';
       } else {
       echo '
    
    Error adding new category: ' .
       mysql_error() . '</p>';
    }
    ?>
    
    
    Add another category</p>
    
    
    Return to categoty</p>
    <?php else: ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    
    
    Enter the new category:</p>
    <label>Name: <input type="text" name="name" /></label>
    
    <input type="submit" value="SUBMIT" />
    </form>
    <?php endif; ?>
    editcat.php

    codice:
    <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 '
    
    Category details updated.</p>';
       } else {
       echo '
    
    Edrror updating category details: ' .
       mysql_error() . '</p>';
       }
    ?>
    
    
    Return to category list</p>
    <?php else:
    $id = $_GET['id'];
    $cat = @mysql_query("SELECT name FROM category WHERE id='$id'");
    if (!$cat) {
    exit('
    
    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">
    
    
    Modifica la categoria:</p>
    <label>Nuovo nome: <input type="text" name="name" value="<?php echo $name; ?>" /></label>
    
    <input type="hidden" name="id" value="<?php echo $id; ?>" />
    <input type="submit" value="SUBMIT" /></p>
    </form>
    <?php endif; ?>
    e infine deletecat.php:

    codice:
    <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 '
    
    Error deleting category from database! 
    ';
       'Error: ' . mysql_error() . '</p>';
    }
    ?>
    
    
    Return to category list</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!

  2. #2
    Moderatore di Server Apache L'avatar di marketto
    Registrato dal
    Sep 2001
    Messaggi
    5,858
    Cotrolla il codice html della pagina cats.php, ovvero guarda se l'id viene stampato e passato tramite GET alle altre pagine quando esegui il link.
    think simple think ringo

  3. #3
    Originariamente inviato da marketto
    Cotrolla il codice html della pagina cats.php, ovvero guarda se l'id viene stampato e passato tramite GET alle altre pagine quando esegui il link.
    Cioè? Non sono molto pratica...
    tutto il codice della pagina cats.php è questo:

    codice:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtmll-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
    <title>Categorie</title>
    </head>
    <body>
    <h1>Gestione Categorie</h1>
    <ul>
    <?php
    include('include/database.php');
    $cats = @mysql_query('SELECT id, name FROM category');
    if (!$cats){
        exit ('
    
    Error retrieving categories from database!
    '. 'Error: ' . mysql_error(). '</p>');
    }
    while ($cat = mysql_fetch_array($cats)) {
        $id = $cat['id'];
     $name = htmlspecialchars($cat['name']);
     echo "[*]$name ".
          "Edit| ".
       "Delete";
    }
    ?>[/list]
    
    
    Add a new category</p>
    
    
    Return to home page</p>
    </body>
    </html>
    Cmq il problema ora è solo per cancellare, le modifiche le fa.
    In deletecat.php

    codice:
    <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 '
    
    Error deleting category from database! 
    ';
       'Error: ' . mysql_error() . '</p>';
    }
    ?>
    
    
    Return to category list</p>
    mi dice sempre che id (nella riga in grassetto) è un Undefined index

  4. #4
    Moderatore di Server Apache L'avatar di marketto
    Registrato dal
    Sep 2001
    Messaggi
    5,858
    devi modificare il codice con:
    codice:
    "Delete";
    think simple think ringo

  5. #5
    Originariamente inviato da marketto
    devi modificare il codice con:
    codice:
    "Delete";
    cavolo l'ho riguardato 100 volte e mi era sfuggito!

    GRAZIE!

  6. #6
    Ehm... posso continuare qui anche se non è per le categorie? Più o meno è la stessa cosa, invece delle categorie è per la gestione delle immagini...

    Cooomunque... devo poter caricare delle immagini alle quali assegnare categoria e autore (gestione categoria e autori già fatta e funzionante).

    newimage.php
    codice:
     <?php
    include("include/database.php");
    
    if (isset($_POST['immagine'])): 
    
      $aid = $_POST['aid'];
      $immagine = $_POST['immagine'];
      $cats = $_POST['cats'];
    
      if ($aid == '') {
        exit('
    
    You must choose an author for this image. Click "Back" and try again.</p>');
      }
    
      $sql = "INSERT INTO immagini SET
          immagine='$immagine',
          authorid='$aid'";
      if (mysql_query($sql)) {
        echo '
    
    New image added</p>';
      } else {
        exit('
    
    Error adding new image: ' . mysql_error() . '</p>');
      }
    
      $jid = mysql_insert_id();
    
      if (isset($_POST['cats'])) {
        $cats = $_POST['cats'];
      } else {
        $cats = array();
      }
    
      $numCats = 0;
      foreach ($cats as $catID) {
        $sql = "INSERT IGNORE INTO photocategory
                SET imgid=$jid, categoryid=$catID";
        $ok = mysql_query($sql);
        if ($ok) {
          $numCats = $numCats + 1;
        } else {
          echo "
    
    Error inserting image into category $catID: " .
              mysql_error() . '</p>';
        }
      }
    ?>
    
    
    
    Image was added to <?php echo $numCats; ?> categories.</p>
    
    
    
    Add another image</p>
    
    
    Return to image search</p>
    
    <?php
    else: // Allow the user to enter a new image
    
      $authors = mysql_query('SELECT id, name FROM author');
      if (!$authors) {
        exit('
    
    Unable to obtain author list from the database.</p>');
      }
    
      $cats = mysql_query('SELECT id, name FROM category');
      if (!$cats) {
        exit('
    
    Unable to obtain category list from the database.</p>');
      }
    ?>
    
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    
    
    Enter the new image:
    
    <form enctype="multipart/form-data" action="".$_SERVER['PHP_SELF']."" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="300000" />
    <input type="file" name="immagine" size="40" />
    
    
    
    
    Author:
    <select name="aid" size="1">
      <option selected value="">Select One</option>
      <option value="">---------</option>
    <?php
      while ($author = mysql_fetch_array($authors)) {
        $aid = $author['id'];
        $aname = htmlspecialchars($author['name']);
        echo "<option value='$aid'>$aname</option>\n";
      }
    ?>
    </select></p>
    
    
    Place in categories:
    
    <?php
      while ($cat = mysql_fetch_array($cats)) {
        $cid = $cat['id'];
        $cname = htmlspecialchars($cat['name']);
        echo "<label><input type='checkbox' name='cats[]' value='$cid' />$cname</label>
    \n";
      }
    ?>
    </p>
    <input type="submit" value="SUBMIT" />
    </form>
    <?php endif; ?>
    imglist.php
    codice:
    <?php
    include("include/database.php");
    
    // The basic SELECT statement
    $select = 'SELECT DISTINCT id, immagine';
    $from   = ' FROM immagini';
    $where  = ' WHERE 1=1';
    
    $aid = $_POST['aid'];  line 21
    if ($aid != '') { // An author is selected
      $where .= " AND authorid='$aid'";
    }
    
    $cid = $_POST['cid']; line 26
    if ($cid != '') { // A category is selected
      $from  .= ', photocategory';
      $where .= " AND id=imgid AND categoryid='$cid'";
    }
    ?>
    
    
    
    Immagini:
    <?php
    $imms = mysql_query($select . $from . $where);
    if (!$imms) {
      echo '</p>';
      exit('
    
    Error retrieving images from database!
    '.
          'Error: ' . mysql_error() . '</p>');
    }
    
    while ($imm = mysql_fetch_array($imms)) {
      echo "</p>\n";
      $id = $imm['id'];
      echo "$imm";
      echo "Edit | " .
          "Delete";
      echo "</p>\n";
    }
    ?>
    </p>
    
    
    New search</p>
    imgsearch.php
    codice:
    
    Insert New Image</p>
    <?php
    include("include/database.php");
    
    $authors = @mysql_query('SELECT id, name FROM author');
    if (!$authors) {
      exit('
    
    Unable to obtain author list from the database.</p>');
    }
    
    $cats = @mysql_query('SELECT id, name FROM category');
    if (!$cats) {
      exit('
    
    Unable to obtain category list from the database.</p>');
    }
    ?>
    
    <form action="imglist.php" method="post">
    
    
    View images satisfying the following criteria:</p>
    <label>By author:
    <select name="aid" size="1">
      <option selected value="">Any Author</option>
    <?php
    while ($author = mysql_fetch_array($authors)) {
      $aid = $author['id'];
      $aname = htmlspecialchars($author['name']);
      echo "<option value='$aid'>$aname</option>\n"; 
    }
    ?>
    </select></label>
    
    <label>By category:
    <select name="cid" size="1">
      <option selected value="">Any Category</option>
    <?php
    while ($cat = mysql_fetch_array($cats)) {
      $cid = $cat['id'];
      $cname = htmlspecialchars($cat['name']);
      echo "<option value='$cid'>$cname</option>\n"; 
    }
    ?>
    </select></label>
    
    <input type="submit" value="Search" />
    </form>
    
    
    Return to home page</p>
    Allora, gli errori sono:

    Notice: undefined index: aid in imglist.php on line 21
    Notice: undefined index: cid in imglist.php on line 26

    e poi quando eseguo la pagina imglist.php mi da la lista delle immagini ma NON stampa l'immagine, scrive:

    Array Edit | Delete

    Sicuramente ci sono degli errori perchè era un codice per dei files di testo e io l'ho riadattato per le immagini... ma non so davvero cos'altro fare!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.