To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-04-2003, 10:57 PM   #1
PHP_apprentice
Member
 
PHP_apprentice's Avatar
 
Join Date: Oct 2003
Posts: 56
Just looking for advice on how to improve my script

This is a script that I use to handle some entry forms and i was wondering if it is efficient and if i could turn it into a function that I could just call when needed.

PHP Code:

<?php
//Begin user session
session_start();
header("Cache-control: private"); //IE 6 Fix
//End Session information
//Include Section
require('db/db_connect.php');
require(
'funtions/ecape_data.php');

?>
<html>
<head>
    <title>DMP | Welcome!</title>
</head>
</html>

<?php


if (isset($_POST['submit'])) { //handle the form
    
if (empty($_POST['user_id']) && empty($_POST['pass_word'])){//check fields for data
        
echo'Please Login! If you are experiencing problems contact<br>a system administrator <a href="mailto:dev@directmailpartners.com"></a> .  Thank you!<br><br>';
        echo
'<a href="login.php">Return to Login!</a>';
        exit();
    }else{
//assign variables
        
$u = escape_data($_POST['user_id']);
        
$p = escape_data($_POST['pass_word']);
        
$_SESSION['user_id'] = $u;
    
        
$query = "SELECT * FROM employee WHERE user_id = '$u' and pass = '$p'";
        
$result = mysql_query($query, $db);
        
$count_result = mysql_num_rows($result);
    
    
//Verfiy login information
    
if ($count_result > 0 ){//start
        
$query = "SELECT fname FROM employee WHERE user_id = '$u'";
        
$name = mysql_query($query, $db);
        
$fname = mysql_fetch_array($name);
        
$_SESSION['fname'] = $fname['fname'];
    
    
    
    
//Information Displayed @ successful login
    
echo'<table align="center"><tr><td><a href="edit.php">Edit Profile</a></td>';
    echo
'<td><a href="inv_truck.php">Enter New Inventory</a></td>';
    echo
'<td><a href="check_out.php">Check Out Inventory</a></td>';
    echo
'<td><a href="check_in.php">Check In Inventory</a></td>';
    
        
//Check for Admin level access
        
$query = "SELECT level FROM employee WHERE user_id = '$u'";
        
$result = mysql_query($query, $db);
        
$level = mysql_fetch_array($result);
        
$admin = $level["level"];
    
    if (
$admin >= 4 ){ //start
        
echo'<td><a href="admin.php">Admin</a><br></td>';
        echo
'<td><a href="job/job_new.php">Enter New Job</a><br></td>';
        echo
'<td><a href="job/job_out.php">Finish Job</a><br></td>';
        echo
'<td><a href="page2.php">Check Inventory<br></a></td></tr>';
        echo
'<tr><td colspan = 6 align-"center"><br><br>Welcome to the members site&nbsp;', $_SESSION['fname'], '<br><br>';
        echo
'You have logged in successfully<br></td></tr>';
        exit();
        }else{
            echo
'</tr><tr><td colspan = 4 align="center"><br><br>Welcome to the members site&nbsp;', $_SESSION['fname'], '<br><br>';
        echo
'You have logged in successfully<br></td></tr></table>';
        }
//End
    
    
    
}Else{
        echo
'Please check to make sure you have the correct username and password!!<br>';
        echo
'<a href="login.php">Return to Login</a>';
    }
//end of user_id and pass check
        
    
    
    
}//verify form was filled in!!!
    
    
    
    
    
}//End of Form handle
            
?>

Last edited by PHP_apprentice; 11-04-2003 at 11:23 PM.
PHP_apprentice is offline   Reply With Quote
Old 11-06-2003, 07:24 AM   #2
illusion
Junior Member
 
Join Date: Oct 2003
Posts: 16
For a better practice, I'd suggest you take out all the logics from your HTML codes. E.g

function isUserExists($username, $password) {
//logics
return true/false;
}

function isAdmin($username){
//logics
return true/false;
}


if (!isUserExists($user_id, $pass_word)) {
die ("<h1>You're not a user!</h1>");
} else {
......
}
illusion is offline   Reply With Quote
Old 11-16-2003, 11:31 AM   #3
jacrewq
Algoalex
 
Join Date: Aug 2002
Location: algobank.com
Posts: 84
php.net/echo


echo <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon no extra whitespace!
END;
jacrewq is offline   Reply With Quote
Old 11-17-2003, 12:51 PM   #4
Shrike
Not Yet Involved
 
Shrike's Avatar
 
Join Date: Oct 2003
Location: The Eighth, Sursamen
Posts: 2,254
I can't see how the if/else blocks fit together because theres no indenting, but you seem to run 3 queries when the first one gets * all fields for user_id/pass

my2c
Shrike is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 03:29 AM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.