...

View Full Version : Break Default ERROR



balkan
10-31-2006, 01:41 PM
i have error in break; default: in last line, also in mysql for sifra insert one number not generate auto code whit 5 charters, someone help me !


<?php
//------------------------------------------
//database connection
mysql_connect("localhost", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
//end database connection
//------------------------------------------

//------------------------------------------
//echo out a navigation panel
echo "
<center><a href='index.php'>Pregled na Kategoerii</a> | <a href='index.php?action=dodaj'>Dodaj SoftwareAdd Tutorial</a></center>
";
//------------------------------------------

//------------------------------------------
//begin main navigation (tutorials.php?action=)
switch($_GET['action'])
{
//------------------------------------------
//this case adds a tutorial.
//pretty self-explanitory
//------------------------------------------
case "dodaj":
//if the form to enter a new
//tutorial hasn't been submitted,
//show it
if(!isset($_POST['dodaj']))
{
echo "
<table border='0' cellpadding='0' cellspacing='0' width='500'>
<form action='$self?action=dodaj' method='post'>
<tr>
<td>Sifra:</td>
<td><input type='hidden' name='sifra'></td>
</tr>
<tr>
<td>Naslov:</td>
<td><input type='text' name='naslov'></td>
</tr>
<tr>
<td>Opis:</td>
<td><textarea name='opis' cols='40' rows='5'></textarea></td>
</tr>
<tr>
<td>Kategorija:</td>
<td>
<select name='kategorija'>
<option>- Izberi -</option>
";
//now what we are doing here is looping through
//the categorys table and getting all the
//categorys and putting them into a select
//so the user can select which category
//the tutorial is on
$query = mysql_query("SELECT * FROM software_kategorija ORDER BY id ASC") or die(mysql_error());
while($row = mysql_fetch_array($query))
{
echo "<option value='$row[id]'>$row[kategorija]";
}
echo "
</select>
</td>
</tr>
<tr>
<td>CD & DVD:</td>
<td>
<select name='cd'>
<option>- Izberi -</option>
";
//now what we are doing here is looping through
//the categorys table and getting all the
//categorys and putting them into a select
//so the user can select which category
//the tutorial is on
$query = mysql_query("SELECT * FROM software_cd ORDER BY id ASC") or die(mysql_error());
while($row = mysql_fetch_array($query))
{
echo "<option value='$row[id]'>$row[cd]";
}
echo "
</select>
</td>
</tr>
<tr>
<td>Novo?</td>
<td><input type='checkbox' name='novo' value='1' checked></td>
</tr>
<tr>
<td colspan='2'><center><input type='submit' name='dodaj' value='Submit New Software'></center></td>
</tr>
</form>
</table>
";
}
//else, error check, enter it
elseif(isset($_POST['dodaj']))
{
$string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$sifra = "";
for($i=0; $i<5; $i++){
$y = rand(0,strlen($string)-1);
$sifra .= $string[$y];
}

$naslov = mysql_real_escape_string(strip_tags($_POST['naslov']));
$opis = mysql_real_escape_string(strip_tags($_POST['opis']));
$kategorija = mysql_real_escape_string(strip_tags($_POST['kategorija']));
$cd = mysql_real_escape_string(strip_tags($_POST['cd']));
$novo = mysql_real_escape_string($_POST['novo']);
$datum = date("m/d/Y");

//we begin error checking....
$error_msg = array();
if(empty($naslov))
{
$error_msg[] = "Please insert a naslov!<br />";
}
if(empty($opis))
{
$error_msg[] = "Please insert a opis!<br />";
}
if(empty($kategorija))
{
$error_msg[] = "Please insert a kategorija!<br />";
}
if(empty($cd))
{
$error_msg[] = "Please select CD!<br />";
}
//print the errors, if any
if(count($error_msg)>0)
{
echo "<strong>ERROR:</strong><br>\n";
foreach($error_msg as $err)
echo "$err";
}
//everythings ok, insert it to the DB
else
{
$sql = "INSERT INTO software (sifra, naslov, opis, kat_id, cd_id, novo, datum, validen) VALUES ('$sifra', '$naslov', '$opis', '$kategorija', '$cd', '$novo', '$datum', '1')";
mysql_query($sql) or die(mysql_error());
echo "Softwerot e uspesno dodaden, za pregled!";
}
}
break;

//------------------------------------------
//this case gets the specified [ID] in the url
//(tutorials.php?action=viewcategory&id=[ID]
//and gets all the tutorials listed under that
//category ID (cat_id)
//------------------------------------------
case "pregled":
//if there is an ID given...
if($_GET['id'])
{
//get the id, put it into a variable, cast to an INT
//(for security purposes)
$id = (int)$_GET['id'];
$query = mysql_query("SELECT * FROM software WHERE kat_id = '$id' AND validen = '1'") or die(mysql_error());

//if no results, show that there are no tutorials
//for that category
if(mysql_num_rows($query) == 0)
{
echo "Nema software vo ovaa Kategorija!";
}
//else, there is..show em
else
{
echo "<h1>Softwares</h1>";
//loop through the tutorials
//show all tutorials
echo "<table border='0' cellpadding='0' cellspacing='0' width='500'>";
while($row = mysql_fetch_array($query))
{
echo "
<tr>
<td>Sifra:</td>
<td><b>$row[sifra]</b></td>
</tr>
<tr>
<td>Naslov:</td>
<td><b>$row[naslov]</b></td>
</tr>
<tr>
<td>Opis:</td>
<td>$row[opis]</td>
</tr>
<tr>
<td>Kategorija:</td>
<td>$row[kat_id]</td>
</tr>
<tr>
<td>CD & DVD:</td>
<td>$row[cd_id]</td>
</tr>
<tr>
<td>Novo:</td>
<td>$row[novo]</td>
</tr>
<tr>
<td>Datum:</td>
<td>$row[datum]</td>
</tr>
<tr>
<td>Izmeni:</td>
<td colspan='2'><b><a href='$self?akcija=izmeni&id=$row[id]'>Izmeni</a></b></td>
</tr>
<tr>
<td colspan='2'><hr /></td>
</tr>
";
}
echo "</table>";
}
}
else
{
echo "Momentalno nema Softwares!";
}
break;

//------------------------------------------
//this case gets the given [ID]
//action=viewtutorial&id=[ID]
//and gets that tutorial ID from the database
//and displays it!
//------------------------------------------
case "izmeni":
//if there is an ID given..
if($_GET['id'])
{
//set $id to the URL id, cast to an INT
//for security purposes
$id = (int)$_GET['id'];

//query the database
$query = mysql_query("SELECT * FROM software WHERE id = '$id'") or die (mysql_error());

//if no rows returned...
if(mysql_num_rows($query) == 0)
{
echo "That ID is not in the database!";
}
//else, show it!
else
{
//update the views for this tutorial!
$popravi = mysql_query("UPDATE software SET naslov = '$naslov', opis = '$opis', kat_id = '$kategorija', cd_id = '$cd', novo = '$novo' WHERE id = '$id'") or die(mysql_error());

//loop through the database
while($row = mysql_fetch_array($query))
{
echo "
<table border='0' cellpadding='0' cellspacing='0' width='500' style='border: 1px solid black; padding: 3px;'>
<tr>
<td colspan='2'>Software: <b>$row[naslov]</b></td>
</tr>
<tr>
<td>Naslov:</td>
<td><input type='text' name='naslov' value='$row[naslov]'></td>
<tr>
<td>Opis:</td>
<td><textarea name='opis' cols='40' rows='5' value='$row[opis]'></textarea></td>
</tr>
<tr>
<td>Kategorija:</td>
<td>
<select name='kategorija'>
";
//now what we are doing here is looping through
//the categorys table and getting all the
//categorys and putting them into a select
//so the user can select which category
//the tutorial is on
$query = mysql_query("SELECT * FROM software_kategorija ORDER BY id ASC") or die(mysql_error());
while($row = mysql_fetch_array($query))
{
echo "<option>- Izberi -</option>";
echo "<option value='$row[id]'>$row[kategorija]";
}
echo "
</select>
</td>
</tr>
<tr>
<td>CD & DVD:</td>
<td>
<select name='cd'>
";
//now what we are doing here is looping through
//the categorys table and getting all the
//categorys and putting them into a select
//so the user can select which category
//the tutorial is on
$query = mysql_query("SELECT * FROM software_cd ORDER BY id ASC") or die(mysql_error());
while($row = mysql_fetch_array($query))
{
echo "<option>- Izberi CD -</option>";
echo "<option value='$row[id]'>$row[cd]";
}
echo "
</select>
</td>
</tr>
<tr>
<td>Novo?</td>
<td><input type='checkbox' name='novo' value='1' checked></td>
</tr>
<tr>
<td colspan='2'><center><input type='submit' name='izmeni' value='Submit New Software'></center></td>
</tr>
</form>
</table>
</tr>
<tr>
<td colspan='2' style='border: 1px solid black;'><center><b>Software</b></center><br />$row[text]</td>
</tr>
<tr>
";
}
//----------------------------
//this part of the code
//checks to see if the submitter
//wants an email left for support
//----------------------------
$new = "Novo";
if($row['novo'] == 1)
{
echo "$new";
}
echo "
</tr>
<tr>
<td><hr /></td>
</tr>
";
}
break;
//------------------------------------------
//default case, this is shown default
//in this instance, we are going to make the default case show
//all the categories that you can view tutorials on
//------------------------------------------
default:
$query = mysql_query("SELECT * FROM software_kategorija") or die(mysql_error());
//if the number of rows returned is 0, then say, no categories
if(mysql_num_rows($query) == 0)
{
echo "Nema Kategorii!";
}
//if anything else, then there has to be categories. show em.
else
{
echo "<h1>Software Kategorii:</h1> ";
//while loop to loop through the database and display results!
while($row = mysql_fetch_array($query))
{
echo "
<table border='0' cellpadding = '0' cellspacing='0' width='500'>
<tr>
<td>Ime na Kategorijata:</td>
<td>$row[kategorija]</td>
</tr>
<tr>
<td><a href='$self?akcija=pregled&id=$row[id]'>Pogedni ja Kategorijata</a></td>
</tr>
<tr>
<td><hr /></td>
</tr>
</table>
";
}
}
break;
}
}
//end navigation
//------------------------------------------
?>

ess
10-31-2006, 03:41 PM
I haven't gone through all of your code...as it doesn't look very clean and tidy. But here is what I think you need to do.

If you look at PHP Switch (http://uk.php.net/switch) documentation, you will notice that you shouldn't use a break statement after default statement.

Here is an example

switch( $your_var )
{
case 1:
case 2:
case 3:
echo "could have been case 0, case 1, or case3...not sure";
break;
case 4:
echo "Hello from Case4";
break;
default:
echo "No match was found";
} # ends switch

if the value of $your_var is equal to 1, 2 or 3, the output will be the same...as there is no break statement to stop the execution.

if the value of $your_var is equal to 4, the output will be "Hello from Case4".


if the value of $your_var is not equal to any of the cases, the output will be "No match was found".

All the best.
:thumbsup:

NancyJ
10-31-2006, 04:35 PM
While its not required, putting break in your default block wont do any harm - the problem is its in the wrong place

your


break;
}
}

should be


}
break;
}


or just get rid of the last break entirely - its the last statement in the switch so theres nothing to break out of.

As for your code not being clean and tidy - I think it looks perfectly respectable



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum