SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Community Advisor ULTiMATE's Avatar
    Join Date
    Aug 2003
    Location
    Bristol, United Kingdom
    Posts
    2,158
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)

    Simple Form Problem - Will not write to DB

    I have this code that I'm using to write to either my buyer or seller database (depending on what option the user chooses, and I've hit a brick wall with getting it working. Here's the code in question:

    PHP Code:
                <?php
                
                    
    if(isset($_POST['accounttype'])):
                    
                    include(
    "./db-connect.inc.php");
                    
                    
    $accounttype $_POST['accounttype'];
                    
    $username $_POST['username'];
                    
    $password $_POST['password'];
                    
    $email $_POST['email'];
                    
    $firstname $_POST['firstname'];
                    
    $surname $_POST['surname'];
                    
    $dob $_POST['dob'];
                    
    $address $_POST['address'];
                    
    $city $_POST['city'];
                    
    $postcode $_POST['postcode'];
                    
    $country $_POST['country'];
                    
    $tel $_POST['tel'];


                    if(
    $accounttype == "buyer")
                    {
                        
    $sql "INSERT INTO `buyer` ( `username` , `password`, `email`, `firstname`, `surname`, `dob`, `address`, `city`, `postcode`, `country`, `tel` ) 
                        VALUES ('
    $username', '$password', '$email', '$firstname', '$surname', '$dob', '$address', '$city', '$postcode', '$country', '$tel');";
                    }
                    else
                    {
                        
    $sql "INSERT INTO `seller` ( `username` , `password`, `email`, `firstname`, `surname`, `dob`, `address`, `city`, `postcode`, `country`, `tel` ) 
                        VALUES ('
    $username', '$password', '$email', '$firstname', '$surname', '$dob', '$address', '$city', '$postcode', '$country', '$tel');";
                    }
                    
                        
                    if(@
    mysql_query($sql))
                    {
                        echo 
    '<p>Account Created!</p>';
                    }
                    else
                    {
                        echo 
    '<p>Error:' mysql_error() . '</p>';
                    }
                
    ?>
    The rest of the code only consists of a form and the else and endif, so I didn't include it. Any help that can be given would be great.

    Extra information if it's needed: The Country field is a dropdown box, and the accounttype is from radio boxes.

  2. #2
    SitePoint Enthusiast Mase's Avatar
    Join Date
    Aug 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At a glance it looks OK.. :S

    Any error messages?

  3. #3
    SitePoint Zealot Bill Palmer's Avatar
    Join Date
    Oct 2005
    Location
    London, UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That code doesn't even run properly...

    Change:
    Code:
    if(isset($_POST['accounttype'])):
    to:
    Code:
    if(isset($_POST['accounttype'])) {
    and add another } before the closing ?>, then try running it.

  4. #4
    SitePoint Enthusiast Mase's Avatar
    Join Date
    Aug 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Bill Palmer View Post
    That code doesn't even run properly...

    Change:
    Code:
    if(isset($_POST['accounttype'])):
    to:
    Code:
    if(isset($_POST['accounttype'])) {
    and add another } before the closing ?>, then try running it.
    I just assumed that he put the colon to take out the following else which contained the form.

  5. #5
    Community Advisor ULTiMATE's Avatar
    Join Date
    Aug 2003
    Location
    Bristol, United Kingdom
    Posts
    2,158
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    I was looking into changing it to that, but it kinda confuses me with the extra code. Here's all the code (with the form inputs taken out, they don't seem that inportant) I'm using.

    PHP Code:
                <?php
                
                    
    if(isset($_POST['accounttype'])):
                    
                    include(
    "./db-connect.inc.php");
                    
                    
    $accounttype $_POST['accounttype'];
                    
    $username $_POST['username'];
                    
    $password $_POST['password'];
                    
    $email $_POST['email'];
                    
    $firstname $_POST['firstname'];
                    
    $surname $_POST['surname'];
                    
    $dob $_POST['dob'];
                    
    $address $_POST['address'];
                    
    $city $_POST['city'];
                    
    $postcode $_POST['postcode'];
                    
    $country $_POST['country'];
                    
    $tel $_POST['tel'];


                    if(
    $accounttype == "buyer")
                    {
                        
    $sql "INSERT INTO `buyer` ( `username` , `password`, `email`, `firstname`, `surname`, `dob`, `address`, `city`, `postcode`, `country`, `tel` ) 
                        VALUES ('
    $username', '$password', '$email', '$firstname', '$surname', '$dob', '$address', '$city', '$postcode', '$country', '$tel');";
                    }
                    else
                    {
                        
    $sql "INSERT INTO `seller` ( `username` , `password`, `email`, `firstname`, `surname`, `dob`, `address`, `city`, `postcode`, `country`, `tel` ) 
                        VALUES ('
    $username', '$password', '$email', '$firstname', '$surname', '$dob', '$address', '$city', '$postcode', '$country', '$tel');";
                    }
                    
                        
                    if(@
    mysql_query($sql))
                    {
                        echo 
    '<p>Account Created!</p>';
                    }
                    else
                    {
                        echo 
    '<p>Error:' mysql_error() . '</p>';
                    }
                
    ?>
                
                <?php else: ?>
                
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" class="cmxform" method="get">
                    <fieldset>
                        <legend>Account Type</legend>
                        <ol>
                            <li>
                                <label for="type">Buyer</label>
                                <input type="radio" name="accounttype" value="buyer" />
                                <label for="type">Seller</label>
                                <input type="radio" name="accounttype" value="seller" />
                            </li>
                        </ol>
                    </fieldset>

                    <fieldset>
                        <legend>User Details</legend>
                        <ol>
                            <li>
                                <label for="username">Username:</label>
                                <input name="username"/>
                            </li>
                            <li>
                                <label for="password">Password:</label>
                                <input name="password" type="password" />
                            </li>
                            <li>
                                <label for="verifypassword">Verify Password:</label>
                                <input name="vpassword" type="password" />
                            </li>
                            <li>
                                <label for="email">Email:</label>
                                <input name="email" />
                            </li>
                        </ol>
                    </fieldset>

                    <fieldset>
                        <legend>Personal Information</legend>
                        <ol>
                            <li>
                                <label for="firstname">First Name:</label>
                                <input name="firstname" />
                            </li>
                            <li>
                                <label for="surname">Surname:</label>
                                <input name="surname" />
                            </li>
                            <li>
                                <label for="dob">Date of Birth:</label>
                                <input name="dob" />
                            </li>
                            <li>
                                <label for="address">Address 1:</label>
                                <input name="address" />
                            </li>
                            <li>
                                <label for="city">City:</label>
                                <input name="city" />
                            </li>
                            <li>
                                <label for="postcode">Postcode:</label>
                                <input name="postcode" />
                            </li>
                            <li>
                                <label for="country">Country:</label>
                                <select name="country">
                                    <option value="uk">United Kingdom</option>
                                    <option value="fr">France</option>
                                    <option value="gr">Germany</option>
                                </select>
                            </li>
                            <li>
                                <label for="tel">Telephone No.</label>
                                <input name="tel" />
                            </li>
                        </ol>
                    </fieldset>

                    <input type="submit" value="Register!" />
                </form>
                            
                <?php endif; ?>
    Quote Originally Posted by Mase View Post
    At a glance it looks OK.. :S

    Any error messages?
    None. It just refreshes the page and adds nothing to the database.

  6. #6
    SitePoint Enthusiast Mase's Avatar
    Join Date
    Aug 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Changed your form tag to:

    Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" class="cmxform" method="post">
    Code:
    <?php
                
                    if(isset($_POST['accounttype'])) :
                    
                    include("./db-connect.inc.php");
                    
                    $accounttype = $_POST['accounttype'];
                    $username = $_POST['username'];
                    $password = $_POST['password'];
                    $email = $_POST['email'];
                    $firstname = $_POST['firstname'];
                    $surname = $_POST['surname'];
                    $dob = $_POST['dob'];
                    $address = $_POST['address'];
                    $city = $_POST['city'];
                    $postcode = $_POST['postcode'];
                    $country = $_POST['country'];
                    $tel = $_POST['tel'];
    
    
                    if($accounttype == "buyer")
                    {
                        $sql = "INSERT INTO `buyer` ( `username` , `password`, `email`, `firstname`, `surname`, `dob`, `address`, `city`, `postcode`, `country`, `tel` )
                        VALUES ('$username', '$password', '$email', '$firstname', '$surname', '$dob', '$address', '$city', '$postcode', '$country', '$tel');";
                    }
                    else
                    {
                        $sql = "INSERT INTO `seller` ( `username` , `password`, `email`, `firstname`, `surname`, `dob`, `address`, `city`, `postcode`, `country`, `tel` )
                        VALUES ('$username', '$password', '$email', '$firstname', '$surname', '$dob', '$address', '$city', '$postcode', '$country', '$tel');";
                    }
                    
                        
                    if(@mysql_query($sql))
                    {
                        echo '<p>Account Created!</p>';
                    }
                    else
                    {
                        echo '<p>Error:' . mysql_error() . '</p>';
                    }
                ?>
                
                <?php else: ?>
                
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" class="cmxform" method="post">
                    <fieldset>
                        <legend>Account Type</legend>
                        <ol>
                            <li>
                                <label for="type">Buyer</label>
                                <input type="radio" name="accounttype" value="buyer" />
                                <label for="type">Seller</label>
                                <input type="radio" name="accounttype" value="seller" />
                            </li>
                        </ol>
                    </fieldset>
    
                    <fieldset>
                        <legend>User Details</legend>
                        <ol>
                            <li>
                                <label for="username">Username:</label>
                                <input name="username"/>
                            </li>
                            <li>
                                <label for="password">Password:</label>
                                <input name="password" type="password" />
                            </li>
                            <li>
                                <label for="verifypassword">Verify Password:</label>
                                <input name="vpassword" type="password" />
                            </li>
                            <li>
                                <label for="email">Email:</label>
                                <input name="email" />
                            </li>
                        </ol>
                    </fieldset>
    
                    <fieldset>
                        <legend>Personal Information</legend>
                        <ol>
                            <li>
                                <label for="firstname">First Name:</label>
                                <input name="firstname" />
                            </li>
                            <li>
                                <label for="surname">Surname:</label>
                                <input name="surname" />
                            </li>
                            <li>
                                <label for="dob">Date of Birth:</label>
                                <input name="dob" />
                            </li>
                            <li>
                                <label for="address">Address 1:</label>
                                <input name="address" />
                            </li>
                            <li>
                                <label for="city">City:</label>
                                <input name="city" />
                            </li>
                            <li>
                                <label for="postcode">Postcode:</label>
                                <input name="postcode" />
                            </li>
                            <li>
                                <label for="country">Country:</label>
                                <select name="country">
                                    <option value="uk">United Kingdom</option>
                                    <option value="fr">France</option>
                                    <option value="gr">Germany</option>
                                </select>
                            </li>
                            <li>
                                <label for="tel">Telephone No.</label>
                                <input name="tel" />
                            </li>
                        </ol>
                    </fieldset>
    
                    <input type="submit" value="Register!" />
                </form>
                            
                <?php endif; ?>
    I've never come across the colon's for the if...else statement.. assuming this is OK i'm struggling to find anything else which is incorrect by just looking at the code.

  7. #7
    Community Advisor ULTiMATE's Avatar
    Join Date
    Aug 2003
    Location
    Bristol, United Kingdom
    Posts
    2,158
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    All works now. Thanks for the help Mase and Bill Palmer.

  8. #8
    SitePoint Zealot Bill Palmer's Avatar
    Join Date
    Oct 2005
    Location
    London, UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had no idea you could do IF statements like that... the lack of the closing ENDIF in the first post threw me off.

    I'll never use them, but nice to know.


Bookmarks

Posting Permissions

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