How To Add Characters To The End Of The Value If It Already Exists?
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. But I want to add 2/3 characters at the
Solution 1:
just run update query if $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 ();
}
Solution 2:
This is helpful for you
<?php$con = mysqli_connect("localhost","root","","post" ) ordie
( "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>";
}
}
publicfunctioncheck_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)
{
returntrue;
}
else
{
returnfalse;
}
}
?>
Solution 3:
Just few modification in your code to insert new value.
<?php$con = mysqli_connect("localhost","root","","post") ordie("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>";
}
}
}
?>
Post a Comment for "How To Add Characters To The End Of The Value If It Already Exists?"