Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果值已经存在,如何在值的末尾添加字符?_Javascript_Php_Jquery - Fatal编程技术网

Javascript 如果值已经存在,如何在值的末尾添加字符?

Javascript 如果值已经存在,如何在值的末尾添加字符?,javascript,php,jquery,Javascript,Php,Jquery,在这里,当我单击post按钮时,它会在数据库中插入一个随机值。 若数据库中已经存在值,则显示错误。它很好用 但如果数据库中已经存在,我想在值的末尾添加2/3个字符。如果$check==1,则我希望在值的末尾添加一些字符,而不是显示警报。如何做到这一点 <?php $con = mysqli_connect("localhost","root","","post") or die("unable to connect to internet"); if(isset($_POST['su

在这里,当我单击post按钮时,它会在数据库中插入一个随机值。
若数据库中已经存在值,则显示错误。它很好用

但如果数据库中已经存在,我想在值的末尾添加2/3个字符。如果
$check==1
,则我希望在值的末尾添加一些字符,而不是显示警报。如何做到这一点

<?php 

$con = mysqli_connect("localhost","root","","post") or die("unable to connect to internet");

if(isset($_POST['submit']))
{
    $slug = $_POST['rand'];
    $get_slug = "select * from slug where post_slug='$slug' ";
    $run_slug = mysqli_query($con,$get_slug );
    $check = mysqli_num_rows($run_slug );

    // if $check==1   then i want to add 2  characters at the end  of $slug .  

    if($check == 1)
    {
        //  instead of showing alert i want to add 2 more characters at the end of that value and and insert it on database

        echo "<script> alert('something is wrong') </script> ";
        exit ();
    }
    else
    {
        $insert ="insert into slug (post_slug) values ('$slug') "; 

        $run = mysqli_query($con,$insert);

        if($run)
        {
            echo "<p style='float:right;'> Posted  successfully </p>";
        }
    }
}
?>

<form method="POST" >
<?php
    $result = "";

    $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    $chararray = str_split($chars);
    for($i = 0; $i < 7 ; $i++)
    {
$randitem = array_rand($chararray);
        $result .= "".$chararray[$randitem];
    }
    echo $result ;
?>

    <input type="hidden" value="<?php echo $result;?>" name="rand" />

    <span class="input-group-btn">
        <button class="btn btn-info"  type="submit" name="submit">POST</button>
    </span>
</form>   

如果
$check==1

if($check == 1){
$newSlug = $slug."xy";    
$update = "update slug set post_slug = '".$newSlug."' where post_slug = '".$slug."'";
        $run = mysqli_query($con,$update );
        echo "<script> alert('Updated Successfully') </script> ";
        exit ();

}
if($check==1){
$newSlug=$slug.“xy”;
$update=“update slug set post\U slug=””。$newSlug。“”其中post\U slug=”。“$slug.”;
$run=mysqli\u查询($con$update);
回显“警报(‘已成功更新’)”;
退出();
}

这对您很有帮助

<?php 

$con = mysqli_connect("localhost","root","","post"  ) or die

( "unable to connect to internet");

if(isset($_POST['submit'])){

        $tmp_slug = $_POST['rand'];
        $slug = $_POST['rand'];

        while(check_exiest($tmp_slug))
        {
            $tmp_rand = rand(11,99);
            $tmp_slug = $slug.$tmp_rand;
        }

        $insert ="insert into slug (post_slug) values ('$tmp_slug') "; 

        $run = mysqli_query($con,$insert);

        if($run) 
        {
            echo "<p style='float:right;'> Posted  successfully </p>";
        }
}

public function check_exiest($slug)
{
    $get_slug = "select * from slug where post_slug='$slug' ";
    $run_slug = mysqli_query($con,$get_slug );
    $check = mysqli_num_rows($run_slug );

    if($check >= 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

?>

只需对代码进行少量修改即可插入新值

<?php 

$con = mysqli_connect("localhost","root","","post") or die("unable to connect to internet");

if(isset($_POST['submit']))
{
    $slug = $_POST['rand'];
    $get_slug = "select * from slug where post_slug='$slug' ";
    $run_slug = mysqli_query($con,$get_slug );
    $check = mysqli_num_rows($run_slug );

    if($check == 1)
    {
        $slug_new = $slug.'ab'; // Add 2 characters at the end
        $update ="UPDATE slug SET post_slug = '$slug_new' WHERE post_slug = '$slug'";
        $run = mysqli_query($con,$update);
    }
    else
    {
        $insert ="insert into slug (post_slug) values ('$slug') ";
        $run = mysqli_query($con,$insert);

        if($run)
        {
            echo "<p style='float:right;'> Posted  successfully </p>";
        }
    }
}
?>

是否要在添加字符串后第二次更新“插入另一个”的现有值用于检查数据库中的所有可能性检查此项是,如果已经存在,我要更新它扫描我不正常添加其他条件吗?我删除了它以使代码更紧凑并消除不必要的条件。如果你能解释你为什么需要其他条件或你想要实现什么,可以帮助你。OOB/功能+1。对于重复的进程来说,函数是惊人的。节省时间,代码干净易读,维护简单。一个位置,而不是梳理和希望您找到所有重复的代码块。