php - 如何使用 mysql 和 php 将唯一 ID 发送到数据库

标签 php mysql

我创建了一个申请表,它没有申请字段。但它应该被隐藏。此应用程序编号应该是自动递增的唯一编号。我使用 date() 和 mysql_insert_id() 方法来生成它。但总是在我的表中显示 0 值。我已经发布了我的代码。请任何人都可以解释我..错误是什么..关于这个案例。此代码已成功插入。但没有显示正确的输出。始终显示 0

if(isset($_POST['submitted'])){


    // catch data
    $loantype = $_POST['loantype'];
    $calcno = $_POST['calcno'];
    $memberid = $_POST['memberid'];
    $appno = date('Y-m-d')."-LN/SS-".mysql_insert_id();
    $amount = $_POST['amount'];
    $rental_type = $_POST['rental_type'];
    $apr = $_POST['apr'];
    $npy = $_POST['npy'];
    $flatrate = $_POST['flatRate'];
    $othercharges = $_POST['othercharges'];
    $repayment = $_POST['Repayment'];


    $payment = $_POST['payment'];
    $totpaid = $_POST['totpaid'];
    $intpaid = $_POST['intpaid'];

    date_default_timezone_set("Asia/Calcutta");
    $created = date('Y-m-d h:i:s');
    $status = "PENDING";
    $discription = "NOT DEFINED";
    $accepted_amount = "NOT DEFINED";


    //create new loan application
    $sql ='INSERT INTO tbl_loan_application VALUES (NULL,"'.$loantype.'","'.$calcno.'","'.$memberid.'","'.$appno.'","'.$amount.'","'.$rental_type.'","'.$apr.'","'.$npy.'","'.$flatrate.'","'.$othercharges.'","'.$repayment.'",
    "'.$created.'","'.$status.'","'.$discription.'","'.$accepted_amount.'","'.$payment.'","'.$totpaid.'","'.$intpaid.'")';


    /*else{

    }*/


        mysql_query($sql, $conn) or die(mysql_error());
        echo'<div class="alert alert-dsgn alert-success fade in" style="width:400px; align:center; margin-left:35%;     margin-top:5px;>
                            <button class="close" aria-hidden="true" data-dismiss="alert" type="button"></button>
                        <i class="fa fa-thumbs-up fa-3x"></i>
                        <article>
                        <h4>Nice! </h4>
                        <p>New Loan application successfully saved..<br /> </p>
                        </article>
                    </div>';
    echo '<a href="newloan.php"><p align="center">Back</p></a>';    
    echo '<p align="center">If you want to view loan application! click <a href="approved_loan_list.php">here</a></p>'; 
    echo mysql_insert_id();
    die();


    //

}

最佳答案

您需要在创建 mysql 数据库时进行此操作。例如:

    CREATE TABLE IF NOT EXISTS `tablename` (
      `Id` int(11) NOT NULL auto_increment,
      `Username` varchar(255) NOT NULL,
      `Password` varchar(255) NOT NULL,
      PRIMARY KEY  (`Id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

这样,每次添加数据库记录时,您的 ID 列都会自动增加 1。您不需要手动执行此操作,因为当您使用 $_POST 方法时,它将具有唯一 ID(例如 1、2、3、4、.. 等)。

关于php - 如何使用 mysql 和 php 将唯一 ID 发送到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24229720/

相关文章:

php - 如何在codeigniter中正确设置flash_message?

php - PHP 相当于 Perl 的正则表达式替换是什么?

javascript - 切换 div 重定向到带有 ID 的链接

mysql - 在 On 语句中使用 If 语句

mysql - 更改和优化 sql 查询

mysql - 如何建立好友关系?hibernate为什么要为ManyToMany关系创建一个新表

php - SQL-仅当列与当前时间相等时才选择行

php - 通过 PHP 启动 C 程序时出现段错误

java - 当从我的 Android 应用程序将数据保存到 mysql 时,它会删除空格

java - 来自 MYSQL DB 查询的编码字符串在 Java 中被破坏