PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
sn202
Forum Commoner
Posts: 36 Joined: Thu Dec 16, 2004 7:30 pm
Post
by sn202 » Sat Dec 18, 2004 6:27 am
Hi, i'm using if statements to carryout an action, depending on the link clicked but i'm getting "Parse error: parse error in /home/sn202/public_html/useradmin.php on line 16" error which is the line "if ($action="show") {" any ideas wot wrong with this...?
PHP Code:
Code: Select all
<?php
$self $_SERVER['PHP_SELF'];
$username = $_POST['username'];
$role = $_POST['role'];
$action = $_GET['action'];
#connect to MYSQL
$conn = @mysql_connect( "linuxproj", "***", "****" )
or die( "could not connect" );
#select the specified database
$rs = @mysql_select_db ( "db_sn202", $conn )
or die( "could not select database" );
$action=(isset($_GET['action']) ? $_GET['action'] : "")
#runs slected PHP function
#show users
if ($action=="show") {
#create the sql query
$sql="select * from users";
#exercute the query
$rs = mysql_query( $sql, $conn )
or die( "could not exercute query: " mysql_error() );
#write data
while( $row = mysql_fetch_array( $rs ) )
{
echo( "ID: " . $row["id"] );
echo( "Username: " . $row["username"];
echo( "role: " . $row["role"] . "<br>");
}
}
#add user
if ($action=="add") {
if( $username and $role ) #ensure values exist
{ #create the sql query
$sql="insert into users (username, role)
values ( $username, "$role" )";
#exercute the query
$rs = mysql_query( $sql, $conn )
or die( "could not exercute query: " mysql_error();
#confirm the added record details
if($rs) { echo( "record added:$username $role" ); }
}
#delete user
if ($action=="delete") {
if( $username ) #ensure values exist
{ #create the sql query
$sql="delete from users where username=$username";
#exercute the query
$rs = mysql_query( $sql, $conn )
or die( "could not exercute query: " mysql_error() );
#confirm the added record details
if($rs) { echo( "record deleted:$username" ); }
}
#update user detailes
if ($action=="update") {
if( $username ) #ensure values exist
{ #create the sql query
$sql="update users set username = $username where username = $oldusername";
#exercute the query
$rs = mysql_query( $sql, $conn )
or die( "could not exercute query" );
$sql="update users set role = $role where username = $username";
#exercute the query
$rs = mysql_query( $sql, $conn )
or die( "could not exercute query: " mysql_error() );
#confirm the added record details
if($rs) { echo( "record updated:$userid $username $role" ); }
}
?>
The code i'm using for the links is:
"useradmin.php?action=show"
I'm also using the "if ($action=="...") {" to display required html for that action eg forms etc.
Any help with this will be much appreciated!!!
Regards,
Simon.
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Sat Dec 18, 2004 8:20 am
get yourself a decent php editor. and see that on line 13 you are missing a ;
sn202
Forum Commoner
Posts: 36 Joined: Thu Dec 16, 2004 7:30 pm
Post
by sn202 » Sat Dec 18, 2004 9:06 am
Cheers, yeah that was one of the bugs, was also missing a } futher on. Page loads now and action "show" works, however, "add", "delete" and "update" do nothing, just refresh the page and no records are added to the database. I can't see the problem, any one else?
Cheers Si.
Heres the updated code:
Code: Select all
<?php
$self =$_SERVER['PHP_SELF'];
$username = $_POST['username'];
$role = $_POST['role'];
$oldusername = $_POST['oldusername'];
$oldrole = $_POST['oldrole'];
$action = $_GET['action'];
error_reporting(E_ALL);
#connect to MYSQL
$conn = @mysql_connect( "linuxproj", "***", "***" )
or die( "could not connect" );
#select the specified database
$rs = @mysql_select_db ( "db_sn202", $conn )
or die( "could not select database" );
$action=(isset($_GET['action'])? $_GET['action'] : "");
#runs slected PHP function
#show users
if ($action=="show") {
#create the sql query
$sql="select * from users";
#exercute the query
$rs = mysql_query( $sql, $conn )
or die(mysql_error());
#write data
while( $row = mysql_fetch_array( $rs ) )
{
echo( "ID: " . $row["userid"] );
echo( " Username: " . $row["username"] );
echo( " role: " . $row["role"] . "<br>");
}
}
#add user
if ($action=="add") {
if( $username and $role ) #ensure values exist
{ #create the sql query
$sql="insert into users (username, role)
values ( $username, "$role" )";
#exercute the query
$rs = mysql_query( $sql, $conn )
or die(mysql_error());
#confirm the added record details
if($rs) { echo( "record added:$username $role" ); }
}
}
#delete user
if ($action=="delete") {
if( $username ) #ensure values exist
{ #create the sql query
$sql="delete from users where username=$username";
#exercute the query
$rs = mysql_query( $sql, $conn )
or die(mysql_error());
#confirm the added record details
if($rs) { echo( "record deleted:$username" ); }
}
}
#update user detailes
if ($action=="update") {
if( $username ) #ensure values exist
{ #create the sql query
$sql="update users set username = $username where username = $oldusername";
#exercute the query
$rs = mysql_query( $sql, $conn )
or die( "could not exercute query" );
$sql="update users set role = $role where username = $username";
#exercute the query
$rs = mysql_query( $sql, $conn )
or die(mysql_error());
#confirm the added record details
if($rs) { echo( "record updated:$userid $username $role" ); }
}
}
?>
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Sat Dec 18, 2004 10:52 am
well, if you post (with update - ..) there won't be a $_GET['action'] ..
<form action="whatever.php?action=update" method="post">
might work (untested)