Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100



Go Back   PHP-Editors > Programming Help > PHP Programming Help

PHP Programming Help Post any question relating to PHP Programming here and hopefully someone can help.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 2008-08-03, 12:56 PM
databear's Avatar
Junior Member
 
Join Date: Aug 2008
Posts: 5
databear is on a distinguished road
Default FIXED - HTTP_POST_VARS error fixed; Now a bigger problem has happened

Hi all,

"I am getting the following error when running my page:
Notice: Undefined variable: HTTP_POST_VARS in (location of file) on line 108"
This above is now fixed thanks to Tom.

But now I have a different problem, once the Create User button is pressed, the data is not sent to my database and as you can see I am now using $_POST instead of the outdated $HTTP_POST_VARS, please help as this is really doing my head in now.

here are the relevant UPDATED codes for this file:

PHP Code:
<?php include('./include/admin.php'); ?>
<?php 
include('./include/debug_helper.php'); ?>
<?php $debug 
true?>
<?php
function insert_data($formdata)
{
// Insert Data into users table
// $formdata = form array
 
// Set up database connection variables
$error "";
$myhost "localhost";
$myuser "user";
$mypassword "password";
$mydb "bagsbookings";
 
// Set up data to insert
$username $formdata['username'];
$password $formdata['password'];
$status $formdata['status'];
 
// Encrypt password using the key "HASHCODE"
$password crypt($password,"HASHCODE");
 
// Connect to mySQL server
$mysql = @mysql_connect($myhost$myuser$mypassword);
if (!
$mysql)
{
$error "Cannot connect to mySQL server";
return (
$error);
}
 
// Connect to Database
$mysqldb = @mysql_select_db($mydb$mysql);
if (!
$mysqldb)
{
$error "Cannot open database";
return(
$error);
}
 
// Insert Data
$myquery "INSERT INTO tbl_users ( username, password, status) VALUES ('".$username."', '".$password."', '".$status."')";
$result mysql_query($myquery$mysql);
if (!
$result)
{
$error "Cannot run query";
return(
$error);
}
 
// Return true if record written successfully
return("true");
}
?>
<?php
function verify_data($formdata)
{
// This function uses the functions in the include file
// and uses them to validate various aspects of the form.
// If validation fails, it returns $error, the appropriate error message
// If validation suceeds, return true
$error "";
$form_data trim_data($formdata);
$user $form_data['username'];
 
// check all form fields are filled in
if (!check_form($form_data))
{
$error "All Form Fields MUST be filled in";
return(
$error);
}
 
// Check password and confirmation password match
if (!confirm_password($form_data'password''confirmpassword'))
{
$error "Password and Confirms password do not match";
return(
$error);
}
 
// Check password length
if(!check_password_length($form_data'password'5));
{
$error "Password should be more than 5 characters long";
return(
$error);
}
 
// Check that username is unique
$check check_unique($user'bagsbookings''localhost''user''password''tbl_users''username');
if (
$check != "true")
{
$error "Username is already in use, select another";
return(
$error);
}
 
// If validated successfully, insert data into table
$insert_check insert_data($formdata);
 
// If error with insertion, return error
if($insert_check != "true")
return(
$insert_check);
 
// Form validated and record inserted successfully
return("");
}
?>
<?php
// Main code - Verifies the form data, and inserts into
// users table in the database
if($_POST["submit"]=="Create_User"){
$error verify_data($_POST);
if (
$error == "")
$success "User inserted successfully";
}
?>
HTML Code:
 
        <p>&nbsp;</p>
          <p><?php echo($error); ?>
              <?php echo($success); ?>
          </p>
        <h4>Create New User</h4>
       <form name="createuser" method="post" action="<?php echo($_SERVER['PHP_SELF'}); ?>">
           <p align="center">Enter new user details here: </p>
            <table width="200" border="1">
              <tr>
                <td>Username:</td>
                <td><div align="center">
                  <input name="username" type="text" id="username" maxlength="45"  >
                </div></td>
              </tr>
              <tr>
                <td>Password: </td>
                <td><div align="center">
                  <input name="password" type="password" id="password" maxlength"80">
                </div></td>
              </tr>
              <tr>
                <td>Confirm Password:</td>
                <td><div align="center">
                  <input name="confirmpassword" type="password" id="confirmpassword" maxlength="80" >
                </div></td>
              </tr>
              <tr>
                <td>User Status:</td>
                <td><div align="center">
                  <select name="status" id="status">
                    <option value="admin">Admin</option>
                    <option value="staff" selected>Staff</option>
                  </select>
                </div></td>
              </tr>
            </table>
            <p align="center">
              <input type="Submit" name="Submit" value="Create_User" >
            </p>
        </form>
        <p>&nbsp;</p>
        <?php 
   debuginfo($_POST, "formdata", 42);
  ?>
This form is used for creating a new user on my website, well it would if it worked

I am using PHP5.2.5 with Apache 2.2.8 on a Microsoft Vista Business PC

This is my first piece of programming with PHP so your help will be greatly appreciated.

Many thanks again in advance

Databear

Last edited by databear; 2008-08-06 at 01:21 PM. Reason: Fixed, but now don't work correctly
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 2008-08-05, 08:18 AM
Junior Member
 
Join Date: Nov 2006
Posts: 27
englishtom is on a distinguished road
Default

Hi Steve,

Sorry for the slow reply...

We need to get PHP to give us some kind of idea what is going wrong so for the moment lets make the query as simple as it possibly can be.

First we connect to mysql and select the correct DB.

PHP Code:
$host "localhost";
$user "";
$pass "";
$connect mysql_connect($host,$user,$pass)
    or die(
"Could not connect: " mysql_error());
 
$db mysql_select_db("")
or die(
"Could not connect to database: " mysql_error());

$connect;
$db
You need to fill in your connection info there, next we take our $_POST and put it in variables.

PHP Code:
$username $_POST['username'];
$password $_POST['password'];
$status $_POST['status']; 
Finally we construct our SQL query, and run it.

PHP Code:
$sql "INSERT INTO tbl_users (`username` , `password` , `status`) VALUES ( '$username' , '$password' , '$status' )";
$result mysql_query($sql)
or die(
'mysql error: '.mysql_error()); 
Try commenting out your current script and replacing it with that simplified one and see if we get back any errors or if it works.

Tom
Reply With Quote
  #3 (permalink)  
Old 2008-08-05, 10:17 AM
databear's Avatar
Junior Member
 
Join Date: Aug 2008
Posts: 5
databear is on a distinguished road
Default

Hi Tom,

The new script comes up with errors stating that the
PHP Code:
$username $_POST['username'];
$password $_POST['password'];
$status $_POST['status']; 
are undefined

also when the page runs it creates a blank entry into the database, and when you fill the form in and press Create User button it does save the details from the form into the database

So we are nearly there

Steve
Reply With Quote
  #4 (permalink)  
Old 2008-08-05, 11:10 AM
Junior Member
 
Join Date: Nov 2006
Posts: 27
englishtom is on a distinguished road
Default

Hi Steve,


PHP Code:
if($_POST["submit"]=="Create_User"){

    
//connect to database
    
$host "localhost";
    
$user "";
    
$pass "";
    
$connect mysql_connect($host,$user,$pass)
        or die(
"Could not connect: " mysql_error());
     
    
$db mysql_select_db("")
    or die(
"Could not connect to database: " mysql_error());
    
    
$connect;
    
$db
    
    
//check data
    
    
$username trim($_POST['username']);
    
$pass1trim($_POST['password']);
    
$pass2trim($_POST['confirmpassword']);
    
$status trim($_POST['status']);
    
    if(
$pass1 == $pass2 && !empty($username) && !empty($status) ){
    
        
// Encrypt password using the key "HASHCODE"
        
$password crypt($pass1,"HASHCODE");
    
        
$sql "INSERT INTO tbl_users (`username` , `password` , `status`) VALUES ( '$username' , '$password' , '$status' )";
        
$result mysql_query($sql)
            or die(
'mysql error: '.mysql_error()); 
            
        echo 
'User inserted successfully!';
    
    } else {
    
        echo 
'Sorry boss, form filled in incorrectly! :(';
    
    }
    
} else {

    echo 
'Sorry boss, wrong button pushed! :(';



That script should check that the create user button is pushed, connect to the db, check that the form has been filled in and passwords match, encrypt the password then insert it into the db.

Sorry I dont have time to do a more comprehensive solution but if you have any further questions I'd be happy to help.

Tom
Reply With Quote
  #5 (permalink)  
Old 2008-08-05, 11:47 AM
databear's Avatar
Junior Member
 
Join Date: Aug 2008
Posts: 5
databear is on a distinguished road
Default

Thanks Tom,

It works like a dream now, apart from when it first runs and states that submit is undefined, even hough all instances of it are in lowercase, but it works so I will sort that bit out later.

Again thanks for your help with this as on my own it would have taken me months to figure it out.

Steve
Reply With Quote
  #6 (permalink)  
Old 2008-08-05, 11:52 AM
Junior Member
 
Join Date: Nov 2006
Posts: 27
englishtom is on a distinguished road
Default

No problem Steve, if you put an @ symbol at the start of any line in PHP you will suppress error messages, so..

PHP Code:
if(@$_POST["submit"]=="Create_User"){ 
Should suppress that error.

Tom

Last edited by englishtom; 2008-08-06 at 06:31 AM. Reason: Correcting code
Reply With Quote
  #7 (permalink)  
Old 2008-08-06, 01:21 PM
databear's Avatar
Junior Member
 
Join Date: Aug 2008
Posts: 5
databear is on a distinguished road
Default Fixed

Thanks Tom,

everything is working greata now .

Thanks again

Databear
Reply With Quote
Must read Review for Serious PHP Developers


NuSphere PhpED 5.5 : The Staff of php-editors.com recently spent a few days working with NuSphere PhpED 5.5 (a popular PHP IDE) and NuCoder 2.0 (a PHP Encoding Utility), read up on all the details.

Sponsored Links
Reply

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 11:15 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.