Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Location
    Oklahoma City, OK
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Inserting Data into Database

    I'm trying to take the following information from a form and insert it into a database. Assuming that the connection information is correct, I'm not able to get it to insert. I was also trying to get it to keep the values that are correct, if something is found to be incorrect during the error checking process. I hope that this is as clear as mud.

    PHP Code:
    <?php
        
        
    include('connectdb.php');
        
    $errors = array();
        
    //If request is a form submission
        
    if($_SERVER['REQUEST_METHOD'] =='POST'){
            
            
            
    //Validation
            
            //Check that firstname is non-blank
            
    if(=== preg_match("/\S+/",$_POST['firstname'])){$errors['firstname'] = "Please enter a first name.";}
            
            
    //Check that lastname is non-blank
            
    if(=== preg_match("/\S+/",$_POST['lastname'])){$errors['lastname'] = "Please enter a last name.";}

            
    //If no validation errors
            
    if(=== count($errors)){
                
                
    //Sanitize all data
                
    $firstname mysql_real_escape_string($_POST['firstname']);
                
    $middlename mysql_real_escape_string($_POST['middlename']);

                
    //Connect to the databse
                
    connectdb();
                
                
    //Insert user into the database
                
    $query "INSERT INTO members_info
                        ('firstname', 'lastname')
                        VALUES
                        ('$firstname', '$lastname')"
    ;
                
                
    mysql_query($query);
                
                if(
    mysql_errno() === 0){
                    
    //Registration successful
                    
    $user_id mysql_insert_id();
                    
    log_in($user_id);
                    
    header("Location: index.php");
                }
        
    //Helper functions
        
    function form_row_class($name){
            global 
    $errors;
            return 
    $errors[$name] ? "form_error_row" "";
        }
        
        function 
    error_for($name){
            global 
    $errors;
            if(
    $errors[$name]){
                return 
    "<div style='color: red;' class='form_error'>" $errors[$name] . "</div>";
            }
        }
        
        function 
    h($string){
            return 
    htmlspecialchars($string);
        }

    <
    html>
    <
    body>
     <
    tr class="<?php echo form_row_class("firstname") ?>">
            <
    td width="25%" align="right" valign="middle">
                    
    First Name:
            </
    td>
            <
    td width="193" valign="left">
                <
    input type="text" name="firstname" id="firstname" size="50" maxlength="21" value="<?php h($_POST['firstname']);?>" width="433"/>
                <?
    php echo error_for('firstname'?>
            </td>
     </tr>

     <tr class="<?php echo form_row_class("lastname"?>">
            <td class="fields" width="25%" align="right" valign="middle">
                    Last Name:
             </td>
            <td width="193">
                <input type="text" name="lastname" id="lastname" size="50" maxlength="21" value="<?php h($_POST['lastname']);?>" width="433"/>
                <?php echo error_for('lastname'?>
            </td>
     </tr>
        <tr>
            <td>
            </td>
            <td colspan="2" align="left" >
                <input type="submit" size="50" name="member_complete_button" value="Create My Account">
            </td>
        </tr>
    </body>
    </html>
    Last edited by Inigoesdr; 09-19-2011 at 06:39 PM.

  • #2
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,559
    Thanks
    76
    Thanked 105 Times in 104 Posts
    Please use the PHP code tags when posting code, it highlights the code, and color codes it, so it's easier to find problems.

    Also why is this showing middlename instead of lastname?

    PHP Code:
    $firstname mysql_real_escape_string($_POST['firstname']);
    $middlename mysql_real_escape_string($_POST['middlename']); 

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    mysql_real_escape_string needs a valid mysql connection.

    Try to put the function connectdb before the mysql_real_escape_string

    and also, Use: mysql_query($query) or die(mysql_error());

    PHP Code:
    <?php

    include('connectdb.php');
    $errors = array();
    //If request is a form submission
    if($_SERVER['REQUEST_METHOD'] =='POST'){


    //Validation

    //Check that firstname is non-blank
    if(=== preg_match("/\S+/",$_POST['firstname'])){$errors['firstname'] = "Please enter a first name.";}

    //Check that lastname is non-blank
    if(=== preg_match("/\S+/",$_POST['lastname'])){$errors['lastname'] = "Please enter a last name.";}

    //If no validation errors
    if(=== count($errors)){

    //Connect to the databse          // Replaced
    connectdb();                          // Replaced

    //Sanitize all data
    $firstname mysql_real_escape_string($_POST['firstname']);
    $middlename mysql_real_escape_string($_POST['middlename']);
    // NOTE: In the preg_match and the form you use lastname and here middlename. Where did you get the middle name from?
    //$lastname = mysql_real_escape_string($_POST['lastname']; // Added

    //Insert user into the database
    $query "INSERT INTO members_info
    ('firstname', 'lastname')
    VALUES
    ('$firstname', '$lastname')"
    ;

    mysql_query($query) or die(mysql_error()); // Editted

    if(mysql_errno() === 0){
    //Registration successful
    $user_id mysql_insert_id();
    log_in($user_id);
    header("Location: index.php");
    }
    //Helper functions
    function form_row_class($name){
    global 
    $errors;
    return 
    $errors[$name] ? "form_error_row" "";
    }

    function 
    error_for($name){
    global 
    $errors;
    if(
    $errors[$name]){
    return 
    "<div style='color: red;' class='form_error'>" $errors[$name] . "</div>";
    }
    }

    function 
    h($string){
    return 
    htmlspecialchars($string);
    }
    // Close PHP
    ?>
    <html>
    <body>
    <tr class="<?php echo form_row_class("firstname"?>">
    <td width="25%" align="right" valign="middle">
    First Name:
    </td>
    <td width="193" valign="left">
    <input type="text" name="firstname" id="firstname" size="50" maxlength="21" value="<?php h($_POST['firstname']);?>" width="433"/>
    <?php echo error_for('firstname'?>
    </td>
    </tr>

    <tr class="<?php echo form_row_class("lastname"?>">
    <td class="fields" width="25%" align="right" valign="middle">
    Last Name:
    </td>
    <td width="193">
    <input type="text" name="lastname" id="lastname" size="50" maxlength="21" value="<?php h($_POST['lastname']);?>" width="433"/>
    <?php echo error_for('lastname'?>
    </td>
    </tr>
    <tr>
    <td>
    </td>
    <td colspan="2" align="left" >
    <input type="submit" size="50" name="member_complete_button" value="Create My Account">
    </td>
    </tr>
    </body>
    </html>
    Last edited by Wanna; 09-19-2011 at 11:51 AM. Reason: Adding code and some comments

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Please remember to read the stickies for this forum. In particular the one about using [php][/php] tags when posting code. You'll notice you are missing a closing PHP tag due to the coloring of the text when you use the proper tags.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •