Jump to content

[SOLVED] Changing and Deleting Entries in a MySQL Database


Grizzly13

Recommended Posts

Hi Guys

 

I am having a slight problem with getting the code right for an admin person to be able to activate / Freeze and account or deleting the user from the database completely. I have found some script which is meant to do the job, but unfortunately it does work. The script seems to use javascript to perform the action of changing and deleting. I am rather new at this, but is it possible to use PHP instead of java?

 

I have attached the code for the management page.

 

Your help will be much appreciated.

 

Grizzly

 

 


<?php


		if ((!empty ($_SESSION['username'])) && ($_SESSION['admin'] == '1'))

		{





			echo"

			<table border='0%' width='100%' height='100%' cellspacing='0%' cellpadding='0%' align='center'>




				<tr>

					<td valign='top'>



					<br>

					<p>";

						include('connect.php');



						$query = ("SELECT * FROM users where admin = '0' ORDER BY id desc");

						$result = mysql_query($query) or die(mysql_error());

						$num_results = mysql_num_rows($result);

						$row_count = 0;

						for($i=0;$i<$num_results; $i++)

						{

							$row = mysql_fetch_array($result);



							if($row['status'] == '1')

								$style='color: green';

							else

								$style='color: red';



							print "<table border='0%' width='70%' cellspacing='0%' cellpadding='0%' align='center'>

									<tr valign='top'>

										<td width='60%' align='Justify'>

											<a href='javascript://' onclick=\"detail(" . $row["id"] . ")\"'  style='". $style ."'>

												<b>" . ucfirst($row['firstname']) . "</b>

											</a>

										</td>



										<td width='*%' align='left'>

											<a href='Manage_Profile.php' class='blackText' style='". $style ."'

											onclick=\"permission(" . $row["id"] . "," . $row["status"] . ")\">

												";

												if($row['status'] == '1')

													print "<b>Freeze Account</b>";

												else

													print "<b>Activate Account</b>";

											echo "

											</a>

										</td>



										<td width='*%' align='right'>";

												if($row['status'] == '1')

												{

													print "

													<a href='Manage_Profile.php' class='greyText'

													onclick='alert(\"You must freeze this account first.\")'>

														<b>Delete Account</b>

													</a>";

												}

												else

												{

													print "

													<a href='Manage_Profile.php' class='blackText' onclick=\"del(" . $row["id"] . ")\">

														<b>Delete Account</b>

													</a>";

												}

											echo "



										</td>

									</tr>

								</table>

								<hr width='70%'>";

								$row_count++;

						}



					echo "

					</td>

				</tr>

			</table>";





		}

?>

 

 

Link to comment
Share on other sites

but unfortunately it does work

 

Damn, a script that works. I can only think of how you now would feel ;)

 

The script seems to use javascript... possible to use PHP instead of java

 

Javascript and Java are 2 different things. JavaScript is a scripting-language which means it's statements are being interpreted by the browser (which means that the browser keeps an association table for each statement and just swaps it with micro-code) while Java code is being translated (using a compiler) into executable code (1's and 0's). This may be not entirely correct and I'm sure someone with a more proper knowledge can tell you the ins and outs.

 

Now to answer your question: Yes you can use just plain php to perform these actions. I do recommend using something else then numbers to indicate status. You may want to start looking at design patterns, active record more precise which would allow you to do somthing like:

 

<?php
class User
{
    const STATE_PENDING = 'pending';
    const STATE_ACTIVATED = 'activated';
    const STATE_FROZEN = 'frozen';
    //depending if the account remains in the system or not STATE_DELETED may be added
    
    protected $_data = array();
    
    public function isPending() {
          return $_data['state'] === self::STATE_PENDING;
    }
    
    ...you get the idea
}
?>

 

You can then use this as:

<?php
while ($row = mysql_fetch_assoc($result)) {
    $users[] = new User($row);
}

foreach ($users as $user) {
    if ($user->isPending()) {
        ..logic..
    }
}
?>

 

The same applies for login:

