How to insert these values to database in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mmarif4u
    New Member
    • Sep 2006
    • 23

    How to insert these values to database in php

    Hi everybody,
    I already post a thread like this but that was in mysql.
    Here i have a problem with all the code,,

    Code:
    <?php // signup.php
    
    include("common.php");
    include("db.php");
    
    if (!isset($_POST['submitok'])):
        // Display the user signup form
        ?>
    <!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
       <title> Access Code Generator Form </title>
      <meta http-equiv="Content-Type"
        content="text/html; charset=iso-8859-1
    </head><link href="style.css" rel="stylesheet" type="text/css">
    <body bgcolor="slate"><center>
    
    <h3> <font color="#FFFFFF" face="verdana"><u>Registration Form For New Users</u><p>Please Fill The Form Below Carefully
    </font>
    </h3></body>
    
    <form name="reg" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    
    <table align="center" border="0" cellpadding="0" cellspacing="5">
    <hr color="blue" />
        <tr>
            <td align="right">
                <p><font color="#FFFFFF" face="verdana">IC Number :</font></p>
            </td>
            <td>
                <input name="icnumber" type="text" maxlength="100" size="25" />
                <font color="orangered" size="+1"><tt><b>*</b></tt></font>
            </td>
        </tr>
        
        <tr>
            <td align="right">
                <p><font color="#FFFFFF" face="verdana">Access Code :</font></p>
            </td>
            <td>
                <input name="acccode" type="text" maxlength="100" size="25" />
                <font color="orangered" size="+1"><tt><b>*</b></tt></font>
            </td>
        </tr>
        <tr>
            <td align="right">
                <p><font color="#FFFFFF" face="verdana">Username :</font></p>
            </td>
            <td>
                <input name="username" type="text" maxlength="100" size="25" />
                <font color="orangered" size="+1"><tt><b>*</b></tt></font><td align="left">Enter a valid user name.</td>
            </td>
        </tr>
        <tr>
            <td align="right">
                <p><font color="#FFFFFF" face="verdana">Full Name :</font></p>
            </td>
            <td>
                <input name="fullname" type="text" maxlength="100" size="25" />
                <font color="orangered" size="+1"><tt><b>*</b></tt></font>
            </td>
        </tr>
        <tr>
            <td align="right">
                <p><font color="#FFFFFF" face="verdana">Email :</font></p>
            </td>
            <td>
                <input name="email" type="text" maxlength="100" size="25" />
                <font color="orangered" size="+1"><tt><b>*</b></tt></font><td>Email must be valid for password retreival.</td>
            </td>
        </tr>
        <tr>
            <td align="right">
                <p><font color="#FFFFFF" face="verdana">Hand Phone :</font></p>
            </td>
            <td>
                <input name="hp" type="text" maxlength="100" size="25" />
                <font color="orangered" size="+1"><tt><b>*</b></tt></font>
            </td>
        </tr>
        <tr><td>
        <font  face="verdana" color="orangered" size="+1"><tt><b>*</b></tt></font>
        <u><font face="arial" size="2" color="#FFFFFF">indicates a required field</u></font>
        </td>
        </tr>
        
        <tr>  
            <td align="right" colspan="3">
               <hr color="blue" />
                
                <input type="reset" value="Reset" />
                <input type="submit" name="submitok" value="Register"  />
                
            </td>
        </tr>
    </table>
    </form>
    
    </body>
    </html>
    
        <?php 
       
    else:
        // Process signup submission
        dbConnect('db');
          
        if ($_POST['icnumber']=='' or $_POST['acccode']=='' or $_POST['username']==''or $_POST['fullname']==''
          or $_POST['email']=='' or $_POST['hp']=='')  { 
             error ('One or more required fields were left blank.\\n'.
                  'Please fill them in and try again.');
        }
        
        // Check for existing user with the new id
        $sql = "SELECT * FROM m_users WHERE icnumber = '$_POST[icnumber]' AND acccode = '$_POST[acccode]'";
        $result = mysql_query($sql);
        if (!$result==0) {	
            error('A database error occurred in processing your '.
                  'submission.\\nIf this error persists, please '.
                 'contact you@example.com.');
        }
        if (mysql_result($result) < 1) {
            error('Your entered Ic number and Access code does not match\\n'.
                 'to the database data. Please try agian.');
        }
        
        $newpass = substr(md5(time()),0,6);
        
        $sql = "INSERT INTO m_users where icnumber = '$_POST[icnumber]' SET
                 password = PASSWORD('$newpass'),
                 email = '$_POST[email]',
                 h/p = '$_POST[hp]',
                 username = '$_POST[username]',
                 fullname = '$_POST[fullname]'";
        if (!mysql_query($sql))
            error('A database error occurred in processing your '.
                  'submission.\\nIf this error persists, please '.
                  'contact admin@straight-a.com.my .\\n' . mysql_error());
                  
        // Email the new password to the person.
        $message = "G'Day!
    
    Your personal account for the Straight-a program
    has been created! To log in, proceed to the
    following address:
    
        http://www.straight-a.com.my/
    
    Your personal login ID and password are as
    follows:
    
        username: $_POST[username]
        password: $newpass
    
    You aren't stuck with this password! Your can
    change it at any time after you have logged in.
    
    If you have any problems, feel free to contact me at
    <admin@straight-a.com.my>.
    
    -Arif
     Webmaster
    ";
    
        mail($_POST['email'],"Your Password for the Straight-a Program",
             $message, "From:Arif <admin@straight-a.com.my>");
             
        ?>
        <!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
          <title> Registration Complete </title>
          <meta http-equiv="Content-Type"
            content="text/html; charset=iso-8859-1" />
        </head>
        <body>
        <p><strong>User registration successful!</strong></p>
        <p>Your userid and password have been emailed to
           <strong><?=$_POST['email']?></strong>, the email address
           you provided in your registration form. To log in,
           go to your email and click the link provided in your email.</p>
        </body>
        </html>
        <?php
    endif;
    ?>
    Now the question arises are these:

    IC number and Access Code are alredy given to users.

    1: I want that when user enters the details like Ic number,access code,Full name,email,hp,u sername etc. The script 1st check that is their any IC number and access code exist in the table, if have than store the
    others values to the table like email,username, hp etc to that row of table.
    if no error msg showed.
    2: Where i edit the script to give right results.Is there any thing wrong in the sql query.

    Plz help me if any body have idea..
    Thanks...
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    What is your problem? You already check that a user exists and, if so, update the table! So what is there to change?

    Ronald :cool:

    Comment

    • mmarif4u
      New Member
      • Sep 2006
      • 23

      #3
      Ronald the problem is simple 1st i want to check the icnumber
      & access code if it exists in the database than it insert the other values
      like fullname,email, handphone,usern ame etc, But if these 2 not exists in
      the database than data not stored in the database...

      For example my ic number is 123456 and access code is 456,i enter
      these values to the text box with completing the form like name=arif,
      email=mmarif4u@ domain.com,user name=mmarif4u etc and click submit
      button,so 1st the query checks the ic number & access code if it is in the
      database than my name,email,user name stored to the database otherwise
      cannot...


      i think u will understand what i want to say here,,,

      The problem is this script cannot work, i dont know something wrong with query
      or script...
      When i click the button it says the user already exists,thats the problem..

      Thanks

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        You cannot do an INSERT when the record already exists. Statement should be
        Code:
        $sql = "UPDATE m_users SET
                password = PASSWORD('$newpass'),
                email = '$_POST[email]',
                h/p = '$_POST[hp]',
                username = '$_POST[username]',
                fullname = '$_POST[fullname]'
                WHERE icnumber = '$_POST[icnumber]'";
        I must also caution you should sanitize ANY input before using it with database functions. You should at least use the strip_tags or htmlentities commands.

        Ronald :cool:

        Comment

        Working...