0

I am new to PHP. My isset function is not working .It does not not show error .It always insert the table. I have given my code. Kindly advice what i done wrong on this

<?php
if(isset($_POST['submit'])) {
    $a= $_POST['companyname'];
    $b= $_POST['ballpark_url'];
    $c= $_POST['username'];
    $d= $_POST['email'];
    $e= $_POST['login1'];
    $f= $_POST['pass'];
    $error = false;
    if(validname($a) ==false) {
        echo $nameerror = "enter the valid name";
    }
    if(validemail($d) ==false) {
        echo $nameerror = "enter the valid email";
    }
    if(isset($_POST['companyname'])) {
        if($error==false) {
            $query= "INSERT INTO user VALUES ('$a','$b','$c','$d','$e','$f','');";
            if(!mysql_query($query)) {
                die ('error:'.mysql_error());
            }
            mysql_close($conn);  
        }
        include("templats/header_1.html");
        include("templats/content.html");
        include("templats/footer.html");
    }
?>

kindly rectify the issue in my code and send back to me...

3
  • Check $_POST array. Is 'submit' element exists in it? Commented Aug 18, 2011 at 6:46
  • 1
    @Marc Towler: That's definitely the answer. Post it! ;) Commented Aug 18, 2011 at 6:50
  • 1. isset() is not a function. 2. Why would you expect any kind of error here? Please show us your form. Commented Aug 18, 2011 at 6:51

4 Answers 4

2

Following on from Garvey, if it doesnt exist, then check in your HTML that you have set a submit button with the name submit...... (thanks shef for saying to post it ;))

Sign up to request clarification or add additional context in comments.

Comments

2

He didn't set the $error variable to true, so it is always inserting.

$error = false;
if(validname($a) ==false) {
    echo $nameerror = "enter the valid name";
}
if(validemail($d) ==false) {
    echo $nameerror = "enter the valid email";
}

This is how it needs to be done

$error = false;
if(validname($a) ==false) {
    echo $nameerror = "enter the valid name";
    $error = true;
}
if(validemail($d) ==false) {
    echo $nameerror = "enter the valid email";
   $error = true;
}

Comments

1

Check if you have a line in your form like

 <input type="submit" name="submit" value="submitOrSomething">

Your isset checks if a name value pair exist with the key "submit". So if you have a submit button but it has a different name other than submit then you have to use that with isset.

1 Comment

Thanks Swordfish for clarifying what i had in my post +1
1

Use !empty($a) instead of isset($_POST['companyname']).

However then your page wouldn't display. Why not:

<?php
if(isset($_POST['submit'])) {

    $company_name = $_POST['companyname'];
    $ballpark_url = $_POST['ballpark_url'];
    $username     = $_POST['username'];
    $email        = $_POST['email'];
    $login1       = $_POST['login1'];
    $pass         = $_POST['pass'];

    $error = false;

    if( !validname( $company_name ) ) {
        echo "enter the valid name";
        $error = true;
    }

    if( !validemail( $email ) ) {
        echo "enter the valid email";
        $error = true;
    }


    if( empty($_POST['companyname']) ) {
        echo "enter a company name";
        $error = true;
    }

    if( !$error ) {
        $query= "INSERT INTO user VALUES ('$company_name',
                                          '$ballpark_url',
                                          '$username',
                                          '$email',
                                          '$login1',
                                          '$pass','');";

        if(!mysql_query($query)) {
         die ( 'error:' . mysql_error() );

         mysql_close( $conn );  
    }

    include("templats/header_1.html"); 
    include("templats/content.html");
    include("templats/footer.html");
}
?>

5 Comments

@JK True, but is the user not in this case just checking to see that the submit key is in the POST array, rather then actually wanting to check the content of it?
@JK: Do you mean the other way around!? :D
@Marc Towler OP mentioned that it IS inserting into the table. This means that both $_POST['submit'] and $_POST['companyname'] are set in the array, however I think he wants to prevent it from submitting when $_POST['companyname'] is empty.
@idea that is a good point that i missed, but i still stand by what i said to JK based on what he said
@Mark Towler You're right - It could be either: OP either didn't expect companyname to have posted, or he expected it to post but wanted to check if it was empty (and used the wrong function to do so). I thought the latter was more likely and based my answer off this assumption.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.