Page 1 of 1

my attempt at an all in one script is failing, please help

Posted: Fri Nov 26, 2004 7:28 pm
by phase
hi all

i have tried to write a script which does all the following on one page,

grab all my users
grab individual records
add / edit / delete these records

the script ive wrote does everything but edit, it just wont do it, ive been looking for hours to see why and ive got to the point where i just dont know or ive looked for too long i aint loking right,

i hope somebody could look over this and tell me where ie gone wrong,

here is te code:

Code: Select all

<?
include "includes/Pagination.php";
include "includes/head.html";
include "includes/theme.php";
include "includes/dbinfo.inc.php";
$title="Users";
openbody($title);
opentable();
include "includes/adminnav.php";

///////////////////checking to see if submit (edit/insert) has been clicked////////////////////////////
if ($submit) {
// here if no ID then adding else we're editing
if ($edit) {
$sql = "UPDATE members SET username='$username',password='$password', email='$email', gid='$gid'
WHERE uid=$edit";
echo $_POST["username"];?></b> has been edited.<hr noshade color="#C0C0C0" size="1">
<? echo"Click <a href="admin.php">to go back to admin section.";
} else {
$sql = "INSERT INTO members (uid, username, password, email, gid) VALUES
('$uid', '$username', '$password', '$email', '$gid')";
$result = mysql_query($sql);
echo $_POST["username"];?></b> has been added.<hr noshade color="#C0C0C0" size="1">
<? echo"Click <a href="admin.php">to go back to admin section.";
}
}

/////////grabbing a single record/////////////////////////////////////////////////////
elseif ($userid) {
$pagination = new EasyPagination($page,4,"eng"); 
$sqlSearch  = "SELECT uid,username,email,gid FROM members WHERE uid=$userid";
$pagination->setSQLSearch($sqlSearch);
$SQLresult = $pagination->getResultData($page);	
if (mysql_num_rows($SQLresult)>=1):
while (list($uid,$username,$email,$gid) = mysql_fetch_row($SQLresult)):
echo "<tr>
<td>$username</td>
<td>$email</td>
<td>$gid</td>
<td><a href="$PHPSELF?edit=$uid">Edit</td>
<td><a href="$PHPSELF?deletecheck=$uid">Delete</td>
</tr>";
endWhile;
endIf;
}


///////////////////////////////////editing a record///////////////////////////////////
elseif ($edit) {
$pagination = new EasyPagination($page,4,"eng"); 
$sqlSearch  = "SELECT uid,username,password,email,gid FROM members WHERE uid=$edit";
$pagination->setSQLSearch($sqlSearch);
$SQLresult = $pagination->getResultData($page);	
if (mysql_num_rows($SQLresult)>=1):
while (list($uid,$username,$password,$email,$gid) = mysql_fetch_row($SQLresult)):
?>
<td><form method="post" action="<? echo $PHPSELF ?>">
<input type=hidden name="uid" value="<?php echo $uid ?>">
<table border="0" width="100%">
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>
	<tr>
		<td background="images/ppd_15.jpg" colspan="2">
		Edit <? echo $title ?></td>
	</tr>
	<tr>
		<td width="23%">UserName: </td>
		<td width="76%">
		<input type="text" name="username" value="<? echo $username ?>" size="20" style="font-size: 10px; color: #000000; border: 1px solid #C0C0C0; background-color: #808080"></td>
	</tr>
	<tr>
		<td width="23%">Password</td>
		<td width="76%">
		<input type="password" name="password" value="<? echo $password ?>"size="20" style="font-size: 10px; color: #000000; border: 1px solid #C0C0C0; background-color: #808080"></td>
	</tr>
	<tr>
		<td width="23%">Email</td>
		<td width="76%">
		<input type="text" name="email" value="<? echo $email ?>" size="20" style="font-size: 10px; color: #000000; border: 1px solid #C0C0C0; background-color: #808080"></td>
	</tr>
	<tr>
		<td width="23%">Group Id</td>
		<td width="76%">
		<input type="text" name="gid" value="<? echo $gid ?>" size="20" style="font-size: 10px; color: #000000; border: 1px solid #C0C0C0; background-color: #808080"></td>
	</tr>


	<tr>
		<td width="99%" colspan="2">
		<p align="center">
		<input type="submit" name="submit" value="Edit <? echo $title ?>" style="border:1px solid #808080; font-size:10px; color:#000000; background-color:#808080"></td>
	</tr>
</table>
<?
endWhile;
endIf;
}

