Using xml in stead of mysql for saving registration form info

Greetings experts,
I would like to change a user verification script from php-mysql to php-xml .. I mean I want to store the data and read it from xml in stead of mysql.
To make my question more understandable :-
Lets say that I have a html file that has a form where users enter :
username, password, country, age, telephonenumber, email and will post those info to register.php where we will have the below code :-
(after some php codes to check the entry and make sure every field entered correctly):-

$query="insert into usertable (username, password, country, age, telephonenumber, email) values ('$username', '$password', '$country', '$age', '$telephonenumber', '$email')";
$result=mysql_query($query);
Now the first thing I want to do is to save those variables in an xml file ( lets call it datafile.xml )

The second part of the question is to make a php script to see if the user exists in datafile.xml
In the login form .. the user would enter username and password.
the verification script with mysql would be something like :-
$q="select * from usertable where username like '$username' and password like '$password'";
$r=mysql_query($q);
if(mysql_num_rows($r)<>0){
echo "login successfull";
}else{
echo "Invalid username or password";}

I want the script to be able to add as much as user we want from the registration form ( not to replace it every time with one user info ).
Thank you very much in advance.
npiutAsked:
Who is Participating?
 
virmaiorCommented:
a flat csv file would be more efficient than XML and easier to code.


0
 
Lukasz ChmielewskiCommented:
for a beginning I'd recommend the lecture of parsing XML with php
http://php.net/manual/en/book.xml.php
0
 
virmaiorCommented:
npiut,  that's actually a bad idea.

(1) XML is not very well designed for speed in comparison with a database.  
(2) It's also meant for transmitting data between disparate applications rather than as the data store for an application.
(3) basically, XML-parsing means culling a giant string to figure out which parts belong to what entry.  A DB install takes care of all of this.



0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
npiutAuthor Commented:
Thanks for your comments virmaior .. but i need to change those codes to work on xml and I will use it in a place where i have no database ( can't install ) and the security is not a big issue ( small network with few people accessing it ).
0
 
Ray PaseurCommented:
I agree with virmaior - a data base is the correct tool, and XML only adds complexity to the issue.  Either install one of the free data bases, or choose the CSV file as your alternative.  

But if you are doing this as a purely academic exercise, you would want to learn about the SimpleXML functions.  They turn an XML file into an object, then you can use iterators like foreach() to walk the object and look for matching usernames, etc.
0
 
npiutAuthor Commented:
Ok guys .. here is what I was able to make so far:-
I made the xml file manually ( userinformation.xml ) :-
<?xml version="1.0" encoding="ISO-8859-1"?>
<USERINFO>
      <USER>
            <NAME>John</NAME>
            <PASSWORD>john2010</PASSWORD>
            <AGE>45</AGE>
            <SEX>Columbia</SEX>
            <COUNTRY>10.90</COUNTRY>
      </USER>

      <USER>
            <NAME>Carolyne</NAME>
            <PASSWORD>carolyne37</PASSWORD>
            <AGE>22</AGE>
            <SEX>Female</SEX>
            <COUNTRY>Canada</COUNTRY>
      </USER>
      
      <USER>
            <NAME>Peter</NAME>
            <PASSWORD>petertheboss</PASSWORD>
            <AGE>52</AGE>
            <SEX>Male</SEX>
            <COUNTRY>Columbia</COUNTRY>
      </USER>
</USERINFO>

and then I use this php code to check for the user .. to make it easier I simply define the username and password inside the php ( ofcourse it will come from the login form page later ):-
checkuser.php :-
<?php
$username='John';
$userpassword='johsdfn2010';
$checkvalue=0;
  $doc = new DOMDocument();
  $doc->load( 'userinformation.xml' );
 
  $books = $doc->getElementsByTagName( "USER" );
  foreach( $books as $book )
  {

  $name = $book->getElementsByTagName( "NAME" );
  $name = $name->item(0)->nodeValue;
 
  $password = $book->getElementsByTagName( "PASSWORD" );
  $password = $password->item(0)->nodeValue;

  $age = $book->getElementsByTagName( "AGE" );
  $age = $age->item(0)->nodeValue;

  $sex = $book->getElementsByTagName( "SEX" );
  $sex = $sex->item(0)->nodeValue;

  $country = $book->getElementsByTagName( "COUNTRY" );
  $country = $country->item(0)->nodeValue;
 
if($username==$name && $userpassword==$password){
$checkvalue=1;
$completeinfo="Username = $name , Userage = $age , UserSex = $sex , UserCountry = $country ";
  }
}
if($checkvalue==1){echo "User exists <br> $completeinfo ";}else{echo "User does not exist";}

  ?>
So basically what I do is I read all the records in the xml file and compare it with the username and password .. if found then I set the variable for checkvalue to 1 .. after reading all the xml file then if the checkvalue = 1 then I display the complete info ( which will become later a welcome screen and a redirection to another page ) .. if username and pw not found then the checkvalue will remain 0 and a message saying ( User does not exist ) will show up and will put the login form to try again.
NOW ! what I need is simply a code to generate the userinformation.xml in stead of creating it manually .. I will make an html page with fields to fill the information and then a registration php page to check the information and make sure they all entered properly .. after passing the check codes I need you guys to write me the code where I can save those info in userinformation.xml.
PLEASE .. NO REDIRECTING TO OTHER TUTORIALS .. I know there is something called google.com ! .. my question is simple ..help me with the codes to write a simple xml file ..
and yes .. all what you said is correct .. using other methodes are much better .. but I need to do it with this simple method.
Thanks again
0
 
npiutAuthor Commented:
hopeless website .. it used to be perfect before .. but not anymore.........
0
 
Ray PaseurCommented:
@npiut: We do not sit around here waiting for you to give us assignments, you know?  We try to answer questions and offer advice from our experience.  Your question as originally posted sounds like, "I want to use a wrench to drive nails -- instead of a hammer.  Please make me a wrench to pound nails."

In response, virmaior gave you the PERFECT answer: "npiut,  that's actually a bad idea."

Recommend you accept the answer from virmaior.  And I further recommend that you learn about the SimpleXML functions.  Because if you insist on using XML, the choice of DomDocument is almost as wrong for this application as the choice of XML over a database!

As far as the "help me with the codes" part - for that you should hire a developer.  We can offer guidance and sometimes sample code, but we are not paid to write your programming for you - we are volunteers who are trying to help our colleagues in the spirit of the open-source community.

Best of luck with your project, ~Ray
0
 
npiutAuthor Commented:
I am out of here .. good luck all
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.