开发者

PHP-Isset function is not working

开发者 https://www.devze.com 2023-03-28 17:53 出处:网络
I am new to PHP. My isset function is not working .It does not not showerror .It always insert the table. I have given my code. Kindly advice what i done wrong on this

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);  
        }
        incl开发者_StackOverflow社区ude("templats/header_1.html");
        include("templats/content.html");
        include("templats/footer.html");
    }
?>

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


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 ;))


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;
}


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.


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");
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消