Php 函数未在wordpress中插入值

Php 函数未在wordpress中插入值,php,wordpress,Php,Wordpress,如果name exist,则该函数正常工作。如果name exist,则返回exists;如果name exist,则返回exists;如果name exist不存在,则不会发生任何事情。no else子句引发什么问题。我做错了什么事。我想在检查后提交表单,并在添加货币时显示成功消息,但没有发生任何事情。我尝试了一整天,最后失败了。建议我。正如@akirk建议的那样,您永远不会将$nomatchfound设置为true或在您的姓名中设置为1 但是,您还可以做一些其他事情来让代码更清晰、更快。例如

如果name exist,则该函数正常工作。如果name exist,则返回exists;如果name exist,则返回exists;如果name exist不存在,则不会发生任何事情。no else子句引发什么问题。我做错了什么事。我想在检查后提交表单,并在添加货币时显示成功消息,但没有发生任何事情。我尝试了一整天,最后失败了。建议我。

正如@akirk建议的那样,您永远不会将$nomatchfound设置为true或在您的姓名中设置为1

但是,您还可以做一些其他事情来让代码更清晰、更快。例如,name_to_match可以返回一个值,而不是设置一个全局变量。您可以使用数据库来检查$nametocheck,而不是在表中的所有值上循环-这将使代码更短,运行更快。所以我将名称改为匹配如下:

function name_to_match($nametocheck){
    global $wpdb,$namematch,$nomatchfound;
    $query="select * from currency";
    $namematch=$wpdb->get_col($query,1);
    //echo $namematch;
        foreach($namematch as $namet){
            //echo $name;
            if($namet == $nametocheck){
                echo "Name Already Exists<br />";
                $nomatchfound=0;
            }

        }
}

function add_signal_form(){
    global $wpdb,$insert,$nametocheck;
    echo "<br /><br /><br />";
    $nametocheck=trim($_POST['name']);
    $sign=trim($_POST['sign']);
    $status=$_POST['status'];
    if(isset($_POST['submit'])){
    //..................Function to call if name exist it will not add 
name_to_match($nametocheck);

        if($nomatchfound ==0){
            echo "Match Found";}
else
{

            $insert= $wpdb->insert('currency',array('name'=>$nametocheck,'sign'=>$sign,'status'=>$status));
                if(!$insert){
                    echo "Currency Not Added Query Fails";
                }
                else
                {
                    echo "Currency Successfully Added";
                }       
    }
 }

    echo '<form name="form1" method="post" action="">';
    echo '<div class="label">';
    echo '<label for="name">Name</label>';
    echo '<div class="field">';
    echo '<input type="text" name="name" id="name">';
    echo '</div>';
    echo '<div class="label">';
    echo '<label for="sign">Sign</label>';
    echo '</div>';
    echo '<div class="field">';
    echo '<input type="text" name="sign" id="sign">';
    echo '</div>';
    echo '<div class="label">';
    echo '<label for="status">Status</label>';
    echo '</div>';
    echo '<div class="field">';
    echo '<select name="status" id="status">';
    echo '<option value="1">Publish</option>';
    echo '<option value="0">Draft</option>';
    echo '</select>';
    echo '</div>';
    echo '<div class="submit">';
    echo '<input type="submit" name="submit" id="submit" value="Save Currency">';
    echo '</div>';
    echo '</p>';
    echo '</form>';
/*
    echo "<form action='' name=form1\" id=\"form1\" method=\"post\">";
    echo '<input type="text" name="text" id="text">';
    echo '<input type="submit" name="submit" value="Add New Signal">';  
        //wp_dropdown_pages();
    echo "</form>";
*/}
然后,add_signal_form函数的开始变为

function name_to_match($nametocheck){
    global $wpdb;

    $query = $wpdb->prepare("select count(*) from currency where name = %s", $nametocheck);
    $matching_row_count = $wpdb->get_var($query);

    return $matching_row_count;
}

您是否将$nomatchfound设置为true或1?否+1对我来说是启用的。。。但谢谢你,我也从你那里学到了准备声明…很高兴能帮上忙。如果你不能+1,你能接受这个答案吗?作为询问者,你应该能够,这给了我一个向上投票的声誉。干杯
function add_signal_form(){
    global $wpdb;
    echo "<br /><br /><br />";
    $nametocheck=trim($_POST['name']);
    $sign=trim($_POST['sign']);
    $status=$_POST['status'];
    if(isset($_POST['submit'])){
        $existing_rows = name_to_match($nametocheck);

        if($existing_rows != 0){
            echo "Match Found";
        }
        else {

            $insert= $wpdb->insert('currency',array('name'=>$nametocheck,'sign'=>$sign,'status'=>$status));
            if (!$insert) {
                echo "Currency Not Added Query Fails";
            }
            else {
                    echo "Currency Successfully Added";
            }       
        }
    }