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

时间:2017-01-03 07:12:55

标签: javascript php jquery

当我点击发布按钮时,它会在数据库中插入一个随机值 如果数据库上已存在某个值,则显示错误。它工作正常。

但是如果它已经存在于数据库中,我想在值的末尾添加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>   

3 个答案:

答案 0 :(得分:1)

如果$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 ();

}

答案 1 :(得分:1)

这对你有帮助

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

?>

答案 2 :(得分:0)

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

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