Gestione categorie

bimbina82

Nuovo Utente
27 Mag 2007
10
0
0
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 ('<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:

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 '<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

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 '<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:

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 '<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!

:confused:

HELP!
 
Togli la @ davanti a tutte le funzioni per le query e aggiungi mysql_error
 
Ora riesco a modificare, ma ancora per cancellare mi da problemi!

deletecat.php
Codice:
<title>Cancella categorie</title>
</head>
<body>
<?php
include('include/database.php');
[B]$id = $_GET['id'];[/B]
$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>

Alla riga in grassetto mi dice che id è un Undefined index, ma perchèèèè????
 
L'errore è in cats.php:

PHP:
"<a href='deletecat.php?=$id'>Delete</a>";

invece di

PHP:
"<a href='deletecat.php?id=$id'>Delete</a>";
 
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('<p>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 '<p>New image added</p>';
  } else {
    exit('<p>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 "<p>Error inserting image into category $catID: " .
          mysql_error() . '</p>';
    }
  }
?>

<p>Image was added to <?php echo $numCats; ?> categories.</p>

<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add another image</a></p>
<p><a href="imgsearch.php">Return to image search</a></p>

<?php
else: // Allow the user to enter a new image

  $authors = mysql_query('SELECT id, name FROM author');
  if (!$authors) {
    exit('<p>Unable to obtain author list from the database.</p>');
  }

  $cats = mysql_query('SELECT id, name FROM category');
  if (!$cats) {
    exit('<p>Unable to obtain category list from the database.</p>');
  }
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Enter the new image:<br />
<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" /><br /><br />
<p>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>
<p>Place in categories:<br />
<?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><br />\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';

[B]$aid = $_POST['aid'];[/B]  [COLOR="Red"]line 21[/COLOR]
if ($aid != '') { // An author is selected
  $where .= " AND authorid='$aid'";
}

[B]$cid = $_POST['cid'];[/B] [COLOR="red"]line 26[/COLOR]
if ($cid != '') { // A category is selected
  $from  .= ', photocategory';
  $where .= " AND id=imgid AND categoryid='$cid'";
}
?>

<p>Immagini:
<?php
$imms = mysql_query($select . $from . $where);
if (!$imms) {
  echo '</p>';
  exit('<p>Error retrieving images from database!<br />'.
      'Error: ' . mysql_error() . '</p>');
}

while ($imm = mysql_fetch_array($imms)) {
  echo "</p>\n";
  $id = $imm['id'];
  echo "$imm";
  echo "<a href='editimg.php?id=$id'>Edit</a> | " .
      "<a href='deleteimg.php?id=$id'>Delete</a>";
  echo "</p>\n";
}
?>
</p>
<p><a href="imgsearch.php">New search</a></p>

imgsearch.php
Codice:
<p><a href="newimage.php">Insert New Image</a></p>
<?php
include("include/database.php");

$authors = @mysql_query('SELECT id, name FROM author');
if (!$authors) {
  exit('<p>Unable to obtain author list from the database.</p>');
}

$cats = @mysql_query('SELECT id, name FROM category');
if (!$cats) {
  exit('<p>Unable to obtain category list from the database.</p>');
}
?>

<form action="imglist.php" method="post">
<p>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><br />
<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><br />
<input type="submit" value="Search" />
</form>
<p><a href="index.php">Return to home page</a></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!
 
Controlla l'output html delle righe:

PHP:
echo "<option value='$aid'>$aname</option>\n";

e

PHP:
echo "<label><input type='checkbox' name='cats[]' value='$cid' />$cname</label><br />\n";

I due value vengono stampati?
 
Controlla l'output html delle righe:

PHP:
echo "<option value='$aid'>$aname</option>\n";

e

PHP:
echo "<label><input type='checkbox' name='cats[]' value='$cid' />$cname</label><br />\n";

I due value vengono stampati?

si si, questi vengono stampati!

In realtà con autori e categorie non ho problemi, il problema è solo relazionarli alle immagini :rolleyes:
 
Ho provato a fare così ma mi sa che ho fatto un bel casino :(

Codice:
<?
if (isset($_FILES['file']))
{
function upload()
{
  $result = false;
  $immagine = '';
  $size = 0;
  $type = '';
  $nome = '';
  $tit = $_POST['titolo'];
  $alt = $_POST['alt'];
  $descr = $_POST['descrizione'];

  $max_size = 300000;

  $result = is_uploaded_file($_FILES['file']['tmp_name']);
  if (!$result)
  {
    echo "Impossibile eseguire l'upload.";
    return false;
  }else{
    $size = $_FILES['file']['size'];
    if ($size > $max_size)
    {
      echo "Il file è troppo grande.";
      return false;
    }
      }
	
    $type = $_FILES['file']['type'];
    $nome = $_FILES['file']['name'];
	
	$aid = $_POST['aid'];
      $cid = $_POST['cid'];
	  
	  if ($aid == '') {
         exit('<p>You must choose an author for this image. Click "Back" and try again.</p>');

    $immagine = @file_get_contents($_FILES['file']['tmp_name']);
    $immagine = addslashes ($immagine);
    @include 'include/database.php';
    $sql = "INSERT INTO immagini (nome, size, type, immagine, titolo, descrizione, alt, authorid) VALUES ('$nome','$size','$type','$immagine', '$tit',  '$descr', '$alt', '$aid')";
    $result = @mysql_query ($sql) or die (mysql_error());
    return true;
  }
  
  $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 "<p>Error inserting image into category $catID: " .
          mysql_error() . '</p>';
    }
  }
  

else{ // Allow the user to enter a new image

  $authors = mysql_query('SELECT id, name FROM author');
  if (!$authors) {
    exit('<p>Unable to obtain author list from the database.</p>');
  }

  $cats = mysql_query('SELECT id, name FROM category');
  if (!$cats) {
    exit('<p>Unable to obtain category list from the database.</p>');
  }
    }
  ?>

<p>Image was added to <?php echo $numCats; ?> categories.</p>

<h3>Upload</h3>

<form enctype=\"multipart/form-data\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">
<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"300000\" />
<input type=\"file\" name=\"file\" size=\"40\" /><br /><br />
Titolo: <textarea name=\"titolo\"></textarea><br />
Descrizione: <textarea name=\"descrizione\"></textarea><br />
Alt: <input name=\"alt\" type=\"text\">

<p>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>
<p>Place in categories:<br />
<?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><br />\n";
  }
?>
</p>

<input type="submit" value="Invia" /></form>
<?php endif; ?>

<br /><a href="link.php">Elenco</a>
<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add another image</a></p>
<p><a href="imgsearch.php">Return to image search</a></p>

Non ci capisco più niente :dipser:
 
mi posti per cortesia un sql della struttura del tuo db?

Database: museum

CREATE TABLE users (
username varchar(30) NOT NULL,
password varchar(32) NOT NULL,
userid varchar(32),
userlevel tinyint(1) UNSIGNED NOT NULL,
email varchar(50) NOT NULL,
timestamp int(11) UNSIGNED NOT NULL,
PRIMARY KEY (username),
);

CREATE TABLE active_guest (
ip varchar(15) NOT NULL,
timestamp int(11) UNSIGNED NOT NULL,
PRIMARY KEY (ip),
);

CREATE TABLE active_users (
ip varchar(15) NOT NULL,
timestamp int(11) UNSIGNED NOT NULL,
PRIMARY KEY (ip),
);

CREATE TABLE banned_users (
username varchar(30) NOT NULL,
timestamp int(11) UNSIGNED NOT NULL,
PRIMARY KEY (username),
);

CREATE TABLE author (
id int(11) NOT NULL auto_increment,
name varchar(50) NOT NULL,
email varchar(50) NOT NULL,
PRIMARY KEY (id),
);

CREATE TABLE category (
id int(11) NOT NULL auto_increment,
name varchar(50) NOT NULL,
PRIMARY KEY (id),
);

CREATE TABLE immagini (
id int(11) NOT NULL auto_increment,
nome varchar(50) NOT NULL,
size varchar(25) NOT NULL,
type varchar (25) NOT NULL,
immagine blob NOT NULL,
titolo varchar(100) NOT NULL,
descrizione varchar(100) NOT NULL,
alt varchar(50) NOT NULL,
authorid int(11) NOT NULL,
categoryid int(11) NOT NULL,
PRIMARY KEY (id),
);

CREATE TABLE photocategory (
categoryid int (11) NOT NULL,
imgid int(11) NOT NULL,
PRIMARY KEY (categoryid, imgid),
);
 

Discussioni simili