<?php
// login query
if (mysql_num_rows($result) == 1) {
    $row = mysql_fetch_assoc($result);
    $user = new User($row);
    if ($user->isPending()) {
        //sorry, wait for your account to be activated or activate your account (if using e-mail activation)
    }
    if ($user->isFrozen()) {
        //sorry, your account has been frozen
    }
}
?>

 

 

Link to comment
Share on other sites

Thanks for the help man, but unfortunately the whole website is based around using "0" and "1". Perhaps the best way forward is to to determine why the current script is not working in this website. I took the code from an example website which I was given and it works perfectly there. I have probably not included something into the new webpage which is needed of something.

 

I have attached the script for both website pages, if you could help me that would be great.

 

Thanks

 

 

Example Website:


<?php



ob_start();

session_start();



if (empty($_SESSION['username']))

{

header('location: index.php');

}

else

{

	if ((!empty ($_SESSION['username'])) && ($_SESSION['admin'] == '0') && ($_SESSION['status'] == '1'))

{

	header('location: userInbox.php');

}

else

{

	if ((!empty ($_SESSION['username'])) && ($_SESSION['admin'] == '0') && ($_SESSION['status'] == '0'))

	{

		header('location: index.php');

	}

	else

	{

		if ((!empty ($_SESSION['username'])) && ($_SESSION['admin'] == '1'))

		{

			include('topHalf.php');



			echo"

			<table border='0%' width='100%' height='100%' cellspacing='0%' cellpadding='0%' align='center'>

				<tr height='5%'>

					<td valign='top'>

						<table border='0%' width='100%' height='100%' cellspacing='0%' cellpadding='0%' align='center'>

							<tr>

								<td align='center' class='blackText'>

									<b><h2>Hello " . $_SESSION['firstname'] . "</h2></b>

								</td>

							</tr>";

							include('menu.php');

						echo "</table>

					</td>

				</tr>



				<tr>

					<td valign='top'>

					<hr width='80%'>

					<br>

					<p>";

						include('connect.php');



						$query = ("SELECT * FROM tblusers where admin = '0' ORDER BY id desc");

						$result = mysql_query($query) or die(mysql_error());

						$num_results = mysql_num_rows($result);

						$row_count = 0;

						for($i=0;$i<$num_results; $i++)

						{

							$row = mysql_fetch_array($result);



							if($row['status'] == '1')

								$style='color: green';

							else

								$style='color: red';



							print "<table border='0%' width='70%' cellspacing='0%' cellpadding='0%' align='center'>

									<tr valign='top'>

										<td width='60%' align='Justify'>

											<a href='javascript://' onclick=\"detail(" . $row["id"] . ")\"' class='tab' style='". $style ."'>

												<b>" . ucfirst($row['organization']) . "</b>

											</a>

										</td>



										<td width='*%' align='left'>

											<a href='manageClientele.php' class='blackText' style='". $style ."'

											onclick=\"permission(" . $row["id"] . "," . $row["status"] . ")\">

												";

												if($row['status'] == '1')

													print "<b>Freeze Account</b>";

												else

													print "<b>Activate Account</b>";

											echo "

											</a>

										</td>



										<td width='*%' align='right'>";

												if($row['status'] == '1')

												{

													print "

													<a href='manageClientele.php' class='greyText'

													onclick='alert(\"You must freeze this account first.\")'>

														<b>Delete Account</b>

													</a>";

												}

												else

												{

													print "

													<a href='manageClientele.php' class='blackText' onclick=\"del(" . $row["id"] . ")\">

														<b>Delete Account</b>

													</a>";

												}

											echo "



										</td>

									</tr>

								</table>

								<hr width='70%'>";

								$row_count++;

						}



					echo "

					</td>

				</tr>

			</table>";



			include('bottomHalf.php');

		}

	}

}

}



ob_end_flush();

?>

 

 

My Website:


<?php

ob_start();
session_start();



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


<head>



<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="robots" content="index, follow">
<meta name="description" content="Manufacturer of Razor Wire, Razor Wire Mesh, Razor Wire with Electrocoil, Razor Wire Flatwrap Coil, Razor Ribbon Strip, Wall Spikes, Barbed Tape, ClearVu Security Fencing and Typical Razor Wire Fencing">
<meta name="keywords" content="razor wire fence,razor wire mesh,razor wire security,barbed tape,loading ramps,wallspikes,razor wire flatwrap coil,security fence systems,clearvu,358,invisible wall,fencing,wire,cochrane,betafence,betafencing south africa,fence,358 mesh,clearvu invisible wall,high security fencing system">



<title>Cochrane Steel | News & Events - Razor Wire, Razor Wire Mesh, Razor Wire Fence, Razor Wire Fence, Razor Wire Mesh, Razor Wire Security, Barbed Tape, Loading Ramps, Wallspikes, Razor Wire Flatwrap Coil, Security Fence Systems</title>




<link rel="stylesheet" href="css/Stylesheet.css" type="text/css" media="screen" charset="utf-8" />
	<link rel="stylesheet" href="css/jd.gallery.css" type="text/css" media="screen" charset="utf-8" />
	<script src="scripts/mootools-1.2.1-core-yc.js" type="text/javascript"></script>
	<script src="scripts/mootools-1.2-more.js" type="text/javascript"></script>
	<script src="scripts/jd.gallery.js" type="text/javascript"></script>
	<script src="scripts/jd.gallery.transitions.js" type="text/javascript"></script>
	<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>


</head>

<body>
<table width="960" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="48" height="145" rowspan="2"> </td>
    <td height="145" colspan="2" rowspan="2"><img src="Images/Cochrane_Products_Logo.png" alt="Cochrane Steel Products Logo" width="209" height="89" /></td>
    <td width="54" height="145" rowspan="2"> </td>
    <td height="50" colspan="2" valign="top"><div align="right">
      <? 
include ("menu.php");

?>
    </div></td>
    <td width="48" height="145" rowspan="2"> </td>
  </tr>
  <tr>
    <td height="95" colspan="2" valign="top"><div align="right">
      <h1><span class="topbodycopy">Manufacturers of Perimeter Security Barriers</span><span class="topheading"><br />
        COCHRANE NEWS AND EVENTS</span></h1>
    </div></td>
  </tr>
  <tr>
    <td width="48"> </td>
    <td colspan="5" valign="top"><div align="right">
      <div id="main">
        <p>  </p>
        <div id="mainlinks">
          <table width="330" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td height="30" valign="top" bgcolor="#FFFFFF"><table width="315" border="0" align="right" cellpadding="0" cellspacing="0">
                <tr>
                  <td height="20" valign="middle" bgcolor="#FFFFFF" class="linehorizontal"><div align="right" class="linksmain"> <a href="index.php">HOME</a>  |  <a href="Cochrane_Steel_Profile.php">PROFILE </a>|   <a href="Cochrane_Steel_Products.php">PRODUCTS</a>   |   <a href="Cochrane_Steel_News.php">NEWS</a>   |   <a href="Contact_Cochrane_Steel.php">CONTACT US</a></div></td>
                </tr>
              </table></td>
            </tr>
          </table>
        </div>
        <script type="text/javascript">
		function startGallery() {
			var myGallery = new gallery($('myGallery'), {
				timed: true,
				defaultTransition: "fadeslideleft"
			});
		}
		window.addEvent('domready', startGallery);
	</script>
        <table width="864" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td> </td>
          </tr>
        </table>
      </div>
    </div></td>
    <td width="48"> </td>
  </tr>
  <tr>
    <td width="48"> </td>
    <td colspan="5"> </td>
    <td width="48"> </td>
  </tr>
  <tr>
    <td> </td>
    <td colspan="5">
    
    
    <table width="864" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
    
    <?php


		if ((!empty ($_SESSION['username'])) && ($_SESSION['admin'] == '1'))

		{





			echo"

			<table border='0%' width='100%' height='100%' cellspacing='0%' cellpadding='0%' align='center'>




				<tr>

					<td valign='top'>



					<br>

					<p>";

						include('connect.php');



						$query = ("SELECT * FROM users where admin = '0' ORDER BY id desc");

						$result = mysql_query($query) or die(mysql_error());

						$num_results = mysql_num_rows($result);

						$row_count = 0;

						for($i=0;$i<$num_results; $i++)

						{

							$row = mysql_fetch_array($result);



							if($row['status'] == '1')

								$style='color: green';

							else

								$style='color: red';



							print "<table border='0%' width='70%' cellspacing='0%' cellpadding='0%' align='center'>

									<tr valign='top'>

										<td width='60%' align='Justify'>

											<a href='javascript://' onclick=\"detail(" . $row["id"] . ")\"'  style='". $style ."'>

												<b>" . ucfirst($row['firstname']) . "</b>

											</a>

										</td>



										<td width='*%' align='left'>

											<a href='Manage_Profile.php' class='blackText' style='". $style ."'

											onclick=\"permission(" . $row["id"] . "," . $row["status"] . ")\">

												";

												if($row['status'] == '1')

													print "<b>Freeze Account</b>";

												else

													print "<b>Activate Account</b>";

											echo "

											</a>

										</td>



										<td width='*%' align='right'>";

												if($row['status'] == '1')

												{

													print "

													<a href='Manage_Profile.php' class='greyText'

													onclick='alert(\"You must freeze this account first.\")'>

														<b>Delete Account</b>

													</a>";

												}

												else

												{

													print "

													<a href='Manage_Profile.php' class='blackText' onclick=\"del(" . $row["id"] . ")\">

														<b>Delete Account</b>

													</a>";

												}

											echo "



										</td>

									</tr>

								</table>

								<hr width='70%'>";

								$row_count++;

						}



					echo "

					</td>

				</tr>

			</table>";





		}

