title: How to generate new random value if it already exists in the database?
body: <p>How to automatically generate new random value in textbox if the value already exists in the database?</p>
  <p>An example of my attempt:</p>
  <pre><code>&lt;?php
  $length = 1;
  $randomString = substr(str_shuffle(&quot;123456789&quot;), 0, $length);
  
  $mysqli1 = new mysqli(&quot;localhost&quot;, &quot;root&quot;, &quot;&quot;, &quot;k&quot;);
  
  $sql = $mysqli1-&gt;query(&quot;SELECT catid FROM subcat WHERE catid = '$randomString'&quot;);
  if(($sql-&gt;num_rows) &gt; 0){
    echo &quot;User id exists already.&quot;;
  }
  echo '&lt;input name=&quot;id&quot; value=&quot;'.$randomString.'&quot;&gt;';
  ?&gt;
  </code></pre>