Results 1 to 3 of 3

Thread: Problem in PHP..

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Posts
    2

    Problem in PHP..

    I have created a form that sends data in my database sql. Thats goes well only if the fields are empty, data is transmitted in the base. I try to use the empty but it not work, My friend told me to use the trim but that also didn't work.

    Here are the coding so that you can know better
    php Code:
    1. <? php
    2.  
    3. mysql_connect ( "localhost", "root", "") OR die ( 'Error selecting database');
    4. mysql_select_db ( "camping") or die ( 'Error selecting database');
    5. / / (=> Useless
    6.  
    7. $ name = $ _POST [ 'name'];
    8. $ first_name = $ _POST [ 'firstname'];
    9. $ address = $ _POST [ 'address'];
    10. $ PostalCode = $ _POST [ 'PostalCode'];
    11. $ city = $ _POST [ 'city'];
    12. $ telephone = $ _POST [ 'telephone'];
    13. $ mail = $ _POST [ 'mail'];
    14. $ message = $ _POST [ 'message'];
    15.  
    16. if (empty ($ name) & & ($ firstname) & & empty ($ address) & & empty ($ PostalCode) & & empty ($ city) & & empty ($ telephone) & & empty ($ mail) & & empty ($ message))
    17. (
    18. echo ( "There is an empty field");
    19. ) else
    20. (
    21. echo ( "fields filled");
    22.  
    23. )
    24.  
    25. $ req = mysql_query ( 'INSERT INTO clients (name, surname, address, Postcode, city, telephone, mail, message) VALUES ("'.$ name.' "" '. $ firstname.' "" '. $ address. ' ""'. $ PostalCode. ' ""'. $ city. ' ""'. $ telephone. ' ""'. $ mail. ' ""'. $ message. ") ') or die (mysql_error ());
    26. if ($ req)
    27. (
    28. echo 'enregisttrement reussi';
    29. )
    30. else
    31. (
    32. echo 'There was a problem <br/>. $ req;
    33. )
    34.  
    35.  
    36.  
    37. mysql_close ();
    38. >

    Can someone help me ?

    Thanks
    edit by admin: no commercial URLs on the forum please
    Last edited by Hack; Jun 3rd, 2009 at 09:25 AM. Reason: Added PHP Code Tags

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Problem in PHP..

    use "||" (OR) instead of "&&" (AND). your check using empty() is checking if -all- fields are empty because AND requires all of those to return true; OR only requires one to return true.

    PHP Code:
    if(empty($name) || ($firstname) || empty($address) || empty($PostalCode) || empty($city) || empty ($telephone) || empty($mail) || empty ($message)){

      
    //there was an empty field
      
    echo "some field was empty";

    }else{

      
    //no empty fields
      //your mysql insert stuff should go here



  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    21

    Re: Problem in PHP..

    For security I suggest you use classes to put your sql stuff in and never use die(mysql_error()) for production

Posting Permissions

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



Click Here to Expand Forum to Full Width