?>
    
    
    </td>
  </tr>
</table>
    
    
    </td>
    <td width="48"> </td>
  </tr>
  <tr>
    <td> </td>
    <td colspan="5"> </td>
    <td width="48"> </td>
  </tr>
  <tr valign="top">
    <td width="48" height="200" rowspan="2"> </td>
    <td height="200" colspan="5"><table width="864" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="1" height="150" valign="top"><br/>
            <table width="1" height="46" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td height="46" valign="top" class="linetile"> </td>
              </tr>
            </table>
          <h2> </h2></td>
        <td width="186" height="150" valign="top"><h1><span class="linksheading"> LANDSIDE PERIMETER BARRIERS</span></h1>
            <p class="links"> - <a href="Why_Ripper_Razor.php">Why Ripper / Razor?</a><br />
               - <a href="Razor_Wire_Fence.php">Razor Wire</a><br />
               - <a href="Razor_Mesh_Fence.php">Welded Razor Mesh</a><br />
               - <a href="Ribbon_Mesh_Fence.php">Ribbon Mesh</a><br />
               - <a href="ClearVu_Fence_Invisible_Wall.php">ClearVu Fencing Mesh</a><br />
               - <a href="ClearVu_Reinforced_Mesh_Fence.php">ClearVu Reinforced Mesh</a><br />
               - <a href="Parkland_Mesh_Fence.php">Parkland Mesh</a></p></td>
        <td width="1" height="150" valign="top"><br />
            <table width="1" height="124" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td height="124" valign="top" class="linetile"> </td>
              </tr>
          </table></td>
        <td width="171" height="150" valign="top"><h1><span class="linksheading">MARINE PERIMETER BARRIERS</span></h1>
            <p class="links">  - <a href="Antipersonnel_Boat_Floating_Barrier.php">Anti Personnel / Boat Barrier</a></p></td>
        <td width="1" height="150" valign="top"><br />
            <table width="1" height="58" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td height="58" valign="top" class="linetile"> </td>
              </tr>
          </table></td>
        <td width="176" height="150" valign="top"><h1><span class="linksheading"> RAPID DEPLOYMENT BARRIERS</span></h1>
            <p class="links"> - <a href="SD_300_Rapid_Deployment_Perimeter.php">SD 300 Barrier</a><br />
               - <a href="HD_Rapid_Deployment_Perimeter.php">HD 300 Barrier</a><br />
               - <a href="SD_600_Rapid_Deployment_Perimeter.php">SD 600 Barrier</a></p></td>
        <td width="1" height="150" valign="top"><br />
            <table width="1" height="84" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td height="84" valign="top" class="linetile"> </td>
              </tr>
          </table></td>
        <td width="159" height="150" valign="top"><h1><span class="linksheading"> PERIMETER ENHANCEMENTS</span></h1>
            <p class="links"> - <a href="Ribbon_Strip_Wall_Topping.php">Ribbon Strip</a><br />
               - <a href="Consertina_Coil_Wall_Topping.php">Concertina Coil</a><br />
               - <a href="Wall_Spike_Toppings.php">Wall Spikes</a><br />
               - <a href="Flat_Wrap_Wall_Topping.php">Flat Wrap</a></p></td>
        <td width="1" height="150" valign="top"><br />
            <table width="1" height="121" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td height="121" valign="top" class="linetile"> </td>
              </tr>
          </table></td>
        <td width="122" height="150" valign="top"><h1><span class="linksheading">OTHER PRODUCTS</span></h1>
            <p class="links"> - <a href="Electrical_Cable_Trays.php">Cable Trays</a><br />
               - <a href="Razor_Wire_Clips.php">Clips</a></p>
          <h1><span class="linksheading">LOGISTICS</span></h1>
          <p class="links">  - <a href="Mobile_Logistics_Loading_Ramp.php">Mobile Loading Ramp</a><br />
             - <a href="Forklift_Trailer.php">Forklift Trailer</a></p></td>
      </tr>
    </table></td>
    <td width="48" height="200" rowspan="2"> </td>
  </tr>
  <tr valign="top">
    <td colspan="5"><table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td colspan="5" valign="top"><p align="center"><span class="topbodycopy">Copyright © 2009 Cochrane Steel Products (Pty) Ltd</span></p></td>
        <td> </td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td width="48"> </td>
    <td width="144"> </td>
    <td width="144"> </td>
    <td> </td>
    <td width="378"> </td>
    <td width="144"> </td>
    <td width="48"> </td>
  </tr>
