Hi,

I have just started to learn php.I wrote a php code to list/add/edit/delete values of a mysql table which is already created and filled.I can print existing values in the table.However,when I want to delete one of them,the code doesn't work.I get errors for switch case.If you can give me an idea, I can solve the problem.

Here is the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>List of subjects - Options</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link href="css/layout.css" rel="stylesheet" type="text/css" />
</head>
<body>
<noscript><p>** Scripts have been disabled in your web browser, as a result of this some features may be unavailable. **</p></noscript>
<div id="subjects"> <br /> 
</div>
</body>
</html>
<?php
@mysql_connect("localhost","root","") or die (" connection error to Mysql");
@mysql_select_db("onlinequiz") or die("no connection to database");
$sql= mysql_query("SELECT * FROM subject");
echo "SubjectID\t"."Subject name\t"."Start date\t"."End date\t"."Week\t"."CourseID\t"."<br>";
while ($row = mysql_fetch_assoc($sql)) {
$id = $row["id"];
$name = $row["name"];
$sdate = $row["sdate"];
$edate = $row["edate"];
$week = $row["week"];
$courseID = $row["courseID"];
echo "$id\t"."$name\t"."$sdate\t"."$edate\t"."$week\t"."$courseID\t"."<br>";
}
switch($_GET['action']){
case 'delete':
$title = 'Delete a Person';
$content = DeleteItem();
break;
case 'new':
$title = 'Add a Person';
$content = ManageItem();
break;
case 'save':
$title = 'Save a Person';
$content = SaveItem();
break;
default:
$title = 'Options';
$content = ShowList();
break;

}
function ShowList(){


$query = mysql_query("SELECT * FROM subject");

while($row = mysql_fetch_assoc($query)){
$output = '<table border="1" width="620" cellpadding="9" style="border-collapse: collapse">
<tr>
<th>Subject</th>
<th width="76">Action</th>
</tr>';
$output .= '<tr>';
$output .= '<td>' . $row["name"] . '</td>';
$output .= '<td width="76"><a href="?action=edit&name=' . $row["name"] . '"> ';

}
$output .= '</table>';

return $output;
}
function DeleteItem(){

$Sname=$_GET["name"];
if($Sname){
$output=mysql_query("DELETE FROM subject where name='$Sname'");
if($output){
$output = '<b>One subject has been removed from the list.</b><br/><br/>';
}
else{
$output = '<b>An Error Occurred: ' . mysql_error() . '</b><br><br>';
}
}
$output = ShowList();

return $output;
}
function ManageItem(){
if(isset($_GET["name"])){
$name= $_GET["name"];

$query = mysql_query("SELECT * FROM subject WHERE name='$name'");
$row = mysql_fetch_assoc($query);
}else{
$row["id"] = '';
$row["name"] = '';
$row["sdate"] = '';
$row["edate"] = '';
$row["week"] = '';
$row["courseID"] = '';
}
return $output;
}
function SaveItem(){

if(isset($_POST["name"])){
$query = "UPDATE subject SET id='$id',name='$name', sdate='$sdate', edate='$edate', week='$week', courseID='$courseID', WHERE id='$id'";


if(mysql_query($query)){
$output = '<b>The subject has been updated.</b><br/><br/>';
}else{
$output = '<b>An Error Occurred: ' . mysql_error() . '</b><br><br>';
}

}else{
$query = "INSERT INTO subject (id,name,sdate,edate,week,courseID) VALUES ('$id','".$_POST["name"]."','".$_POST["sdate"]."','".$_POST["edate"]."','".$_POST["week"]."','".$_POST["courseID"]."')";

if(mysql_query($query)){
$output = '<b>The subject has been added to the list.</b><br/><br/>';
}else{
$output = '<b>An Error Occurred: ' . mysql_error() . '</b><br><br>';
}
}
$output .= ShowList();

return $output;

}
ob_start();
?>
<form method="POST"action="<?=$_SERVER['PHP_SELF']?>?action=save">
<?php

if(isset($_GET['name'])){
echo '<input type="hidden" name="Subject name" value="' . $name . '">';
}

?>
<table border="0" width="450">
<tr>
<td><font size="2">Save:</font></td>
<td><input type="text" name="name" size="20" value="<?php echo htmlspecialchars($row['name']); ?>"></td>
</tr>
<tr>
<td rowspan="3">
<p align="center">
<input type="submit" value="Save subject" name="submit"></td>
</tr>
</table>
</form>
<form method="POST"action="<?=$_SERVER['PHP_SELF']?>?action=new">
<table border="0" width="450">
<tr>
<td><font size="2">Add:</font></td>
<td><input type="text" name="name" size="20" value="<?php echo htmlspecialchars($row['name']); ?>"></td>
</tr>
<tr>
<td rowspan="3">
<p align="center">
<input type="submit" value="Add subject" name="submit"></td>
</tr>
</table>
</form>
<form method="POST"action="<?=$_SERVER['PHP_SELF']?>?action=delete">
<table border="0" width="450">
<tr>
<td><font size="2">Delete:</font></td>
<td><input type="text" name="name" size="20" value="<?php echo htmlspecialchars($row['name']); ?>"></td>
</tr>
<tr>
<td rowspan="3">
<p align="center">
<input type="submit" value="Delete subject" name="submit"></td>
</tr>
</table>
</form>

The problem is that you are trying to get $_POST value by using $_GET method.
Try changing your code in line 70 by modifying,

$Sname=$_POST["name"];

Go through the Predefined variables in PHP

Thanks for your concern and quick reply:)I changed the code as you said.I skipped that somehow.Anyway, it still does not work.I am using phpMyadmin and localhost for now.When I try to delete a row by entering "name" or add a new row by entering "name", it says "you dont have permission for the access".I googled the error, but found many possibilities :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.