开发者

php stop passing null values tp mysql

i am using following code to add values

$debdes = $_POST['debdes'];
$debamt = $_POST['debamt'];
$crdes  = $_POST['crdes'];
$cramt  = $_POST['cramt'];
$date   = $_POST['date'];
include_once ("db.php");

$ucbook = "INSERT INTO cbook(debde开发者_如何转开发s,debamt,crdes,cramt,date) VALUES ('$debdes','$debamt','$crdes','$cramt','$date');";

now i want that if "debamt" is filled "debdes" should not be empty and like this if "cramt" is filled then "crdes" should not be empty while pressing submit button so if some one press submit button it displays a message that say fill "debdes" first.


What you're looking for is some basic form validation:

$debdes = $_POST['debdes'];
$debamt = $_POST['debamt'];
$crdes  = $_POST['crdes'];
$cramt  = $_POST['cramt'];
$date   = $_POST['date'];
include_once ("db.php");

$errors = array();

// Check for error conditions
if(!empty($debamt) && empty($debdes)) {
    $errors[] = 'Fill also debdes';
}

if(!empty($cramt) && empty($crdes)) {
    $errors[] = 'Fill also crdes';
}

// If no errors were found, execute the query
if(count($errors) == 0) {
    $ucbook = "INSERT INTO cbook(debdes,debamt,crdes,cramt,date) VALUES     ('$debdes','$debamt','$crdes','$cramt','$date');";
} else {
    // If there are errors, display them:
    echo implode('<br />', $errors);
}


On the previous form itself you can have the "submit" button trigger a javascript function as follows:-

function validate(){
    var debamt=document.*form_name*.debamt.value;
    var debdes=document.*form_name*.debdes.value;
    var error_message;
    if(debamt!=null && debdes==null){      
        error_message = 'Please fill debdes value first!!!';
    }
    var cramt=document.*form_name*.cramt.value;
    var crdes=document.*form_name*.crdes.value;
    if(cramt!=null && crdes==null){      
        error_message = '\nPlease fill crdes value first!!!';
    }
    alert(error_message);
}


 if( ! empty($_POST['debdes']) && empty($_POST['debdes']))
     die('fill "debdes" first');

the same goes for cramt and crdes.


$debdes = $_POST['debdes'];
$debamt = $_POST['debamt'];
$crdes  = $_POST['crdes'];
$cramt  = $_POST['cramt'];
$date   = $_POST['date'];

if ( !empty($debamt) && empty($debdes) ) {
    show_error('debdes');
}

if ( !empty($cramt) && empty($crdes) ) {
    show_error('drdes');
}

function show_error($string) {
    echo "Value '$string' must be filled.";
    exit;
}

include_once ("db.php");

$ucbook = "INSERT INTO cbook(debdes,debamt,crdes,cramt,date)
           VALUES ('$debdes','$debamt','$crdes','$cramt','$date');";
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