</table>
</body>
</html>
<?php

ob_end_flush();

?>

 

 

 

Link to comment
Share on other sites

if (empty($_SESSION['username'])) {
   header('location: index.php');
} else { // directly implies $_SESSION['username'] is not empty (if it was this wouldn't execute)
    if ((!empty ($_SESSION['username'])) && ($_SESSION['admin'] == '0') && ($_SESSION['status'] == '1'))

 

if ((!empty ($_SESSION['username'])) && ($_SESSION['admin'] == '0') && ($_SESSION['status'] == '1'))
if ((!empty ($_SESSION['username'])) && ($_SESSION['admin'] == '0') && ($_SESSION['status'] == '0'))
if ((!empty ($_SESSION['username'])) && ($_SESSION['admin'] == '1'))

 

Can be simplified to:

if (!empty($_SESSION['username'])) {
    if (!$_SESSION['admin']) {// !0 = true
        if (!$_SESSION['status']) {// status=0
            header('Location: index.php');
        } else {// status=1
            header('Location: userInbox.php');
        }
    } else {// admin=1
        include('topHalf.php');
        echo '';
    }
} else {// $_SESSION['username']=empty
    header('Location: index.php');
}

 

Which can be directly transformed to:

if (empty($_SESSION['username']) || ($_SESSION['admin'] == FALSE && $_SESSION['status'] == FALSE)) {
    header('Location: index.php');
}

if ($_SESSION['admin'] == FALSE && $_SESSION['status'] == TRUE) {
    header('Location: userInbox.php');
}

if ($_SESSION['admin'] == TRUE) {
    include('topHalf.php');
    echo '';
}

 

This should help you simplify your script and will make it easier to spot errors more easily.

 

To simplify it further, you'd get:

if (empty($_SESSION['username']) || (empty($_SESSION['admin']) && empty($_SESSION['status']))) {
    header('Location: index.php');
}

if (empty($_SESSION['admin']) && $_SESSION['status'] == TRUE) {
    header('Location: userInbox.php');
}

if ($_SESSION['admin'] == TRUE) {
    include('topHalf.php');
    echo '';
}

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.