如果值已经存在,如何在值的末尾添加字符? [英] how to add characters to the end of the value if it already exists?

查看:68
本文介绍了如果值已经存在,如何在值的末尾添加字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,当我单击发布按钮时,它将在数据库中插入一个随机值.
如果数据库上已经存在一个值,则显示错误.它工作正常.

Here when I click post button it inserts a random value on database.
If a value already exists on database then show error. It works fine.

但是我想在值的末尾添加2/3个字符(如果它已经存在于数据库中).如果$check == 1,那么我想在值的末尾添加一些字符,而不是显示警报.该怎么做?

But I want to add 2/3 characters at the end of value if it already exists on database. If $check == 1 then I want to add some characters at the end of the value instead of showing alert. How to do this?

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

推荐答案

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

Just few modification in your code to insert new value.

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

这篇关于如果值已经存在,如何在值的末尾添加字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