////////////////////////////////adding a record/////////////////////////////////////
elseif ($add) {
?>
<td><form method="post" action="<? echo $PHPSELF ?>">
<input type=hidden name="uid" value="<?php echo $uid ?>">
<table border="0" width="100%">
	<tr>
		<td colspan="2">&nbsp;</td>
	</tr>
	<tr>
		<td background="images/ppd_15.jpg" colspan="2">
		Add <? echo $title ?></td>
	</tr>
	<tr>
		<td width="23%">UserName: </td>
		<td width="76%">
		<input type="text" name="username" size="20" style="font-size: 10px; color: #000000; border: 1px solid #C0C0C0; background-color: #808080"></td>
	</tr>
	<tr>
		<td width="23%">Password</td>
		<td width="76%">
		<input type="password" name="password" size="20" style="font-size: 10px; color: #000000; border: 1px solid #C0C0C0; background-color: #808080"></td>
	</tr>
	<tr>
		<td width="23%">Email</td>
		<td width="76%">
		<input type="text" name="email" size="20" style="font-size: 10px; color: #000000; border: 1px solid #C0C0C0; background-color: #808080"></td>
	</tr>


	<tr>
		<td width="23%">Group Id</td>
		<td width="76%">
		<select size="1" name="gid" style="font-size: 10px; color: #000000; border: 1px solid #C0C0C0; background-color: #808080">
		<option>1</option>
		<option selected>2</option>
		<option>3</option>
		</select></td>
	</tr>
	<tr>
		<td width="99%" colspan="2">
		<p align="center">
		<input type="submit" name="submit" value="Add <? echo $title ?>" style="border:1px solid #808080; font-size:10px; color:#000000; background-color:#808080"></td>
	</tr>
</table>
<?
}

///////////////////////////////deleting a record////////////////////////////////////
elseif ($deletecheck) {
$sql = "SELECT * FROM members WHERE uid=$deletecheck";
$result = mysql_query($sql);
echo"<td>Are you sure you want to delete this $title? <td><a href="$PHPSELF?delete=$deletecheck"> Yes</a></td><td><a href="$PHPSELF?edit=$uid"> No</a></td>";
}
elseif ($delete) {
$sql = "DELETE FROM members WHERE uid=$delete";
$result = mysql_query($sql);
echo "<td>$sql Record deleted! Click <a href="admin.php">here</a> to go back to admin section.<p>";
}

/////////////////////////////main page/////////////////////////////////////////////
else {
if ($_GET["page"]!=""):		$page	= $_GET["page"];	else:	$page	= 1;		endif;
$pagination = new EasyPagination($page,4,"eng"); 
$sqlSearch  = "SELECT uid,username,email,gid FROM members";
$pagination->setSQLSearch($sqlSearch);
$sqlNumRows = "SELECT COUNT(uid) AS numTotal FROM members";
$pagination->setSQLNumRows($sqlNumRows);
$SQLresult = $pagination->getResultData($page);	
echo "Page: <font color='#CCCC66'>$page</font> >> Showing: ".$pagination->getListCurrentRecords()."<br>";
echo "Result: <font color='#CCCC66'>".$pagination->getTotalRecords()."</font> occurrence(s) in <font color='#CCCC66'>".$pagination->getPagesFound()."</font> page(s)<br>";
echo "<br>";
echo $pagination->getNavigation()."<br><br>";
echo $pagination->getCurrentPages();
echo "<br><br>";
echo $pagination->getNavigationGroupLinks();
if (mysql_num_rows($SQLresult)>=1):
echo"<td colspan=3><a href="$PHPSELF?add=1">Add new $title<hr></td>";
?><td colspan=3><?
while (list($uid,$username,$email,$gid) = mysql_fetch_row($SQLresult)):
echo "<tr>
<td><a href="$PHPSELF?userid=$uid">$username</td>
<td>$email</td>
<td>$gid</td>
</tr>";
endWhile;
endIf;
}
closetable();
closebody();
include "includes/footer.html";
?>

Posted: Fri Nov 26, 2004 8:02 pm
by hawleyjr
Is there a particular reason why you are trying to do this all in one page...? IMHO I'd break it up.

Also, what errors are you getting? what is it doing or not doing?

Posted: Fri Nov 26, 2004 8:08 pm
by phase
theres no particular reason for doing it on one page apart from knowing where everything is,

i am not recieving any errors whatsoever unfortunately, the problem is when i edit a record and click submit, it just doesnt work, it goes to the confirmation page which says its been edited, but once i click the users again, its still there unedited :(

Posted: Sat Nov 27, 2004 9:10 am
by phase
please please does anybody have any advice / solutions / suggestions on this matter?

i been going over it and over it and im lost.

Posted: Sat Nov 27, 2004 9:21 am
by kral_majales
if i were you, i'd break it up, then try and fix the problem, then put it all back together when it works....

Posted: Sat Nov 27, 2004 9:33 am
by hawleyjr
I'm headed out the door so I don't have time to go line by line through your code. but try this:

Put an

Code: Select all

echo "1";
and then an

Code: Select all

echo "2";
and so on through your code. It will help you track where your code is going. Put this before inside and outside of every condition statement. It will help you'll see.

Edit your SQL statement

Posted: Sat Nov 27, 2004 12:50 pm
by RobertGonzalez

Code: Select all

<?php
$sql = "UPDATE members SET username='$username',password='$password', email='$email', gid='$gid'WHERE uid=$edit";
?>
Try changing your SQL script at line 19. There needs to be a space between the $username VAR and the password field name AND the $gid VAR and the WHERE clause. Like this...

Code: Select all

<?php
$sql = "UPDATE members SET username='$username', password='$password', email='$email', gid='$gid' WHERE uid=$edit";
?>
You might also want to put in some error checking to along the way to see where things are crapping out. Just a suggestion.