SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checking empty fields

    Hi How are you , I want from this script to
    *don't send data to the database if the name field is empty,
    but the problem here is if I leave the name field empty it sends data to the database and it shows the message "the field name is empty please fill it"at the same time ,
    I don't want the script to send any thing to the data base if the field name is empty
    please any help and I appreciate if someone can explain and give me the easiest way to do this.





    PHP Code:


    <?php
       error_reporting
    (E_ALL E_NOTICE);
      
        
    //1-connecting to localhost
        
    if(!$connect=mysql_connect('localhost','root','')){
        exit(
    'unable to connect to the data base');}
        
       
    // select database
        
    if (!mysql_select_db('topic')){
        exit (
    'unable to locate database');}
        
        
        
        
    // define avariable into php
         
    $name=isset($_POST["name"]) ? $_POST['name'] : "";
         
    $area=isset($_POST["area"]) ? $_POST['area'] : "";
      
      
         
    // inserting data into data base
        
    $insert="INSERT INTO hiji SET
        name='"
    .mysql_real_escape_string($name)."' ,
        area='"
    .mysql_real_escape_string($area)."'";

        if(!
    $a=mysql_query($insert)) { 
        exit( 
    'error query'.mysql_error());
       }

    //fetching data from database

    $db_search mysql_query("SELECT name,area FROM omar");

    if (@
    mysql_num_rows($db_search) > 0) {
    //here I had amastake that I wasnt able to bring data from area because I haventselected area in mysql_query area 
    while($data mysql_fetch_array($db_search)) 
    { Print 
    "<b>Name:</b>".$data['name'] . "<br/>" 
     Print 
    "<b>Area:</b>".$data['area'] . "<br/>" ;
    }
    }
    //if the field form is empty or not 
    if(empty($name)){
        echo
    "the field name is empty please fill it";
        
    }
    if ( empty(
    $name))
           {
                  
                    return 
    false;
            }

  2. #2
    SitePoint Zealot Cute Tink's Avatar
    Join Date
    Apr 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I moved your code around a little bit:

    PHP Code:
    <?php

    error_reporting
    (E_ALL E_NOTICE);

    //1-connecting to localhost

    if (!$connect=mysql_connect('localhost','root','')){

       exit(
    'unable to connect to the data base');}

       
    // select database

       
    if (!mysql_select_db('topic')){

            exit (
    'unable to locate database');
        
        }

       
    // define avariable into php

       
    $name=isset($_POST["name"]) ? $_POST['name'] : "";
       
    $area=isset($_POST["area"]) ? $_POST['area'] : "";

       
    // here is where you need to check if the $name has been set
       // and only do the insert if it is
       
    if ( $name != '' ) {

          
    // inserting data into data base

            
    $insert="INSERT INTO hiji SET
            name='"
    .mysql_real_escape_string($name)."' ,
            area='"
    .mysql_real_escape_string($area)."'";

            if (!
    $a=mysql_query($insert)) { 

                exit( 
    'error query'.mysql_error());

            }

        }
        
       else {

          echo 
    'The name field is empty please fill it';

       }


        
    //fetching data from database

        
    $db_search mysql_query("SELECT name,area FROM omar");

        if (@
    mysql_num_rows($db_search) > 0) {

            
    //here I had amastake that I wasnt able to bring data from area because I haventselected area in mysql_query area 

            
    while ($data mysql_fetch_array($db_search)) { 
        
                Print 
    "<b>Name:</b>".$data['name'] . "<br/>" 

                Print 
    "<b>Area:</b>".$data['area'] . "<br/>" ;

            }

        }
        
    }
    ?>
    return false doesn't fit where you put it. You would use that if you were creating a function to do this for you.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you alot


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
  •