简体   繁体   中英

PHP & MySQL check database if data is entered question

I was wondering how can I check if there is no tags entered into the database for a specific post if there are none, display no tags have been entered . How can I do this using PHP?

Here is my PHP & MySQL code.

$tag = array();
$dbc = mysqli_query($mysqli, "SELECT tags.tag
                  FROM posts_tags 
                  INNER JOIN tags ON tags.id = posts_tags.tag_id 
                  WHERE posts_tags.post_id = '" . $post_id . "' 
                  GROUP BY tags.tag");

if (!$dbc) {
    print mysqli_error($mysqli);
}  else {
    while($row = mysqli_fetch_array($dbc)){
    $tag[] = $row['tag'];
    }
}

If the tag array is empty, then there are no tags:

if(empty($tag)){
   echo 'no tags have been entered';
}

You could also check for the number of rows returned:

if(mysqli_num_rows($dbc) == 0){
   echo 'no tags have been entered.';
}

Use mysqli_num_rows and code like this:

$tag = array();
$dbc = mysqli_query($mysqli, "SELECT tags.tag
                  FROM posts_tags 
                  INNER JOIN tags ON tags.id = posts_tags.tag_id 
                  WHERE posts_tags.post_id = '" . $post_id . "' 
                  GROUP BY tags.tag");

if (!$dbc)
{
    print mysqli_error($mysqli);
}
elseif(!mysqli_num_rows($dbc))
{
    echo "no tags have been entered";
}
else
{
    while($row = mysqli_fetch_array($dbc))
    {
        $tag[] = $row['tag'];
    }
    print_r($tag);
}

$(".signup_name_image").hide();

var error_signup_name = false;

$(".signup_name").blur(function(){
    check_signup_name();
});

function check_signup_name() {
    var signup_name = $.trim($('.signup_name').val());
    if (signup_name == '' || signup_name.length < 6 || signup_name.length > 50 ) {
        $(".signup_name").css({"border":"1px solid #ff0000"});
        $(".signup_name_image").show();
        $(".signup_name_show").html("&#8226; Write Full Name in 6 to 50 alphabets");
        **$(".signup_name").focus();**
        error_signup_name = true;
    } else if (/^[a-zA-Z0-9- ]*$/.test(signup_name) == false){
        $(".signup_name").css({"border":"1px solid #ff0000"});
        $(".signup_name_image").show();
        $(".signup_name_show").html("&#8226; Contain invalid character");
        **$(".signup_name").focus();**
        error_signup_name = true;
    }else {
        $(".signup_name").css({"border":"1px solid #21cc00"});
        $(".signup_name_image").hide(); 
    }
}

$("#signup_form").submit(function(){

    error_signup_name = false;

    check_signup_name();

    if (error_signup_name == false) {
        return true;
    } else {
        return false;
    }
});

$(".signup_name_image").hover(function(){
    $(".signup_name_show").show();
}, function() {
    $(".signup_name_show").hide();}
);

Now Focus is stuck on same field How to Focus on field when submit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM