SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Please help with my if statement

    ok basically it connects to mysql fine but adds empty fields when the page is refreshed or adds the same data to the field multiple times

    can i write an if to make it check if name and email not null
    also to stop it from entering the same data multiple times

    <?php
    $name=$_POST['name'];
    $email=$_POST['email'];
    require_once ('mysql_connect.php');
    mysql_query("INSERT INTO `email` VALUES ('$name', '$email')");
    mysql_close();
    ?>
    <font face="Arial" color="#FFFFFF" size="+1">
    <form action="index1.php" method="post">
    &nbsp &nbsp &nbsp &nbsp Please Enter Your Details <br>
    &nbsp &nbsp Your Name: <input type="text" name= "name"><br>
    &nbsp &nbsp E-mail: &nbsp &nbsp &nbsp &nbsp <input type="text" name = "email"><br>
    &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
    <input type="submit" value="Click To Join"> <br></form></font>

    </form>
    </left>
    </body>
    </html>

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay few things here:

    PHP Code:
    $name=$_POST['name'];
    $email=$_POST['email']; 
    I'll say it till chickens stop dancing in south america (that's tough to pull off btw), you need to escape that data through something such as mysql_real_escape_string(). Accepting user input with no restriction leads to SQL injection.

    PHP Code:
    // See if the post array actually has data (ie the form is posted)
    if(!empty($_POST))
    {
      if(empty(
    $_POST['name'] || empty($_POST['email'])
      {
          
    // If the email or name field is blank, set a warning to display later
          
    $error 'Please fill in your name and email.<br/>';
      }
      else
      {
        
    // Escape the data and insert it into the database
        
    $namemysql_real_escape_string($_POST['name']);
        
    $emailmysql_real_escape_string($_POST['email']);
        require_once (
    'mysql_connect.php');
        
    mysql_query("INSERT INTO `email` VALUES ('$name', '$email')");
        
    mysql_close();
      }



    Code HTML4Strict:
    &nbsp &nbsp &nbsp &nbsp Please Enter Your Details <br>
    &nbsp &nbsp Your Name: <input type="text" name= "name"><br>
    &nbsp &nbsp E-mail: &nbsp &nbsp &nbsp &nbsp <input type="text" name = "email"><br>
    &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp

    You seem to have a lot of non breaking spaces here. This could be due to an editor, but for readability I think it's a good idea to use css padding, or some kind of table spacing instead.

    Also you can use that $error variable to have some sort of warning message for required fields:

    PHP Code:
    <?php
    if(!empty($error))
    {
    ?>
    <div class="error"><?php echo $error?></div>
    <?php
    }
    ?>

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its adding blank rows because you aren't making sure that $_POST isn't empty.
    Code:
    <?php
    if (!empty($_POST)) {
    $name=$_POST['name'];
    $email=$_POST['email'];
    require_once ('mysql_connect.php');
    mysql_query("INSERT INTO `email` VALUES ('$name', '$email')");
    mysql_close();
    }
    ?>
    <font face="Arial" color="#FFFFFF" size="+1">
    <form action="index1.php" method="post">
    &nbsp &nbsp &nbsp &nbsp Please Enter Your Details <br>
    &nbsp &nbsp Your Name: <input type="text" name= "name"><br>
    &nbsp &nbsp E-mail: &nbsp &nbsp &nbsp &nbsp <input type="text" name = "email"><br>
    &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
    <input type="submit" value="Click To Join"> <br></form></font>
    
    </form>
    </left>
    </body>
    </html>
    MySQL v5.1.58
    PHP v5.3.6


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
  •