Having issues with processing form on single page

Discussion in 'PHP' started by frik.groenewald, Aug 13, 2012.

  1. #1
    I'm trying to write a piece of code that should check in the MySQL db if a site name already exists. I'm quite rusty with php so please excuse if I sound stupid. What bothers me is that this code worked a few hours ago, and just stopped working.

    The variable $Request does get populated.
    I'm running three forms on the same page, with different values for the submit buttons. I have removed the second form when I picked up that this one stopped, but it had no influence on the problem.

    Maybe a fresh set of eyes can help.

    Any and all help appreciated.
    <?php require_once ('connections/micatalo.php');?>
    <?php
    //ini_set('display_errors', '5');
    //error_reporting(5);
    if (!empty($_POST['available-submit'])) {
        //do something here;
        //   echo "Available submit";
    $Request=$_POST['site'];
        //echo $Request;
     
    $dup = mysql_query("SELECT * FROM micat_user WHERE user_Site='$Request'");
            if(mysql_num_rows($dup) >0){
                $available=" : Site is not available";
            }
            else{
                $available=" : Site is available";
            }
            
    }
    ?>
    PHP:

     
    frik.groenewald, Aug 13, 2012 IP
  2. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Ok, so you're saying that the form is passing $_POST['site'] to the page that contains the code above?

    If it is getting passed across, then what is the error?

    If you add:

    
    echo $available 
    exit;
    
    Code (markup):
    ..to the bottom of that code, what happens?

    Does it say: "Site is not available", or "Site is available", or does it give an error message, or what?
     
    MrLeN, Aug 13, 2012 IP
  3. ibnatu

    ibnatu Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    youre better off making the condition $_POST['site'], and seeing if thats empty.
     
    ibnatu, Aug 14, 2012 IP
  4. kunalpawar15

    kunalpawar15 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Make sure you are using HTTP POST method to submit the request and the names of form elements are same as you are using code ...
    Your form tag should look like this ...

    <form ... method='post' .....>
     
    kunalpawar15, Aug 14, 2012 IP
  5. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #5
    A few tips:

    - Mever define variables with a beginning upper case character
    - require_once() does not need to have its own PHP open/close tags
    - Be consistent with your white space. Either you put spaces around logic operators or you don't
    - Conditional statements do not need braces around their sub-blocks if they only contain one line
     
    BRUm, Aug 18, 2012 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Another tip, without seeing the markup for your form -- well, it's like giving CSS or scripting without the markup it's applied to; It's gibberish.

    But yeah, as BRUM said, formatting and consistency would help since you seem to be indenting and formatting almost randomly/willy-nilly.

    Also, you should NEVER add a $_POST value directly to a query without some form of sanitation performed on it, which is part of why the mysql_ functions are deprecated in favor of PDO or mySQLi... which means if this is a new site, you really should NOT be using those commands either.
     
    deathshadow, Aug 18, 2012 IP