...

View Full Version : edit user php script (need a help)



viddz
04-17-2012, 11:51 AM
Iam new to php and iam having some problems with my edit user php file. The problem is the query doesnt perform. No error showing. Just PLs can some one help me with this code. Thanks in advance.




<?php
session_start();


if(!isset($_SESSION['SESS_LOGIN']) || $_SESSION['SESS_TYPE'] !='admin')// if session variable "login" does not exist.
{
echo '<script language="javascript">';
echo 'alert("Please login as ADMINISTRATOR to edit a user");';
echo ' window.location.replace("login-form.php");';
echo '</script>';

//header("location:login-form.php"); // Re-direct to login-form.php

}
else
{
include("config.php");
$id = mysql_real_escape_string($_POST['id']);
$login = mysql_real_escape_string($_POST['login']);
$password = mysql_real_escape_string($_POST['password']);
$type = mysql_real_escape_string($_POST['type']);

$qry_edit = " UPDATE members SET login='$login', password='$password', type='$type' WHERE id='$id' ";

$count = mysql_query("SELECT COUNT(id) FROM members WHERE id='$id'");
if(mysql_num_rows($count)==1)
{

if($result=mysql_query($qry_edit))
//or die(mysql_error());
{
/*echo '<script language="javascript">';
echo 'alert("you have successfully edited one user !" );';
// echo 'window.setTimeout("window.location.replace('adduser.php');",20);';
//echo 'window.setTimeout("window.location.replace('adduser.php'),20");';
echo '</script>';*/
header("Location: view_all_user.php");

}
else
{

echo "<br><font color=red size=+1 >Problem in editing !</font>" ;
echo "ERROR - unable to save new username and password!<br>";
$SQLError = "SQL ERROR: ".mysql_errno().". ".mysql_error()."<BR><BR>";
echo "$SQLError";
mysql_close();
}
}
//echo "<br><font color=green size=+1 >you have successfully edited one user ! <br>[ username = $login ] </font>" ;
else
{
echo "<br><font color=red size=+1 >No id !</font>" ;
}
}
?>

angst
04-17-2012, 01:21 PM
try changing this;


if($result=mysql_query($qry_edit))

to this


if(mysql_query($qry_edit))

viddz
04-17-2012, 02:08 PM
Done,
But following error occured what shal i do?

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in F:\xampplite\htdocs\vidi\edit_user_save.php on line 26

No id !

angst
04-17-2012, 02:21 PM
you'll need to figure out why the ID isn't being passed along.

mlseim
04-17-2012, 02:27 PM
Add a test line to see what is happening ...


$id = mysql_real_escape_string($_POST['id']);
$login = mysql_real_escape_string($_POST['login']);
$password = mysql_real_escape_string($_POST['password']);
$type = mysql_real_escape_string($_POST['type']);
echo "id: $id";
exit;

viddz
04-17-2012, 02:33 PM
@angst

i ddnt get wat u say. may b my language skills r bad.

this is my view all user page code.



<?php
session_start();


if(!isset($_SESSION['SESS_LOGIN']) || $_SESSION['SESS_TYPE'] !='admin')// if session variable "login" does not exist.
{
echo '<script language="javascript">';
echo 'alert("Please login as ADMINISTRATOR to view all users");';
echo ' window.location.replace("login-form.php");';
echo '</script>';

//header("location:login-form.php"); // Re-direct to login-form.php

}
else
{
include("config.php");

// get results from database
$result = mysql_query("SELECT * FROM members")
or die(mysql_error());

// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>login</th> <th>type</th> <th>password</th> <th></th> <th></th> </tr>";

// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result ))
{

// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['login'] . '</td>';
echo '<td>' . $row['type'] . '</td>';
echo '<td>' . $row['password'] . '</td>';
echo '<td><a href="edituser.php?id=' . $row['id'] . '">Edit</a></td>';
echo '<td><a href="delete_user.php?id=' . $row['id'] . '">Delete</a></td>';
echo "</tr>";
}

echo "</table>";
}

?>



this is my edit user form code



<!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=utf-8" />
<title>Edit user</title>



<script type="text/javascript">

function validateForm(){
with (document.edituserForm)

{
if(login.value == "") {
alert("Error: login cannot be blank!");
login.focus();
return false;
}
if(login.value.length < 4) {
alert("Error:Login must contain at least four characters!");
login.focus();
return false;

}

re = /^\w+$/;
if(!re.test(login.value)) {
alert("Error: login must contain only letters, numbers and underscores!");
login.focus();
return false;
}

if(password.value == "" ) {
alert("Error: Password field cannot be blank !");
password.focus();
return false;
}
if(password.value.length < 4) {
alert("Error: Password must contain at least four characters!");
password.focus();
return false;
}

if(!re.test(password.value)) {
alert("Error: Password must contain only letters, numbers and underscores!");
password.focus();
return false;

}



else

// alert("You have added a new user : " + login.value);
return true;

}}
</script>

<link href="table.css" rel="stylesheet" type="text/css" media="screen" />

</head>

<body>
<?php $id = $_GET[id]; ?>

<form ACTION="edit_user_save.php" name="edituserForm" id="edituserForm" METHOD="POST" align="center" onsubmit="return validateForm()" >

<input type="hidden" name="id" value="$id" />
<div align="center"><center>
<table width="436" id="table">
<tr>
<td width="214">username</td>
<td width="254"><input name="login" maxlength="10" id="login" /></td>
</tr>
<tr>
<td>password</td>
<td><input type="text" id="password" name="password" maxlength="10" /></td>
</tr>
<tr>
<td>Select user type :</td>
<td><select name="type" id="type">
<option value="admin"> Admin </option>
<option value="labassistant"> Lab Assistant </option>
<option value="storekeeper"> Store Keeper </option>
</select></td>
</tr>

<tr>
<td></td>
<td><input type="submit" name="button" id="button" value="save"/></td>
</tr>
</table>
</center></div>

</form>
</body>
</html>


anythng wrong with those codes ??

mlseim
04-17-2012, 02:39 PM
Here's one problem I see ...


This line:
<input type="hidden" name="id" value="$id" />

Should be:
<input type="hidden" name="id" value="<?=$id=>" />

viddz
04-17-2012, 02:41 PM
@mlseim,

done as u told,
this is the result: id: $id

wat would be the error ?

mlseim
04-17-2012, 03:01 PM
id:$id

That is an error ... $id should be a number, not the literal "$id".

Fix this:

This line:
<input type="hidden" name="id" value="$id" />

Should be:
<input type="hidden" name="id" value="<?=$id=>" />

viddz
04-17-2012, 05:39 PM
yeah !! thank u very much mlseim & angst. problem is with id. :D :thumbsup:
problem solved



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum