Welcome to Dream.In.Code
Become a PHP Expert!

Join 149,843 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,396 people online right now. Registration is fast and FREE... Join Now!




Inserting and Managing Database Records

 
Reply to this topicStart new topic

Inserting and Managing Database Records

nitro23456
5 Feb, 2007 - 03:45 AM
Post #1

New D.I.C Head
*

Joined: 21 Jan, 2007
Posts: 5


My Contributions
Hi guys I have the follwing code in a file called register.php its a bit long so please bear with me:

CODE

<?php
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
set_magic_quotes_runtime(0);

include("include/dbcommon.php");
include("include/_users_variables.php");

session_unset();


$registered=false;
$onload=' ';
$strMessage="";
$strUsername="";
$strPassword="";
$strEmail="";
$values=array();

include('libs/Smarty.class.php');
$smarty = new Smarty();

$conn=db_connect();

if(@$_POST["btnSubmit"] == "Register")
{

    $filename_values=array();
    $files_move=array();

//    processing Username - start

    $value = postvalue("value_Username");
    $type=postvalue("type_Username");
    if (in_assoc_array("type_Username",$_POST) || in_assoc_array("value_Username",$_POST) || in_assoc_array("value_Username",$_FILES))
    {
        $value=prepare_for_db("Username",$value,$type);
    }
    else
        $value=false;
    if(!($value===false))
    {
        $values["Username"]=$value;
    }

//    processibng Username - end

//    processing Password - start

    $value = postvalue("value_Password");
    $type=postvalue("type_Password");
    if (in_assoc_array("type_Password",$_POST) || in_assoc_array("value_Password",$_POST) || in_assoc_array("value_Password",$_FILES))
    {
        $value=prepare_for_db("Password",$value,$type);
    }
    else
        $value=false;
    if(!($value===false))
    {
        $values["Password"]=$value;
    }

//    processibng Password - end

//    processing Email Address - start

    $value = postvalue("value_Email_Address");
    $type=postvalue("type_Email_Address");
    if (in_assoc_array("type_Email_Address",$_POST) || in_assoc_array("value_Email_Address",$_POST) || in_assoc_array("value_Email_Address",$_FILES))
    {
        $value=prepare_for_db("Email Address",$value,$type);
    }
    else
        $value=false;
    if(!($value===false))
    {
        $values["Email Address"]=$value;
    }

//    processibng Email Address - end

//    processing First Name - start

    $value = postvalue("value_First_Name");
    $type=postvalue("type_First_Name");
    if (in_assoc_array("type_First_Name",$_POST) || in_assoc_array("value_First_Name",$_POST) || in_assoc_array("value_First_Name",$_FILES))
    {
        $value=prepare_for_db("First Name",$value,$type);
    }
    else
        $value=false;
    if(!($value===false))
    {
        $values["First Name"]=$value;
    }

//    processibng First Name - end

//    processing Last Name - start

    $value = postvalue("value_Last_Name");
    $type=postvalue("type_Last_Name");
    if (in_assoc_array("type_Last_Name",$_POST) || in_assoc_array("value_Last_Name",$_POST) || in_assoc_array("value_Last_Name",$_FILES))
    {
        $value=prepare_for_db("Last Name",$value,$type);
    }
    else
        $value=false;
    if(!($value===false))
    {
        $values["Last Name"]=$value;
    }

//    processibng Last Name - end

//    processing Date of Birth - start

    $value = postvalue("value_Date_of_Birth");
    $type=postvalue("type_Date_of_Birth");
    if (in_assoc_array("type_Date_of_Birth",$_POST) || in_assoc_array("value_Date_of_Birth",$_POST) || in_assoc_array("value_Date_of_Birth",$_FILES))
    {
        $value=prepare_for_db("Date of Birth",$value,$type);
    }
    else
        $value=false;
    if(!($value===false))
    {
        $values["Date of Birth"]=$value;
    }

//    processibng Date of Birth - end

//    processing Address Line 1 - start

    $value = postvalue("value_Address_Line_1");
    $type=postvalue("type_Address_Line_1");
    if (in_assoc_array("type_Address_Line_1",$_POST) || in_assoc_array("value_Address_Line_1",$_POST) || in_assoc_array("value_Address_Line_1",$_FILES))
    {
        $value=prepare_for_db("Address Line 1",$value,$type);
    }
    else
        $value=false;
    if(!($value===false))
    {
        $values["Address Line 1"]=$value;
    }

//    processibng Address Line 1 - end

//    processing Address Line 2 - start

    $value = postvalue("value_Address_Line_2");
    $type=postvalue("type_Address_Line_2");
    if (in_assoc_array("type_Address_Line_2",$_POST) || in_assoc_array("value_Address_Line_2",$_POST) || in_assoc_array("value_Address_Line_2",$_FILES))
    {
        $value=prepare_for_db("Address Line 2",$value,$type);
    }
    else
        $value=false;
    if(!($value===false))
    {
        $values["Address Line 2"]=$value;
    }

//    processibng Address Line 2 - end

//    processing Town/City - start

    $value = postvalue("value_Town_City");
    $type=postvalue("type_Town_City");
    if (in_assoc_array("type_Town_City",$_POST) || in_assoc_array("value_Town_City",$_POST) || in_assoc_array("value_Town_City",$_FILES))
    {
        $value=prepare_for_db("Town/City",$value,$type);
    }
    else
        $value=false;
    if(!($value===false))
    {
        $values["Town/City"]=$value;
    }

//    processibng Town/City - end

//    processing County/State - start

    $value = postvalue("value_County_State");
    $type=postvalue("type_County_State");
    if (in_assoc_array("type_County_State",$_POST) || in_assoc_array("value_County_State",$_POST) || in_assoc_array("value_County_State",$_FILES))
    {
        $value=prepare_for_db("County/State",$value,$type);
    }
    else
        $value=false;
    if(!($value===false))
    {
        $values["County/State"]=$value;
    }

//    processibng County/State - end

//    processing Post/Zip Code - start

    $value = postvalue("value_Post_Zip_Code");
    $type=postvalue("type_Post_Zip_Code");
    if (in_assoc_array("type_Post_Zip_Code",$_POST) || in_assoc_array("value_Post_Zip_Code",$_POST) || in_assoc_array("value_Post_Zip_Code",$_FILES))
    {
        $value=prepare_for_db("Post/Zip Code",$value,$type);
    }
    else
        $value=false;
    if(!($value===false))
    {
        $values["Post/Zip Code"]=$value;
    }

//    processibng Post/Zip Code - end

//    processing Country - start

    $value = postvalue("value_Country");
    $type=postvalue("type_Country");
    if (in_assoc_array("type_Country",$_POST) || in_assoc_array("value_Country",$_POST) || in_assoc_array("value_Country",$_FILES))
    {
        $value=prepare_for_db("Country",$value,$type);
    }
    else
        $value=false;
    if(!($value===false))
    {
        $values["Country"]=$value;
    }

//    processibng Country - end


    $strUsername = $values["Username"];
    $strPassword = $values["Password"];
    $strEmail = $values["Email Address"];

//    add filenames to values
    foreach($filename_values as $key=>$value)
        $values[$key]=$value;

    $strMessage="";
//    check if entered username already exists
    if(!strlen($strUsername))
        $strMessage="Username can not be empty.";
    else
    {
        $strSQL="select count(*) from `_users` where `Username`=".add_db_quotes("Username",$strUsername);
           $rs=db_query($strSQL,$conn);
        $data=db_fetch_numarray($rs);
        if($data[0]>0)
            $strMessage="Username"." <i>".$strUsername."</i> "."already exists. Choose another username.";
    }


    $retval=true;
    if(!$strMessage)
    {
        if(function_exists("BeforeRegister"))
            $retval = BeforeRegister($values);
    }

    if(!$strMessage && $retval)
    {

    //    encrypt password
    $originalpassword=$values["Password"];
    $values["Password"]=md5($values["Password"]);
//    make SQL string
        $strSQL = "insert into `_users` ";
        $strFields="(";
        $strValues="(";
        foreach($values as $akey=>$value)
        {
            $strFields.=AddFieldWrappers($akey).", ";
            $strValues.=add_db_quotes($akey,$value).", ";
        }
        if(substr($strFields,-2)==", ")
            $strFields=substr($strFields,0,strlen($strFields)-2);
        if(substr($strValues,-2)==", ")
            $strValues=substr($strValues,0,strlen($strValues)-2);
        $strSQL.=$strFields.") values ".$strValues.")";
//    insert new user
        LogInfo($strSQL);
        db_exec($strSQL,$conn);

        foreach ($files_move as $file)
            move_uploaded_file($file[0],$file[1]);

    $values["Password"]=$originalpassword;

        if(function_exists("AfterSuccessfulRegistration"))
            AfterSuccessfulRegistration();

//    send email to user
        $message="You can now login and enter the latest competition and post comments in the forum as you have registered as a user at http://".$_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"]."\r\n";
        $label = "Username";
        $message.=$label.": ".$values["Username"]."\r\n";
        $label = "Password";
        $message.=$label.": ".$values["Password"]."\r\n";
        $label = "Email Address";
        $message.=$label.": ".$values["Email Address"]."\r\n";
        mail($strEmail,"Notification on registering",$message);

//    send letter to admin
        $message="User registered at http://".$_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"]."\r\n";
        $label = "Username";
        $message.=$label.": ".$values["Username"]."\r\n";
        $label = "Password";
        $message.=$label.": ".$values["Password"]."\r\n";
        $label = "Email Address";
        $message.=$label.": ".$values["Email Address"]."\r\n";
        $label = "First Name";
        $message.=$label.": ".$values["First Name"]."\r\n";
        $label = "Last Name";
        $message.=$label.": ".$values["Last Name"]."\r\n";
        $label = "Date of Birth";
        $message.=$label.": ".$values["Date of Birth"]."\r\n";
        $label = "Address Line 1";
        $message.=$label.": ".$values["Address Line 1"]."\r\n";
        $label = "Address Line 2";
        $message.=$label.": ".$values["Address Line 2"]."\r\n";
        $label = "Town/City";
        $message.=$label.": ".$values["Town/City"]."\r\n";
        $label = "County/State";
        $message.=$label.": ".$values["County/State"]."\r\n";
        $label = "Post/Zip Code";
        $message.=$label.": ".$values["Post/Zip Code"]."\r\n";
        $label = "Country";
        $message.=$label.": ".$values["Country"]."\r\n";
        mail("name@boxedclever.net","Notification on registering",$message);
//    show Registartion successful message
        $smarty->assign("username",htmlspecialchars($strUsername));
        $smarty->assign("password",htmlspecialchars($strPassword));
        $smarty->display("register_success.htm");
        return;
    }
    else
    {
        if(function_exists("AfterUnsuccessfulRegistration"))
            AfterUnsuccessfulRegistration();
    }
    $smarty->assign("message",$strMessage);
}

//    validation stuff
$bodyonload="";
$onsubmit="";
$includes="";

if($bodyonload)
{
    $onsubmit="return validate();";
    $bodyonload="onload=\"".$bodyonload."\"";
}








$smarty->assign("includes",$includes);
$smarty->assign("bodyonload",$bodyonload);
if(strlen($onsubmit))
    $onsubmit="onSubmit=\"".$onsubmit."\"";
$smarty->assign("onsubmit",$onsubmit);

//    assign values to the controls

if(!count($values))
{
}

$smarty->assign("value_Username",@$values["Username"]);
$smarty->assign("value_Password",@$values["Password"]);
$smarty->assign("value_Email_Address",@$values["Email Address"]);
$smarty->assign("value_First_Name",@$values["First Name"]);
$smarty->assign("value_Last_Name",@$values["Last Name"]);
$smarty->assign("value_Date_of_Birth",@$values["Date of Birth"]);
$smarty->assign("value_Address_Line_1",@$values["Address Line 1"]);
$smarty->assign("value_Address_Line_2",@$values["Address Line 2"]);
$smarty->assign("value_Town_City",@$values["Town/City"]);
$smarty->assign("value_County_State",@$values["County/State"]);
$smarty->assign("value_Post_Zip_Code",@$values["Post/Zip Code"]);
$smarty->assign("value_Country",@$values["Country"]);


$readonlyfields=array();

//    show readonly fields

$linkdata="";
$smarty->assign("linkdata",$linkdata);

$smarty->assign("submitonclick","onclick=\"java script: if (editform.value1_Password==undefined) return true; if(editform.value_Password.value != editform.value1_Password.value) {alert('"."Passwords do not match. Re-enter password"."');editform.value1_Password.value='';return false;} return true;\"");

$smarty->display("register.htm");

?>


Now this will enter my users details into my database and send me and them an email to acknowledge. I need to know:

1. What to add to the code so that when people submit their info it will also add them to my forum users - I am using phpbb for my forum in a separate table called phpbb1_users. SO basically I want the username and password and email address submitted above into my _users table to be entered into the relevant fields in my phpbb1_users table when they click register

I have the following example code for the job:

CODE

function AfterSuccessfulRegistration()
{
//**********  Insert a record into another table  ************
global $conn;
$strSQLInsert = "insert into TableName (Field1, Field2) values (Value1, Value2)";
db_exec($strSQLInsert,$conn);

}


What do I need to edit in it? and where do I place it in the first piece of code? - presume I change 'TableName' with 'phpbb1_users'? and what do I put in for Field1, Field2 and Value1, Value2 etc?

Do I directly swap 'Field1' for 'username' etc etc?

Also after a while, I will be removing entries from my _users table to start my sight off fresh again - obviously they will still exist in my forum users table as I want to keep them there. What will happen if someone re-registers with the same details in the _users table and their record already exists from previoulsy in the forum table? will I get an error or will it just add them to the _users table ignoring that they are alreadyin the forum table?

This post has been edited by nitro23456: 5 Feb, 2007 - 04:08 AM
User is offlineProfile CardPM
+Quote Post

rockstar_
RE: Inserting And Managing Database Records
6 Feb, 2007 - 09:30 AM
Post #2

D.I.C Head
Group Icon

Joined: 16 Oct, 2006
Posts: 187


Dream Kudos: 275
My Contributions
QUOTE(nitro23456 @ 5 Feb, 2007 - 04:45 AM) *

What do I need to edit in it? and where do I place it in the first piece of code? - presume I change 'TableName' with 'phpbb1_users'? and what do I put in for Field1, Field2 and Value1, Value2 etc?

Do I directly swap 'Field1' for 'username' etc etc?

Also after a while, I will be removing entries from my _users table to start my sight off fresh again - obviously they will still exist in my forum users table as I want to keep them there. What will happen if someone re-registers with the same details in the _users table and their record already exists from previoulsy in the forum table? will I get an error or will it just add them to the _users table ignoring that they are alreadyin the forum table?

It would help a ton if you posted the schema for your php_bb_users table. If you can show me that, I can help you grok simple SQL queries enough so that you can insert and update records in the table.

rockstar_
User is offlineProfile CardPM
+Quote Post

apollyonus
RE: Inserting And Managing Database Records
14 Feb, 2007 - 01:03 AM
Post #3

New D.I.C Head
*

Joined: 13 Feb, 2007
Posts: 7


My Contributions
QUOTE(nitro23456 @ 5 Feb, 2007 - 04:45 AM) *

Also after a while, I will be removing entries from my _users table to start my sight off fresh again - obviously they will still exist in my forum users table as I want to keep them there.


It generally just seems like a _bad_ idea to me to authenticate your users twice. I'd suggest either using either phpbb's users table or or your own to do the job once.

As to your question of "What will happen if someone tries to register again with the same info?" That will depend on how your db schema is setup. It is possible that fields such as username & email will be unique, and attempt to duplicate a unique field value will throw an error.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:05AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month