PHP - Switch Statement Help
Hi all,
I am creating a switch page which will be acting as more of an index page for the different php files I have in my project. I have seven different pages and, I am a bit stuck as to what the order is and where the codes should go. I have got this far as is shown below: Code: [Select] <!--PHP Update--> <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("project1", $con); if ($_GET['action']=='delete_ok' and or){ }elseif ($_GET['action']=='edit_ok'){ } switch($_GET['action']){ case 'delete_ok': #########Delete OK Start################# //echo '<meta http-equiv="Refresh" content="0;URL=switch.php">'; #########Delete OK End ################# break; case 'edit_ok': break; } ?> <!--HTML--> <?php switch($_GET['action']){ case 'delete'; break; case 'edit'; echo 'Edit page'; ?> <form action="switch.php?action=edit_ok" method="post"> <?php break; default: echo "<table border='1'> <tr> <th>ID</th> <th>Date Of Birth</th> <th>Gender</th> <th>Title</th> <th>First Name</th> <th>Last Name</th> <th>Address Line 1</th> <th>Address Line 2</th> <th>City</th> <th>Postcode</th> <th>Contact No</th> <th>Email</th> <th>Additional Info</th> <th>Action</th> </tr>"; $result = mysql_query("SELECT * FROM project_data"); while($row = mysql_fetch_array($result)){ echo "<tr>"; echo "<td>" . $row['ID'] . "</td>"; echo "<td>" . $row['Date_Of_Birth'] . "</td>"; echo "<td>" . $row['Gender'] . "</td>"; echo "<td>" . $row['Title'] . "</td>"; echo "<td>" . $row['First_Name'] . "</td>"; echo "<td>" . $row['Last_Name'] . "</td>"; echo "<td>" . $row['Address_Line_1'] . "</td>"; echo "<td>" . $row['Address_Line_2'] . "</td>"; echo "<td>" . $row['City'] . "</td>"; echo "<td>" . $row['Postcode'] . "</td>"; echo "<td>" . $row['Contact_No'] . "</td>"; echo "<td>" . $row['Email'] . "</td>"; echo "<td>" . $row['Additional_Comment'] . "</td>"; echo "<td><a href='switch.php?action=edit&id=" . $row['ID']."'>Edit</a>  <a href='delete_write.php?id=" . $row['ID']."'>Delete</a></td>"; echo "</tr>"; } echo "</table>"; echo "<a href='write.php'>Insert</a>"; } mysql_close($con); ?> the file names a delete_write.php- deletes the data display.php- displays the data edit_write.php- editing the info edit_write_ok.php- edits on the database and shows on display.php write.php- this is the input page write_input.php- this inputs the data from write.php into the main database any help would be much appreciated. MOD EDIT: code tags added. Similar TutorialsHello, when you build a switch, does it have to have a default case? If so, is there a way to get it to default to the first case instead of re posting the code in the default? Code: [Select] <?php echo '<form action="hwCase.php" method="post"> Enter number <input type="text" name="number"/><br /> </form>'; $input = $_POST['number']; // if a number is entered in the form, ie not null if (isset($input)) { switch ($input) { // if number entered is less than 0, ie negative number case ($input<0): echo "Enter a positive number."; break; // if number entered is greater than 1000 case ($input>1000): echo "Enter a number less than 1000."; break; // if number entered is not a number, ie a letter, a character case (!ctype_digit($input)): echo "<br><img src=\"squidward.jpg\"><br><br>Enter a valid number. "; break; // if valid number is entered, loop "hello world" that many times case (($input>0) && ($input<1000)): echo "hello world " . $input . "<br />"; break; // if anything else is entered default: echo "Enter a number."; break; } } // if no number is entered in the form, ie null else { echo "Please enter a number."; } ?> For some reason, my loop doesn't work. Please help me fix it! Thanks in advance! I need to loop this part: Code: [Select] case (($input>0) && ($input<1000)): echo "hello world " . $input . "<br />"; break; $price = 0.00; switch($someValue) { case "One": $price = 1; break; case "Two": $total = $price + 2; echo $total break; } I'm having problem on this. Basically what i want is to use the value of $price in case "One" to case "Two". But it won't work. Any idea how to accomplish this? Thank you in advance. I couldn't understand switch statements in .net and I still don't get them with php. Can someone turn this into a switch statement, I just can't wrap my head around then. if (isset($_POST['news'])){ add($_POST['text']); header("location: admin.php"); } elseif (isset($_POST['rnews'])){ deletenews($_POST['number']); header("location: admin.php"); } elseif (isset($_POST['taddm'])){ taddmember($_POST['website'], $_POST['name']); header("location: admin.php"); } elseif (isset($_POST['tdelm'])){ tdeletemember($_POST['number']); header("location: admin.php"); } elseif (isset($_POST['addm'])){ addmember($_POST['website'], $_POST['name']); header("location: admin.php"); } elseif (isset($_POST['delm'])){ deletemember($_POST['number']); header("location: admin.php"); } elseif (isset($_POST['tnews'])){ tadd($_POST['text']); header("location: admin.php"); } elseif (isset($_POST['trnews'])){ tdelete($_POST['number']); header("location: admin.php"); } elseif (isset($_POST['video'])){ videoupdate($_POST['link']); header("location: admin.php"); } else { echo " Failed"; } I have database that is a list of categories that I would like to insert into a switch statement. This is the only way that I know how to do it right off hand and of course it doesn't work. I've looked on the internet and found examples but they are slightly over my head as ways to do it. Depending on $group which comes from the url a title an h1 would be assigned. Code: [Select] $q = "SELECT * FROM categories"; $r = @mysqli_query ($dbc, $q); switch ($group) { if ($r) { //If $r ran OK while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){ case '$row['id_cat']': $h1 = '.$row['group']'; break; } } } Thanks S i am trying to get the allowed file types from the database into a switch statement but with no luck so far. They types are in the database like: jpg, png, gif. All seperated with a comma and all in one db column. I am then splitting them at the comma and inserting them into $file_list. In the switch statement i want it to be like: switch($ext) { case "jpg": case "png": case "gif": break; } how can i make this possible? here is my code so far: $ext = strtolower(pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION)); $f_types = $f_types_info['file_types']; $f_types = preg_split("|\,|", $f_types); $types = count($f_types); $file_list = ""; for($i=0; $i<$types; $i++) { $file_list .= $f_types[$i]; } switch ($ext) { case $file_list: break; default: $error = "File Type Not Allowed"; break; } Hello Everyone, I have to change the if statements to a switch statement, in this game. Can anyone help? Thanks. var checkKeyPressed = function (e) { // Press key A or key D to make dog run to the left or right // The running speed is specified by the step variable, 10 by default. // If you replace 10 with a larger integer, the dog will run faster. //press A, dog runs left if (e.keyCode == "65") { if (prex[0] > 5) { prex[0] = prex[0] - step; } } //press D, dog runs right if (e.keyCode == "68") { if (prex[0] < right) { prex[0] = prex[0] + step; } } }; Hi there, I am working on a PHP web form. There is simple textbox where users can enter countries. If the user enters any of the European countries for example Spain, Germany or Italy then the web page must echo ' You entered a European country. The code I am using is: switch ($txtCountry) { case 'Germany' || 'Spain' || 'Belgium' || 'Cyprus' || 'Estonia': echo "You entered a European Country"; break; case 'Japan': echo "You entered a Far Eastern Country"; break; default: echo "Unknown Country"; break; } Now the problem is even if I enter a different country like Japan, it goes to the first Case: i.e. 'You entered a European Country' Whats the best way to use Switch case to check multiple values. Is my Syntax correct or do I need to use single quote of double quote here. Please reply. Thank you! Hey guys, I have a mysql switch statement that shows different things based on the row value. Here it is... <?php switch ($rows['icon']) { case 1: $picture = '<img src=\"img/apple.gif\" title=\"apple\" alt=\"apple\" />'; echo $picture; break; case 2: $picture = '<img src=\"img/banana.gif\" title=\"banana\" alt=\"banana\" />'; echo $picture; break; case 3: $picture = '<img src=\"img/orange.gif\" title=\"orange\" alt=\"orange\" />'; echo $picture; break; default: echo "$rows[icon] is something other than 1 2 or 3"; break; } ?> Here is how the other code looks like. <?php error_reporting(E_ALL); ini_set('display_errors', '1'); mysql_connect("localhost", "", "")or die("cannot connect"); mysql_select_db("test")or die("cannot select DB"); $tbl_name="test_mysql"; $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $count=mysql_num_rows($result); if (isset($_POST['Submit'])) { for($i=0;$i<$count;$i++){ $month = $_POST['month']; $date = $_POST['date']; $message = $_POST['message']; $title = $_POST['title']; $id = $_POST['id']; $icon = $_POST['icon']; // ILYA $monthday= $month[$i]."<br>".$date[$i]; $sql1="UPDATE $tbl_name SET monthday='$monthday', month='$month[$i]', date='$date[$i]', message='" . mysql_real_escape_string($message[$i]) . "', title='" . mysql_real_escape_string($title[$i]) . "', icon='$icon[$i]' WHERE id=".$id[$i]; // ILYA if(!($result1 = mysql_query($sql1))){ "<BR>Error UPDATING $tbl_name "; exit(); } } } $result=mysql_query($sql); $count=mysql_num_rows($result); ?> Dont mind the other UPDATE code its part of something else. Anyways, why is the case automaticaly going to the last one... with the words: is something other than 1 2 or 3 Also, this is with multiple rows at once so I think that that may the problem... As the title says, I'm looking for a better way to create a switch statement on an "edit" form. Since there are 3 of the same types of <selects>, all using the values of US states. Billing state, shipping state, drivers license state. Currently I am using this: switch($row['pri_add4']){ case 'AK': $pri_add4 = '<option value="AK" selected="selected">AK</option><option value="AL">AL</option><option value="AR">AR</option><option value="AZ">AZ</option><option value="CA">CA</option><option value="CO">CO</option><option value="CT">CT</option><option value="DC">DC</option><option value="DE">DE</option><option value="FL">FL</option><option value="GA">GA</option><option value="HI">HI</option><option value="IA">IA</option><option value="ID">ID</option><option value="IL">IL</option><option value="IN">IN</option><option value="KS">KS</option><option value="KY">KY</option><option value="LA">LA</option><option value="MA">MA</option><option value="MD">MD</option><option value="ME">ME</option><option value="MI">MI</option><option value="MN">MN</option><option value="MO">MO</option><option value="MS">MS</option><option value="MT">MT</option><option value="NC">NC</option><option value="ND">ND</option><option value="NE">NE</option><option value="NH">NH</option><option value="NJ">NJ</option><option value="NM">NM</option><option value="NV">NV</option><option value="NY">NY</option><option value="OH">OH</option><option value="OK">OK</option><option value="OR">OR</option><option value="PA">PA</option><option value="RI">RI</option><option value="SC">SC</option><option value="SD">SD</option><option value="TN">TN</option><option value="TX">TX</option><option value="UT">UT</option><option value="VA">VA</option><option value="VT">VT</option><option value="WA">WA</option><option value="WI">WI</option><option value="WV">WV</option><option value="WY">WY</option>'; break; case 'AL': $pri_add4 = '<option value="AK">AK</option><option value="AL" selected="selected">AL</option><option value="AR">AR</option><option value="AZ">AZ</option><option value="CA">CA</option><option value="CO">CO</option><option value="CT">CT</option><option value="DC">DC</option><option value="DE">DE</option><option value="FL">FL</option><option value="GA">GA</option><option value="HI">HI</option><option value="IA">IA</option><option value="ID">ID</option><option value="IL">IL</option><option value="IN">IN</option><option value="KS">KS</option><option value="KY">KY</option><option value="LA">LA</option><option value="MA">MA</option><option value="MD">MD</option><option value="ME">ME</option><option value="MI">MI</option><option value="MN">MN</option><option value="MO">MO</option><option value="MS">MS</option><option value="MT">MT</option><option value="NC">NC</option><option value="ND">ND</option><option value="NE">NE</option><option value="NH">NH</option><option value="NJ">NJ</option><option value="NM">NM</option><option value="NV">NV</option><option value="NY">NY</option><option value="OH">OH</option><option value="OK">OK</option><option value="OR">OR</option><option value="PA">PA</option><option value="RI">RI</option><option value="SC">SC</option><option value="SD">SD</option><option value="TN">TN</option><option value="TX">TX</option><option value="UT">UT</option><option value="VA">VA</option><option value="VT">VT</option><option value="WA">WA</option><option value="WI">WI</option><option value="WV">WV</option><option value="WY">WY</option>'; break; case 'AR': $pri_add4 = '<option value="AK">AK</option><option value="AL">AL</option><option value="AR" selected="selected">AR</option><option value="AZ">AZ</option><option value="CA">CA</option><option value="CO">CO</option><option value="CT">CT</option><option value="DC">DC</option><option value="DE">DE</option><option value="FL">FL</option><option value="GA">GA</option><option value="HI">HI</option><option value="IA">IA</option><option value="ID">ID</option><option value="IL">IL</option><option value="IN">IN</option><option value="KS">KS</option><option value="KY">KY</option><option value="LA">LA</option><option value="MA">MA</option><option value="MD">MD</option><option value="ME">ME</option><option value="MI">MI</option><option value="MN">MN</option><option value="MO">MO</option><option value="MS">MS</option><option value="MT">MT</option><option value="NC">NC</option><option value="ND">ND</option><option value="NE">NE</option><option value="NH">NH</option><option value="NJ">NJ</option><option value="NM">NM</option><option value="NV">NV</option><option value="NY">NY</option><option value="OH">OH</option><option value="OK">OK</option><option value="OR">OR</option><option value="PA">PA</option><option value="RI">RI</option><option value="SC">SC</option><option value="SD">SD</option><option value="TN">TN</option><option value="TX">TX</option><option value="UT">UT</option><option value="VA">VA</option><option value="VT">VT</option><option value="WA">WA</option><option value="WI">WI</option><option value="WV">WV</option><option value="WY">WY</option>'; break; case 'AZ': $pri_add4 = '<option value="AK">AK</option><option value="AL">AL</option><option value="AR">AR</option><option value="AZ" selected="selected">AZ</option><option value="CA">CA</option><option value="CO">CO</option><option value="CT">CT</option><option value="DC">DC</option><option value="DE">DE</option><option value="FL">FL</option><option value="GA">GA</option><option value="HI">HI</option><option value="IA">IA</option><option value="ID">ID</option><option value="IL">IL</option><option value="IN">IN</option><option value="KS">KS</option><option value="KY">KY</option><option value="LA">LA</option><option value="MA">MA</option><option value="MD">MD</option><option value="ME">ME</option><option value="MI">MI</option><option value="MN">MN</option><option value="MO">MO</option><option value="MS">MS</option><option value="MT">MT</option><option value="NC">NC</option><option value="ND">ND</option><option value="NE">NE</option><option value="NH">NH</option><option value="NJ">NJ</option><option value="NM">NM</option><option value="NV">NV</option><option value="NY">NY</option><option value="OH">OH</option><option value="OK">OK</option><option value="OR">OR</option><option value="PA">PA</option><option value="RI">RI</option><option value="SC">SC</option><option value="SD">SD</option><option value="TN">TN</option><option value="TX">TX</option><option value="UT">UT</option><option value="VA">VA</option><option value="VT">VT</option><option value="WA">WA</option><option value="WI">WI</option><option value="WV">WV</option><option value="WY">WY</option>'; break; case 'CA': $pri_add4 = '<option value="AK">AK</option><option value="AL">AL</option><option value="AR">AR</option><option value="AZ">AZ</option><option value="CA" selected="selected">CA</option><option value="CO">CO</option><option value="CT">CT</option><option value="DC">DC</option><option value="DE">DE</option><option value="FL">FL</option><option value="GA">GA</option><option value="HI">HI</option><option value="IA">IA</option><option value="ID">ID</option><option value="IL">IL</option><option value="IN">IN</option><option value="KS">KS</option><option value="KY">KY</option><option value="LA">LA</option><option value="MA">MA</option><option value="MD">MD</option><option value="ME">ME</option><option value="MI">MI</option><option value="MN">MN</option><option value="MO">MO</option><option value="MS">MS</option><option value="MT">MT</option><option value="NC">NC</option><option value="ND">ND</option><option value="NE">NE</option><option value="NH">NH</option><option value="NJ">NJ</option><option value="NM">NM</option><option value="NV">NV</option><option value="NY">NY</option><option value="OH">OH</option><option value="OK">OK</option><option value="OR">OR</option><option value="PA">PA</option><option value="RI">RI</option><option value="SC">SC</option><option value="SD">SD</option><option value="TN">TN</option><option value="TX">TX</option><option value="UT">UT</option><option value="VA">VA</option><option value="VT">VT</option><option value="WA">WA</option><option value="WI">WI</option><option value="WV">WV</option><option value="WY">WY</option>'; break; case 'CO': $pri_add4 = '<option value="AK">AK</option><option value="AL">AL</option><option value="AR">AR</option><option value="AZ">AZ</option><option value="CA">CA</option><option value="CO" selected="selected">CO</option><option value="CT">CT</option><option value="DC">DC</option><option value="DE">DE</option><option value="FL">FL</option><option value="GA">GA</option><option value="HI">HI</option><option value="IA">IA</option><option value="ID">ID</option><option value="IL">IL</option><option value="IN">IN</option><option value="KS">KS</option><option value="KY">KY</option><option value="LA">LA</option><option value="MA">MA</option><option value="MD">MD</option><option value="ME">ME</option><option value="MI">MI</option><option value="MN">MN</option><option value="MO">MO</option><option value="MS">MS</option><option value="MT">MT</option><option value="NC">NC</option><option value="ND">ND</option><option value="NE">NE</option><option value="NH">NH</option><option value="NJ">NJ</option><option value="NM">NM</option><option value="NV">NV</option><option value="NY">NY</option><option value="OH">OH</option><option value="OK">OK</option><option value="OR">OR</option><option value="PA">PA</option><option value="RI">RI</option><option value="SC">SC</option><option value="SD">SD</option><option value="TN">TN</option><option value="TX">TX</option><option value="UT">UT</option><option value="VA">VA</option><option value="VT">VT</option><option value="WA">WA</option><option value="WI">WI</option><option value="WV">WV</option><option value="WY">WY</option>'; break; case 'CT': $pri_add4 = '<option value="AK">AK</option><option value="AL">AL</option><option value="AR">AR</option><option value="AZ">AZ</option><option value="CA">CA</option><option value="CO">CO</option><option value="CT" selected="selected">CT</option><option value="DC">DC</option><option value="DE">DE</option><option value="FL">FL</option><option value="GA">GA</option><option value="HI">HI</option><option value="IA">IA</option><option value="ID">ID</option><option value="IL">IL</option><option value="IN">IN</option><option value="KS">KS</option><option value="KY">KY</option><option value="LA">LA</option><option value="MA">MA</option><option value="MD">MD</option><option value="ME">ME</option><option value="MI">MI</option><option value="MN">MN</option><option value="MO">MO</option><option value="MS">MS</option><option value="MT">MT</option><option value="NC">NC</option><option value="ND">ND</option><option value="NE">NE</option><option value="NH">NH</option><option value="NJ">NJ</option><option value="NM">NM</option><option value="NV">NV</option><option value="NY">NY</option><option value="OH">OH</option><option value="OK">OK</option><option value="OR">OR</option><option value="PA">PA</option><option value="RI">RI</option><option value="SC">SC</option><option value="SD">SD</option><option value="TN">TN</option><option value="TX">TX</option><option value="UT">UT</option><option value="VA">VA</option><option value="VT">VT</option><option value="WA">WA</option><option value="WI">WI</option><option value="WV">WV</option><option value="WY">WY</option>'; break; case 'DC': $pri_add4 = '<option value="AK">AK</option><option value="AL">AL</option><option value="AR">AR</option><option value="AZ">AZ</option><option value="CA">CA</option><option value="CO">CO</option><option value="CT">CT</option><option value="DC" selected="selected">DC</option><option value="DE">DE</option><option value="FL">FL</option><option value="GA">GA</option><option value="HI">HI</option><option value="IA">IA</option><option value="ID">ID</option><option value="IL">IL</option><option value="IN">IN</option><option value="KS">KS</option><option value="KY">KY</option><option value="LA">LA</option><option value="MA">MA</option><option value="MD">MD</option><option value="ME">ME</option><option value="MI">MI</option><option value="MN">MN</option><option value="MO">MO</option><option value="MS">MS</option><option value="MT">MT</option><option value="NC">NC</option><option value="ND">ND</option><option value="NE">NE</option><option value="NH">NH</option><option value="NJ">NJ</option><option value="NM">NM</option><option value="NV">NV</option><option value="NY">NY</option><option value="OH">OH</option><option value="OK">OK</option><option value="OR">OR</option><option value="PA">PA</option><option value="RI">RI</option><option value="SC">SC</option><option value="SD">SD</option><option value="TN">TN</option><option value="TX">TX</option><option value="UT">UT</option><option value="VA">VA</option><option value="VT">VT</option><option value="WA">WA</option><option value="WI">WI</option><option value="WV">WV</option><option value="WY">WY</option>'; break; } NOTE: I had to remove a lot of the state options so the post wouldn't exceed the 40000 characters limit. I have a "switch statement" that is a bit ridiculous switch($row['warr_Ext_Term']) { case '24 month / 24,000 miles': $month24 = date('Y-m-d',strtotime($date_sold . '+730 days')); $warr_Ext_Exp_Date = substr($month24, 5, 2).'-'.substr($month24, 8, 2).'-'.substr($month24, 2, 2); $warr_Ext_Exp_Miles = number_format($row['mileage'] + 24000).' miles'; $today = date('Y-m-d'); if ($month24 > $today) { $warr_Ext_Exp_Text = 'Expires'; } else { $class = 'red'; $warr_Ext_Exp_Text = '<span class="'.$class.'">Expired</span>'; } break; } $warr_Ext_Cmpy = ' <li><label>Extended</label>'.$row['warr_Ext_Cmpy'].'</li> <li><label>Term</label>'.$row['warr_Ext_Term'].'</li> <li><label>Contract #</label>'.$row['warr_Ext_Num'].'</li> <li><label>'.$warr_Ext_Exp_Text.'</label><span class="'.$class.'">'.$warr_Ext_Exp_Date.'</span><br><label> </label><span class="'.$class.'">'.$warr_Ext_Exp_Miles.'</span></li>' ; What it does is determines when a customers warranty expires, or when it did expire based on the miles that they bought the vehicle at, and based on today's date. The problem is that I sell numerous warranties from 9 days, 4,500 miles up to 120 months, 100,000 miles. Is there a better way to write this, based on the info pulled from the "$row['warr_Ext_Term']" row? Any help would be greatly appreciated... Morning, I'm working on a web site using a switch to separate different calls from a MySQL database. My style looks like this: <?php switch ($_GET['page']) { case 'news': echo ("NEWS HERE"); } break; case 'schedule'; echo ("SCHEDULE HERE"); } break; default: echo ("MAIN PAGE HERE"); break; } What I am trying to do, is specify that if the page is news, there can be an additional variable for id. I attempted before and after the break; for the news case, to make a new switch, but it isn't taking. I've looked around online, but can't find any information about this. Can anyone offer a hand? I'm having issues with the following: Code: [Select] <?php session_start(); $_SESSION['username']=$_POST['username']; $_SESSION['password']=$_POST['password']; if($_SESSION['username']=="username" && $_SESSION['password']=="password"){ if($_GET['product']=="add"){ $content.=' <p><label>Product Name:</label> <input type="text" name="product_name" size="30" /> <label>Product Price:</label> <input type="text" name="product_price" size="5" /> </p> <p><label>Product Category:</label> <input type="text" name="product_category" size="30" /></p> <p><label>Product Link:</label> <input type="text" name="product_link" size="30" /></p> <p><label>Product Image:</label> <input type="text" name="product_image" size="30" /></p> <p><label>Product Tag:</label> <input type="text" name="product_tag" size="30" /></p> <p><label>Product Keywords:</label> <input type="text" name="keyword" size="30" /></p> <p><label>Product Features:</label><br /> <textarea name="product_features" rows="10" cols="60"></textarea> </p> <p><label>Product Pros:</label><br /> <textarea name="product_pros" rows="5" cols="30"></textarea> </p> <p><label>Product Cons:</label><br /> <textarea name="product_cons" rows="5" cols="30"></textarea> </p> <p><label>Product Description:</label><br /> <textarea name="product_description" rows="10" cols="60"></textarea> </p> <p><label>Product Notes:</label><br /> <textarea name="product_notes" rows="5" cols="30"></textarea> </p> '; $logout='<div><a href="./acp_admincp.php?log-out">Log-Out</a></div>'; } elseif($_GET['product']=="view"){ } else{ $content.=' <a href="./admincp.php?product=add">Add New Product</a> <br /> <a href="./admincp.php?product=view">View Products</a> '; } } elseif(isset($_GET['log-out'])){ session_start(); session_unset(); session_destroy(); header("Location: ./admincp.php"); } else{ $content=' <form action="./admincp.php" method="post"> <p><label>Username:</label> <input type="text" name="username" size="30" />'; $content.='</p> <p><label>Password:</label> <input type="password" name="password" /></p>'; $content.='<p><input type="submit" value="Submit" name="Submit" /></p> </form>'; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <base href="http://ghosthuntersportal.com/" /> <title>Ghost Hunter's Portal - Admin Control Panel</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta name="verify-v1" content="" /> <meta name="keywords" content="ghost, hunters, hunter, ghosts, spirit, spirits, paranormal, investigation, investigator, investigators, k2, emf, meter, kii" /> <meta name="description" content="Ghost Hunters Potal. Parnormal research equipment store." /> <meta name="author" content="Andrew McCarrick" /> <meta name="robots" content="index, follow" <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <img src="./logo.png" alt="Ghost Hunter's Portal Admin Control Panel" /> <br /> <div style="color: #AA44AA; font-size: 26px; margin-top: -30px; margin-left: 125px;">Admin Control Panel</div> <?php echo $logout; echo $content; ?> </body> </html> I can log-in, and get to the page with the two links on it. However, once I click one of the links it falls back to the log-in page, and it ends up being a never ending loop. It's doing this: Log-In --> Page with links ---> Log-In page again Should be doing this: Log-In --> Page with links --> Add Product page or View Products page I can never get into the the actual sub page. Just to be clear, the address bar actually shows product=add or product=view, but it still shows the log-in page. Code: [Select] <!--PHP Update--> <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("project1", $con); if ($_GET['action']=='delete_ok'){ }elseif ($_GET['action']=='edit_ok'){ } switch($_GET['action']){ case 'write_ok'; $sql="INSERT INTO project_data (Date_Of_Birth, Gender, Title, First_Name, Last_Name, Address_Line_1, Address_Line_2, City, Postcode, Contact_No, Email, Additional_Comment) VALUES ('".$_POST[dateofbirth]."','".$_POST[gender]."','".$_POST[title]."','".$_POST[firstname]."','".$_POST[lastname]."','".$_POST[address1]."','".$_POST[address2]."','".$_POST[city]."','".$_POST[postcode]."','".$_POST[contactno]."','".$_POST[email]."','".$_POST[note]."')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }else{ echo '<script>alert("Data Has Been Successfully Updated");</script>'; echo '<meta http-equiv="Refresh" content="0;URL=switch.php">'; } break; case 'edit_ok'; if ((!isset($_GET['id']) || trim($_GET['id']) == '')) { die('Missing record ID!'); } $id= $_GET['id']; mysql_query("UPDATE project_data SET Date_Of_Birth='".$_POST[dateofbirth]."',Gender='".$_POST[gender]."',Title='".$_POST[title]."',First_Name='".$_POST[firstname]."',Last_Name='".$_POST[surname]."',Address_Line_1='".$_POST[address1]."',Address_Line_2='".$_POST[address2]."',City='".$_POST[city]."',Postcode='".$_POST[postcode]."',Contact_No='".$_POST[contactno]."',Email='".$_POST[email]."',Additional_Comment='".$_POST[note]."' WHERE ID='".$_GET['id']. "'") or die ("Error in query: $query. " . mysql_error()); echo '<script>alert("Data Has Been Successfully Updated");</script>'; echo '<meta http-equiv="Refresh" content="0;URL=switch.php">'; } break; case 'delete_ok': #########Delete OK Start################# $id= $_GET['id']; $result = mysql_query("DELETE FROM project_data WHERE ID = '$id'") or die ("Error in query: $query. " . mysql_error()); echo '<script>alert("Data Has Been Successfully Updated");</script>'; echo '<meta http-equiv="Refresh" content="0;URL=switch.php">'; } #########Delete OK End ################# break; } ?> <!--HTML--> <?php switch($_GET['action']){ case 'delete'; break; case 'edit'; echo 'Edit page'; ?> <?php $id=$_GET['id']; $result = mysql_query("SELECT * FROM project_data WHERE id='$id'"); $row = mysql_fetch_array($result); ?> <table border = "1"> <form action="switch.php?id=<?php echo $row['ID']?> action=edit_ok" method="post"> <tr> <td>Date Of Birth</td> <td><input type="text" name="dateofbirth" value="<?php echo $row['Date_Of_Birth']?>"/></td> </tr> <tr> <td>Gender</td> <td><input type="radio" name="gender" value="male" <?php if($row['Gender']=='male'){echo 'checked';}?>/> Male<input type="radio" name="gender" value="female" <?php if($row['Gender']=='female'){echo 'checked';}?>/> Female</td> </tr> <tr> <td>Title</td> <td><select name="title"> <option value="">Please Select</option> <option value="Mr" <?php if($row['Title']=='Mr'){echo 'selected';}?>>Mr</option> <option value="Ms" <?php if($row['Title']=='Ms'){echo 'selected';}?>>Ms</option> <option value="Mrs" <?php if($row['Title']=='Mrs'){echo 'selected';}?>>Mrs</option> <option value="Miss" <?php if($row['Title']=='Miss'){echo 'selected';}?>>Miss</option> <option value="Other" <?php if($row['Title']=='Other'){echo 'selected';}?>>Other</option> </select></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="firstname" value="<?php echo $row['First_Name']?>"/></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname" value="<?php echo $row['Last_Name']?>" /></td> </tr> <tr> <td>Address Line 1</td> <td><input type="text" name="address1" value="<?php echo $row['Address_Line_1']?>" /></td> </tr> <tr> <td>Address Line 2</td> <td><input type="text" name="address2" value="<?php echo $row['Address_Line_2']?>" /></td> </tr> <tr> <td>City</td> <td><input type="text" name="postcode" value="<?php echo $row['City']?>" /></td> </tr> <tr> <td>Postcode</td> <td><input type="text" name="postcode" value="<?php echo $row['Postcode']?>" /></td> </tr> <tr> <td>Contact No</td> <td><input type="text" name="contactno" value="<?php echo $row['Contact_No']?>" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" value="<?php echo $row['Email']?>"/></td> </tr> <tr> <td>Additional Info</td> <td><textarea rows="10" cols="30" name="note"><?php echo $row['Additional_Comment']?></textarea></td> </tr> <tr> <td><input type="submit" value="Edit" /></td> <td><a href='switch.php'>Back</a></td> </tr> </table> </form> <?php break; case 'write'; ?> <form action="switch.php?action=write_ok" method="post"> <table> <tr> <td>Date Of Birth</td> <td><input type="text" name="dateofbirth" /></td> </tr> <tr> <td>Gender</td> <td><input type="radio" name="gender" value="male" />Male<input type="radio" name="gender" value="female" /> Female</td> </tr> <tr> <td>Title</td> <td><select name="title"> <option value="" selected="selcted">Please Select</option> <option value="Mr">Mr</option> <option value="Mrs">Mrs</option> <option value="Miss">Miss</option> <option value="Ms">Ms</option> <option value="Other">Other</option> </select></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="firstname" /></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname" /></td> </tr> <tr> <td>Address Line 1</td> <td><input type="text" name="address1" /></td> </tr> <tr> <td>Address Line 2</td> <td><input type="text" name="address2" /></td> </tr> <tr> <td>City</td> <td><input type="text" name="city" /></td> </tr> <tr> <td>Postcode</td> <td><input type="text" name="postcode" /></td> </tr> <tr> <td>Contact No</td> <td><input type="text" name="contactno" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" /></td> </tr> <tr> <td>Additional Info</td> <td><textarea rows="15" cols="30" name="note">Please Enter Any Additional Information</textarea></td> </tr> <tr> <td><input type="submit" value="Submit"></td> <td><a href='switch.php'>Back</a></td> </tr> </table> </form> <?php break; default: echo "<table border='1'> <tr> <th>ID</th> <th>Date Of Birth</th> <th>Gender</th> <th>Title</th> <th>First Name</th> <th>Last Name</th> <th>Address Line 1</th> <th>Address Line 2</th> <th>City</th> <th>Postcode</th> <th>Contact No</th> <th>Email</th> <th>Additional Info</th> <th>Action</th> </tr>"; $result = mysql_query("SELECT * FROM project_data"); while($row = mysql_fetch_array($result)){ echo "<tr>"; echo "<td>" . $row['ID'] . "</td>"; echo "<td>" . $row['Date_Of_Birth'] . "</td>"; echo "<td>" . $row['Gender'] . "</td>"; echo "<td>" . $row['Title'] . "</td>"; echo "<td>" . $row['First_Name'] . "</td>"; echo "<td>" . $row['Last_Name'] . "</td>"; echo "<td>" . $row['Address_Line_1'] . "</td>"; echo "<td>" . $row['Address_Line_2'] . "</td>"; echo "<td>" . $row['City'] . "</td>"; echo "<td>" . $row['Postcode'] . "</td>"; echo "<td>" . $row['Contact_No'] . "</td>"; echo "<td>" . $row['Email'] . "</td>"; echo "<td>" . $row['Additional_Comment'] . "</td>"; echo "<td><a href='switch.php?action=edit&id=" . $row['ID']."'>Edit</a>  <a href='switch.php?action=delete&id=" . $row['ID']."'>Delete</a></td>"; echo "</tr>"; } echo "</table>"; echo "<a href='switch.php?action=write'>Insert</a>"; } mysql_close($con); ?> Here is the completed code, but I still have a problem the page is coming up with an error: ( ! ) Fatal error: Cannot break/continue 1 level in C:\wamp\www\p1\switch.php on line 63 Call Stack # Time Memory Function Location 1 0.0009 413960 {main}( ) ..\switch.php:0 I cannot seem to figure it out, Can anyone help? Thanks Here is my dilemma. I'm trying to get the "class name" to return instead of the number that is associated with it. I wrote a switch so that each of the 16 cases would be associated with what the class name is. However, how do I make it output into my statement? Here is what I have so far: function getClassByNumber($x){ switch($x){ case 1: return "Alchemist"; case 2: return "Assassin"; case 3: return "Dark Arts"; case 4: return "Dark Paladin"; case 5: return "Entertainer"; case 6: return "Hunter"; case 7: return "Mage"; case 8: return "Monk"; case 9: return "Paladin"; case 10: return "Pirate"; case 11: return "Priest"; case 12: return "Psion"; case 13: return "Scholar"; case 14: return "Thief"; case 15: return "Warlock"; case 16: return "Warrior"; } } $class = getClassByNumber($username->class); echo You are now a $class. Username is defined as seeking the table I want. Class is the field where the number is stored. Thanks! Code: [Select] <?php switch($_GET['action']){ case 'delete': break; case 'edit': case 'write': if($action=='edit'){ $form_action = 'edit_ok'; echo 'Edit Page'; }else{ $form_action = 'write_ok'; echo 'Write Page'; } ?> <?php $id=$_GET['id']; $result = mysql_query("SELECT * FROM project_data WHERE id='$id'"); $row = mysql_fetch_array($result); ?> <table border = "1"> <form action="switch.php?id=<?php echo $row['ID']?>&action=[color=purple][b]<?php echo $action ?>[/b][/color]" method="post"> <tr> <td>Date Of Birth</td> <td><input type="text" name="dateofbirth" value="<?php echo $row['Date_Of_Birth']?>"/></td> </tr> <tr> <td>Gender</td> <td><input type="radio" name="gender" value="male" <?php if($row['Gender']=='male'){echo 'checked';}?>/> Male<input type="radio" name="gender" value="female" <?php if($row['Gender']=='female'){echo 'checked';}?>/> Female</td> </tr> <tr> <td>Title</td> <td><select name="title"> <option value="">Please Select</option> <option value="Mr" <?php if($row['Title']=='Mr'){echo 'selected';}?>>Mr</option> <option value="Ms" <?php if($row['Title']=='Ms'){echo 'selected';}?>>Ms</option> <option value="Mrs" <?php if($row['Title']=='Mrs'){echo 'selected';}?>>Mrs</option> <option value="Miss" <?php if($row['Title']=='Miss'){echo 'selected';}?>>Miss</option> <option value="Other" <?php if($row['Title']=='Other'){echo 'selected';}?>>Other</option> </select></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="firstname" value="<?php echo $row['First_Name']?>"/></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname" value="<?php echo $row['Last_Name']?>" /></td> </tr> <tr> <td>Address Line 1</td> <td><input type="text" name="address1" value="<?php echo $row['Address_Line_1']?>" /></td> </tr> <tr> <td>Address Line 2</td> <td><input type="text" name="address2" value="<?php echo $row['Address_Line_2']?>" /></td> </tr> <tr> <td>City</td> <td><input type="text" name="postcode" value="<?php echo $row['City']?>" /></td> </tr> <tr> <td>Postcode</td> <td><input type="text" name="postcode" value="<?php echo $row['Postcode']?>" /></td> </tr> <tr> <td>Contact No</td> <td><input type="text" name="contactno" value="<?php echo $row['Contact_No']?>" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" value="<?php echo $row['Email']?>"/></td> </tr> <tr> <td>Additional Info</td> <td><textarea rows="10" cols="30" name="note"><?php echo $row['Additional_Comment']?></textarea></td> </tr> <tr> <td><input type="submit" value="Update" /></td> <td><a href='switch.php'>Main Page</a></td> </tr> </table> </form> <?php break; //case 'write'; ?> <!--<table border = "1"> <form action="switch.php?action=write_ok" method="post"> <table> <tr> <td>Date Of Birth</td> <td><input type="text" name="dateofbirth" /></td> </tr> <tr> <td>Gender</td> <td><input type="radio" name="gender" value="male" />Male<input type="radio" name="gender" value="female" /> Female</td> </tr> <tr> <td>Title</td> <td><select name="title"> <option value="" selected="selcted">Please Select</option> <option value="Mr">Mr</option> <option value="Mrs">Mrs</option> <option value="Miss">Miss</option> <option value="Ms">Ms</option> <option value="Other">Other</option> </select></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="firstname" /></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname" /></td> </tr> <tr> <td>Address Line 1</td> <td><input type="text" name="address1" /></td> </tr> <tr> <td>Address Line 2</td> <td><input type="text" name="address2" /></td> </tr> <tr> <td>City</td> <td><input type="text" name="city" /></td> </tr> <tr> <td>Postcode</td> <td><input type="text" name="postcode" /></td> </tr> <tr> <td>Contact No</td> <td><input type="text" name="contactno" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" /></td> </tr> <tr> <td>Additional Info</td> <td><textarea rows="15" cols="30" name="note">Please Enter Any Additional Information</textarea></td> </tr> <tr> <td><input type="submit" value="Submit"></td> <td><a href='switch.php'>Back</a></td> </tr> </table> </form>--> <?php //break; I have this code, which is basically a form for inputting and another for editing. As you can see I have made one part a comment as I want just the edit part to control the input and edit, which is shown at the very beginning with write case, and the seperate edit case, all I need to complete it is the part that I have highlighted in purple/bold. Does anybody have any help? Hello again having trouble to switch pages All it dose just refrashe page <a href=/?p=cinema&pid=503&act=video>Test</a> Quote $keiciam = isset($_GET['act']) ? $_GET['act']:''; switch($keiciam) { case 'video': eval("\$cinema_player = \"".$TSUE['TSUE_Template']->LoadTemplate('cinema_player')."\";"); break; default: } So, I'm working on making my code a little prettier. Previously, if I wanted to do 3 different things, I would create 3 different php pages for those three different things. For Example: <form action="user_add.php"> <form action="user_edit.php"> <form action="user_delete.php"> This would usually be 6 different pages. (3 "gui" pages and 3 php pages that performed specific actions) So, I've decided to create 1 specific page called "actions.php" Using a switch.case variable named $request This will allow me to achieve the following: <form action="actions.php?request=userAdd"> <form action="actions.php?request=userEdit"> <form action="actions.php?request=userDelete"> My actions page looks like this: Code: [Select] <?php include("connectDB.php"); $request=$_GET['request']; switch ($request) { case "userDelete": echo "delete"; break; case "userEdit": echo "edit"; break; case "userAdd": echo "add"; break; } ?> Now, assume I access the page that allows me to add a new user. The form action would look like this: <form action=actions.php?request=userAdd> When I click on the submit button, I would see the words "add" on the page. Perfect, good job, yay me. Now, assume I add the following code block into the "userAdd" case Code: [Select] <?php ... case "userAdd": // Get values from form $username=$_POST['username']; $password=$_POST['password']; $email=$_POST['email']; // Insert data into mysql $sql="INSERT INTO $tbl_users(username, password, email)VALUES('$username', '$password', '$email')"; $result=mysql_query($sql); // if successfully insert data into database, displays message "Successful". if($result) { echo "Successful"; } else { echo "ERROR"; } echo $previous_page; break; ?>This works perfectly. Good job, yay me! Now assume I change the case block to look like this: Code: [Select] <?php ... case "userAdd": adduser(); break; } function adduser() { // Get values from form $username=$_POST['username']; $password=$_POST['password']; $email=$_POST['email']; // Insert data into mysql $sql="INSERT INTO $tbl_users(username, password, email)VALUES('$username', '$password', '$email')"; $result=mysql_query($sql); // if successfully insert data into database, displays message "Successful". if($result) { echo "Successful"; } else { echo "ERROR"; } echo $previous_page; } ?>This doesn't work. WHY? If I change the adduser() function to echo "hello world"; I would see "hello world", but when I attempt to copy and paste the block of code (that works in the switch case statement) into the function, it stops working. (It prints nothing) Now, I don't even know if this is the correct place to put this. Last time I had a mysql database error and my thread was moved here, even though I knew it wasn't a php error. So, if this is the wrong section, I apologize - but I assume it falls under both a mysql and php problem. Hello everyone from a php newbie, I have a option selections box and need to select an action depending on the results. Its for an insert script for items into a Database and depending on the chosen option it add the picture to the DB table The selection options are <select name="extension" style="font-size:18px"><option value="pdf">PDF</option><option value="zip">ZIP</option><option value="powerpoint">PowerPoint</option></select> the Swtich is switch $_POST['extention']{ case 'zip': echo $insert_image = $_POST['extension'].'winzip.gif'; break; case 'powerpoint': echo $insert_image = $_POST['extension'].'ppt.gif'; break; default: echo $insert_image = $_POST['extension'].'products/pdf.gif'; } Would someone please tell me if this is correct syntax? Or have i got it horribly wrong! Thank you for your a help Charlie |