Jump to content

switch statement help


mayman212

Recommended Posts

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:

 

<!--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>&nbsp&nbsp<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 are:

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.

Link to comment
Share on other sites

Ive got this far, but I'm now stuck on the edit and delete parts. When I click edit it redirects me to the form but it is blank, I want it to be filled with the record that I clicked the edit button on. And also help on the delete part would also be helpful. Thanks

 


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


   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());

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 'delete_ok':
#########Delete OK Start#################






#########Delete OK End #################
break;
}
?>

<!--HTML-->

<?php

switch($_GET['action']){
case 'delete';

break;
case 'edit';
	echo 'Edit page';

	?>

                <table border = "1">
			<form action="switch.php?id=<?php echo $row['ID']?>" method="post" 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='display.php'>List</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>&nbsp&nbsp<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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.