表单提交后生成引用ID

时间:2014-09-10 10:47:50

标签: php mysql for-loop

每次用户提交表单并插入表单时,我都会尝试生成类似此CTS-P 0 then CTS-P 1的引用ID。

我提出的是在我提交时将CTS-P 0插入数据库。但问题是我再次提交后不会递增CTS-P 0 to CTS-P 1

我尝试使用mysql_insert_id()这是我到目前为止所做的事情。这是最小的事情,但我无法解决。请看看

    <?php 
$con=mysql_connect("localhost","root","");
mysql_select_db("dbname",$con);
$genid="";

if(isset($_POST['submit'])) {   
$frstname=$_POST["frstname"];   

$genid=mysql_insert_id();
$genid .=count($genid);
//echo $genid;

for($i=0; $i<$genid; $i++) {

$sql = "INSERT INTO tblname (`namecol`,`refidcol`) VALUES ('$frstname','CTS-P $genid[$i]')";
$result = mysql_query($sql);
}}
    ?>

//here is the form
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="generate" >

First Name<input type="text"  name="frstname" />
<input type="submit" name="submit" value="Submit" />
</form>

它将它作为CTS-P 0插入我的refid列中但是从下次提交时没有递增。我知道它非常noobish但我被卡住了。

1 个答案:

答案 0 :(得分:1)

<?php 
    $con=mysql_connect("localhost","root","");
    mysql_select_db("dbname",$con);

    if(isset($_POST['submit'])) {   
        $frstname=$_POST["frstname"];   

        $sql = "SELECT * FROM tblname";
        $genid = mysql_query($sql, $con);
        $genid = mysql_num_rows($genid);

        //Since you're using "0" as your first number, I decided to comment this out, if not, uncomment it
        //$genid++;

        $sql = "INSERT INTO tblname (`namecol`,`refidcol`) VALUES ('$frstname','CTS-P $genid')";
        $result = mysql_query($sql);
    }
?>