30. januar 2006 - 10:03Der er
15 kommentarer og 1 løsning
'Edit' og 'Delete' funktioner på liste.
Hejsa,
Har oprettet følgende tabel i min database 'dvd':
| dvds | CREATE TABLE `dvds` ( `lend_to_username` varchar(50) NOT NULL default '', `date_created` datetime NOT NULL default '0000-00-00 00:00:00', `rating` tinyint(3) unsigned NOT NULL default '0', `version` varchar(30) NOT NULL default '', `region` tinyint(3) unsigned NOT NULL default '0', `sound_format` varchar(30) NOT NULL default '', `year_produced` smallint(4) unsigned NOT NULL default '0', `genre` varchar(30) NOT NULL default '', `title` varchar(100) NOT NULL default '', `catalogue_number` varchar(40) NOT NULL default '', PRIMARY KEY (`catalogue_number`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
Jeg har fået bikset nedenstående kode sammen, som lister data. Jeg mangler at få føjet en 'Edit' og en 'Delete' funktion på hver af de viste linier. Ved valg af 'Edit' skal den ønskede data åbnes i en form, så jeg kan tilrette felterne, og trykke Save eller Cancel. Ved valg af 'Delete' ønsker jeg at der promptes for, om man er sikker på sletning, og hvis ja skal der slettes fra tabellen 'dvds':
<!-- START PÅ KODE // Get all the data from the "dvds" table $result = mysql_query("SELECT title, genre, rating, year_produced, version, sound_format, region from dvds ORDER BY title") or die(mysql_error());
echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\">"; echo "<tr> <th class=\"listheading\">title</th> <th class=\"listheading\">genre</th> <th class=\"listheading\">Rating</th> <th class=\"listheading\">Year</th> <th class=\"listheading\">Version</th> <th class=\"listheading\">Sound</th> <th class=\"listheading\">Region</th> </tr>"; // keeps getting the next row until there are no more to get $class = false; while($row = mysql_fetch_array( $result )) { if($class == "odd") { $class = "even"; } else { $class = "odd"; } // Print out the contents of each row into a table echo "<tr><td class=\"tablelist".$class."\">"; echo $row['title']; echo "</td><td class=\"tablelist".$class."\">"; echo $row['genre']; echo "</td><td class=\"tablelist".$class."\">"; echo ("<img src=\"./graphic/".$row['rating'].".gif\" alt=\"\">\n"); echo "</td><td class=\"tablelist".$class."\">"; echo $row['year_produced']; echo "</td><td class=\"tablelist".$class."\">"; echo $row['version']; echo "</td><td class=\"tablelist".$class."\">"; echo $row['sound_format']; echo "</td><td class=\"tablelist".$class."\">"; echo $row['region']; echo "</td></tr>"; } echo "</table>"; SLUT PÅ KODE -->
<tr> <td width="100" class="formcaption">Title</td> <td><input name="title" type="text" id=""></td> </tr> <tr> <td width="100" class="formcaption">Genre</td> <td> <select name="genre" id=""> <option value ="Action">Action</option> <option value ="Adventure">Adventure</option> <option value ="Animation">Animation</option> <option value ="Dokumentar">Dokumentar</option> <option value ="Drama">Drama</option> <option value ="Erotik">Erotik</option> <option value ="Eventyr">Eventyr</option> <option value ="Familiefilm">Familiefilm</option> <option value ="Gangsterfilm">Gangsterfilm</option> <option value ="Gyser">Gyser</option> <option value ="Komedie">Komedie</option> <option value ="Krigsfilm">Krigsfilm</option> <option value ="Krimi">Krimi</option> <option value ="Musical">Musical</option> <option value ="Musik">Musik</option> <option value ="Romance">Romance</option> <option value ="Science Fiction">Science Fiction</option> <option value ="Standup">Standup</option> <option value ="Tegnefilm">Tegnefilm</option> <option value ="Thriller">Thriller</option> <option value ="Western">Western</option> </select> </td> </tr>
JEg har her lavet en slags eksempel - jeg håber du kan følge ideen. din select med genre tager noget tid - men evt. kan du lave den som et array så det bliver lidt smartere - ellers akn det lavs som jeg har vist.
Variablerne der står som values er jo tomme hvis edit ikke er valgt
PS: du mangler "action" tagget i din form - ja det virker i IE men ikke i alle browsere.
Jeg håber det hjalp
if(isset($_POST['add'])) { include './include/opendb.php';
include './include/closedb.php'; echo "New DVD added"; } else { // Jeg forudsætter at du har et link der hedder noget i retningen af <a href="filnavn.php?edit=$row[catalogue_number]">Ret</a> hvor catalogue_number hentes i DB if(isset($_GET['edit'])){ $res = mysql_query("SELECT * FROM dvds WHERE catalogue_number = '".$_GET['catalogue_number']."'") or die(mysql_error()); while($row = mysql_fetch_array($res)){ $title = $row['title']; $genre = $row['genre']; } } ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?do=edit"> <table width="400" border="0" cellspacing="1" cellpadding="2">
<tr> <td width="100" class="formcaption">Title</td> <td><input name="title" type="text" id="" value="<?php echo $title; ?>"></td> </tr> <tr> <td width="100" class="formcaption">Genre</td> <td> <select name="genre" id=""> <option value ="Action"<?php echo ($genre == "Action" ? " SELECTED" : ""); ?>>Action</option> <option value ="Adventure">Adventure</option> <option value ="Animation">Animation</option> <option value ="Dokumentar">Dokumentar</option> <option value ="Drama">Drama</option> <option value ="Erotik">Erotik</option> <option value ="Eventyr">Eventyr</option> <option value ="Familiefilm">Familiefilm</option> <option value ="Gangsterfilm">Gangsterfilm</option> <option value ="Gyser">Gyser</option> <option value ="Komedie">Komedie</option> <option value ="Krigsfilm">Krigsfilm</option> <option value ="Krimi">Krimi</option> <option value ="Musical">Musical</option> <option value ="Musik">Musik</option> <option value ="Romance">Romance</option> <option value ="Science Fiction">Science Fiction</option> <option value ="Standup">Standup</option> <option value ="Tegnefilm">Tegnefilm</option> <option value ="Thriller">Thriller</option> <option value ="Western">Western</option> </select> </td> </tr>
Undskylder den lange svartid, men har haft travlt. Har prøvet at rette lidt i koden, men der er sgu mange ting jeg ikke kan finde ud af. :o(
listDVDs.php ser nu sådan ud:
***** START ***** <?php
include './include/header.php';
session_start();
// is the one accessing this page logged in or not? if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) {
// not logged in, move to login page header('Location: index.php'); exit; }
include './include/opendb.php';
// Get all the data from the "dvds" table $result = mysql_query("SELECT * from dvds ORDER BY title") or die(mysql_error());
echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\">"; echo "<tr> <th> </th> <th> </th> <th class=\"listheading\">title</th> <th class=\"listheading\">genre</th> <th class=\"listheading\">Rating</th> <th class=\"listheading\">Year</th> <th class=\"listheading\">Version</th> <th class=\"listheading\">Sound</th> <th class=\"listheading\">Region</th> </tr>"; // keeps getting the next row until there are no more to get $class = false; while($row = mysql_fetch_array( $result )) { if($class == "odd") { $class = "even"; } else { $class = "odd"; } // Print out the contents of each row into a table echo "<tr><td class=\"tablelist".$class."\">"; echo "<a href=\"editDVD.php?edit=$row[catalogue_number]\">Edit</a>"; echo "</td><td class=\"tablelist".$class."\">"; echo "<a href=\" SQL HJÆLP HER: SELECT * FROM dvds WHERE catalogue_number = '".$_GET['catalogue_number']."' \">Delete</a>"; echo "</td><td class=\"tablelist".$class."\">"; echo $row['title']; echo "</td><td class=\"tablelist".$class."\">"; echo $row['genre']; echo "</td><td class=\"tablelist".$class."\">"; echo ("<img src=\"./graphic/".$row['rating'].".gif\" alt=\"\">\n"); echo "</td><td class=\"tablelist".$class."\">"; echo $row['year_produced']; echo "</td><td class=\"tablelist".$class."\">"; echo $row['version']; echo "</td><td class=\"tablelist".$class."\">"; echo $row['sound_format']; echo "</td><td class=\"tablelist".$class."\">"; echo $row['region']; echo "</td></tr>"; } echo "</table>";
include './include/closedb.php'; ?>
</body> </html> ***** SLUT *****
editDVD.php ser nu sådan ud (ved godt det ikke er rigtigt, men jeg har prøvet med så meget af din kode jeg forstod. Jeg har brug for lidt flere hints for at komme videre.):
***** START ***** <?php
include './include/header.php';
session_start();
// is the one accessing this page logged in or not? if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) {
// not logged in, move to login page header('Location: index.php'); exit; }
if(isset($_POST['add'])) { include './include/opendb.php';
<tr> <td width="100" class="formcaption">Genre</td> <td> <select name="genre" id=""> <option value ="Action">Action</option> <option value ="Adventure">Adventure</option> <option value ="Animation">Animation</option> <option value ="Dokumentar">Dokumentar</option> <option value ="Drama">Drama</option> <option value ="Erotik">Erotik</option> <option value ="Eventyr">Eventyr</option> <option value ="Familiefilm">Familiefilm</option> <option value ="Gangsterfilm">Gangsterfilm</option> <option value ="Gyser">Gyser</option> <option value ="Komedie">Komedie</option> <option value ="Krigsfilm">Krigsfilm</option> <option value ="Krimi">Krimi</option> <option value ="Musical">Musical</option> <option value ="Musik">Musik</option> <option value ="Romance">Romance</option> <option value ="Science Fiction">Science Fiction</option> <option value ="Standup">Standup</option> <option value ="Tegnefilm">Tegnefilm</option> <option value ="Thriller">Thriller</option> <option value ="Western">Western</option> </select> </td> </tr>
<tr> <td width="100" class="formcaption">Production year</td> <td> <select name="year_produced" id=""> <option value ="2006">2006</option> <option value ="2005">2005</option> <option value ="2004">2004</option> <option value ="2003">2003</option> <option value ="2002">2002</option> <option value ="2001">2001</option> <option value ="2000">2000</option> <option value ="1999">1999</option> <option value ="1998">1998</option> <option value ="1997">1997</option> <option value ="1996">1996</option> <option value ="1995">1995</option> <option value ="1994">1994</option> <option value ="1993">1993</option> <option value ="1992">1992</option> <option value ="1991">1991</option> <option value ="1990">1990</option> <option value ="1989">1989</option> <option value ="1988">1988</option> <option value ="1987">1987</option> <option value ="1986">1986</option> <option value ="1985">1985</option> <option value ="1984">1984</option> <option value ="1983">1983</option> <option value ="1982">1982</option> <option value ="1981">1981</option> <option value ="1980">1980</option> <option value ="1979">1979</option> <option value ="1978">1978</option> <option value ="1977">1977</option> <option value ="1976">1976</option> <option value ="1975">1975</option> <option value ="1974">1974</option> <option value ="1973">1973</option> <option value ="1972">1972</option> <option value ="1971">1971</option> <option value ="1970">1970</option> <option value ="1969">1969</option> <option value ="1968">1968</option> <option value ="1967">1967</option> <option value ="1966">1966</option> <option value ="1965">1965</option> <option value ="1964">1964</option> <option value ="1963">1963</option> <option value ="1962">1962</option> <option value ="1961">1961</option> <option value ="1960">1960</option> </select> </td> </tr>
<tr> <td width="100" class="formcaption">Sound format</td> <td> <select name="sound_format" id=""> <option value ="Dolby Digital 1.0 (Mono)">Dolby Digital 1.0 (Mono)</option> <option value ="Dolby Digital 2.0 (Stereo)">Dolby Digital 2.0 (Stereo)</option> <option value ="Dolby Digital 5.0">Dolby Digital 5.0</option> <option value ="DTS">DTS</option> </select> </td> </tr>
<tr> <td width="100" class="formcaption">Region code</td> <td> <select name="region" id=""> <option value ="0">0</option> <option value ="1">1</option> <option value ="2">2</option> <option value ="3">3</option> <option value ="4">4</option> <option value ="5">5</option> <option value ="9">9</option> </select> </td> </tr>
<tr> <td width="100" class="formcaption">Version</td> <td> <select name="version" id=""> <option value ="Standard">Standard</option> <option value ="Box Set">Box Set</option> <option value ="Collectors Edition">Collectors Edition</option> <option value ="Directors Cut">Directors Cut</option> <option value ="Extended Edition">Extended Edition</option> <option value ="Limited Edition">Limited Edition</option> <option value ="Special Edition">Special Edition</option> <option value ="Ultimate Edition">Ultimate Edition</option> <option value ="Superbit Version">Superbit Version</option> </select> </td> </tr>
<tr> <td width="100" class="formcaption">Rating</td> <td> <select name="rating" id=""> <option value ="0">0</option> <option value ="1">1</option> <option value ="2">2</option> <option value ="3">3</option> <option value ="4">4</option> <option value ="5">5</option> <option value ="6">6</option> </select> </td> </tr>
P.t. når jeg forsøger at editere en DVD, få jeg nedenstående fejl. Der er altså et eller andet adgangsproblem med den profil der bruges til at rette i DB'en. Det forstår jeg så ikke helt, for jeg bruger root konto'en:
***** START ***** Notice: Undefined index: catalogue_number in C:\Apache2\htdocs\dvd\editDVD.php on line 39
Warning: mysql_query(): Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Apache2\htdocs\dvd\editDVD.php on line 39
Warning: mysql_query(): A link to the server could not be established in C:\Apache2\htdocs\dvd\editDVD.php on line 39 Access denied for user 'ODBC'@'localhost' (using password: NO) ***** SLUT *****
include './include/closedb.php'; echo "New DVD added"; } else { if(isset($_GET['edit'])){ $res = mysql_query("SELECT * FROM dvds WHERE catalogue_number = '".$_GET['catalogue_number']."'") or die(mysql_error()); while($row = mysql_fetch_array($res)){ $e_title = $row['title']; // Jeg har tilføjet e_ ved alle vars pga så ved jeg de er til edit $e_genre = $row['genre']; $e_year_produced = $row['year_produced']; $e_sound_format = $row['sound_format']; $e_region = $row['region']; $e_version = $row['version']; $e_rating = $row['rating']; $e_date = $row['date_created']; } }
// opdatering af tabel if(isset($_GET['do']) && $_GET['do'] == "edit"){ mysql_query("UPDATE dvds SET ............ WHERE catalogue_number = '".$_POST['catalogue_number']."'") or die(mysql_error()); echo "Databasen for: ".$_POST['title']." blev opdateret!"; }
<tr> <td width="100" class="formcaption">Sound format</td> <td> <select name="sound_format" id=""> <?php // Her ville jeg lave det lige som ved genre måske ville jeg vælge values der ikke er tekst - men numre evt. 1-4 ?> <option value ="Dolby Digital 1.0 (Mono)">Dolby Digital 1.0 (Mono)</option> <option value ="Dolby Digital 2.0 (Stereo)">Dolby Digital 2.0 (Stereo)</option> <option value ="Dolby Digital 5.0">Dolby Digital 5.0</option> <option value ="DTS">DTS</option> </select> </td> </tr>
<tr> <td width="100" class="formcaption">Region code</td> <td> <select name="region" id=""> <?php // Her ville jeg lave det lige som ved genre ?> <option value ="0">0</option> <option value ="1">1</option> <option value ="2">2</option> <option value ="3">3</option> <option value ="4">4</option> <option value ="5">5</option> <option value ="9">9</option> </select> </td> </tr>
<tr> <td width="100" class="formcaption">Version</td> <td> <select name="version" id=""> <?php // Her ville jeg lave det lige som ved genre ?> <option value ="Standard">Standard</option> <option value ="Box Set">Box Set</option> <option value ="Collectors Edition">Collectors Edition</option> <option value ="Directors Cut">Directors Cut</option> <option value ="Extended Edition">Extended Edition</option> <option value ="Limited Edition">Limited Edition</option> <option value ="Special Edition">Special Edition</option> <option value ="Ultimate Edition">Ultimate Edition</option> <option value ="Superbit Version">Superbit Version</option> </select> </td> </tr>
<tr> <td width="100" class="formcaption">Rating</td> <td> <select name="rating" id=""> <option value ="0">0</option> <option value ="1">1</option> <option value ="2">2</option> <option value ="3">3</option> <option value ="4">4</option> <option value ="5">5</option> <option value ="6">6</option> </select> </td> </tr>
Jeg har rette i ovenståendekode - jeg håber du forstår - ellers så spørg. Dine selects kan nemt optimeres med foreach() eller for() - så bliver koden meget kortere og du skal lave mindre skrive arbejde/kodning.
New DVD formen bliver returneret, dog med <br /> i input text felterne (?). Resten af felterne ser ok med dine tidligere forslag. Når jeg har udfyldt de ønskede felter og trykker Submit, sker der ingenting. Det er som om formen ikke bliver post'ed.
editDVD.php ser sådan ud:
***** START ***** <?php
include './include/header.php'; include './include/opendb.php'; session_start();
// is the one accessing this page logged in or not? if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) {
Næste fejl optræder i listDVDs.php, hvor mit Edit kald ikke virker. Ligesom før returneres input text felterne med værdien <br /> (?), og ingen data er trukket med over. Der vises IKKE detaljer om den valgte DVD. Desuden får jeg fejlen:
Notice: Undefined index: catalogue_number in C:\Apache2\htdocs\dvd\editDVD.php on line 37 ( Linie 37: $res = mysql_query("SELECT * FROM dvds WHERE catalogue_number = '".$_GET['catalogue_number']."'") or die(mysql_error()); )
listDVD.php:
***** START ***** <?php
include './include/header.php'; include './include/opendb.php'; session_start();
// is the one accessing this page logged in or not? if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) {
// not logged in, move to login page header('Location: index.php'); exit; }
// Get all the data from the "dvds" table $result = mysql_query("SELECT * from dvds ORDER BY title") or die(mysql_error());
echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\">"; echo "<tr> <th class=\"listheading\"> </th> <th class=\"listheading\"> </th> <th class=\"listheading\">title</th> <th class=\"listheading\">genre</th> <th class=\"listheading\">Rating</th> <th class=\"listheading\">Year</th> <th class=\"listheading\">Version</th> <th class=\"listheading\">Sound</th> <th class=\"listheading\">Region</th> </tr>"; // keeps getting the next row until there are no more to get $class = false; while($row = mysql_fetch_array( $result )) { if($class == "odd") { $class = "even"; } else { $class = "odd"; } // Print out the contents of each row into a table echo "<tr><td class=\"tablelist".$class."\">"; echo "<a href=\"editDVD.php?edit=$row[catalogue_number]\">Edit</a>"; echo "</td><td class=\"tablelist".$class."\">"; echo "<a href=\"\">Delete</a>"; echo "</td><td class=\"tablelist".$class."\">"; echo $row['title']; echo "</td><td class=\"tablelist".$class."\">"; echo $row['genre']; echo "</td><td class=\"tablelist".$class."\">"; echo ("<img src=\"./graphic/".$row['rating'].".gif\" alt=\"\">\n"); echo "</td><td class=\"tablelist".$class."\">"; echo $row['year_produced']; echo "</td><td class=\"tablelist".$class."\">"; echo $row['version']; echo "</td><td class=\"tablelist".$class."\">"; echo $row['sound_format']; echo "</td><td class=\"tablelist".$class."\">"; echo $row['region']; echo "</td></tr>"; } echo "</table>";
undefined index fejlen som du får skyldes at din error_reporting i php.ini er sat til all - den skal være: all ~ e_notice (eller noget i den stil har ikke lige php.ini her ) - ret det og gust at genstarte serveren.
er der KUN <br /> i input felterne? og intet andet?
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.