2

i created a application form and it has application no field. but it should be hidden. this application no should be auto increment unique number. i use date() and mysql_insert_id() method to generate that. but always show 0 value in my table. i have posted my code. please anyone can explain me..what is the error..about this case. this code is successfully inseterd. but did not show correct output. always show 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();


    //

}

3 Answers3

1

You need to make this while making your mysql database. For example:

    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 ;

That way you will have ID column that will auto increase with 1 every time you add a database record. You do not need to do it manually, as when you use $_POST method, it will be with unique ID (for example 1, 2, 3, 4, .. etc..).

  • i already created a table and it is name is tbl_loan_application and these data should be go to that table..are you mean other tabe? –  Jun 15 '14 at 12:52
  • No, create your table using the model above. with this row: ["primary key (`Id`)"] and with the last line – user3214661 Jun 15 '14 at 12:55
  • You should use the auto increment property on one of your columns (the one you use for 'id') then it will automatically increment properly - you can add this at any time, it doesn't need to be when you first create the table. – Jonathan Jun 15 '14 at 13:53
0

First of all, consider dropping mysql, in favour of mysqli or PDO, since the mysql functions are deprecated. More info here.

Second of all, the function you use returns the ID of the LAST query you ran, as stated here, thus it will return 0 if the last query didn't generate a new ID.

To start studying mysqli or PDO, you can consider the official MySQL documentation, or the PHP documentation for PDO and mysqli.

Also on a related notice, it's a good practice to state the fields you wish to affect with insert, like this:

INSERT INTO table1(field_name1, field_name2, ... , field_nameN) VALUES(value1, value2, ... ,  valueN)

Suggestion for resolution: Insert them with some default appno, and add this trigger:

CREATE TRIGGER `UPDATES` AFTER INSERT ON `tbl_loan_application`
FOR EACH ROW BEGIN
    SET NEW.appno = CONCAT(CURDATE(), "-LN/SS-" , NEW.ID);
END

This will automatically assign the code after each insert, inside the database. I assumed that field names inside your DB are 'appno' for the code and 'ID' for your auto increment field.

EDIT:

How do Triggers work:

As stated here, triggers are 'stored function' which executes after or before event (update, insert or delete). The correct syntax is:

CREATE TRIGGER `Trigger_Name` [BEFORE/AFTER] [INSERT/UPDATE/DELETE] ON `table_name`
/* Some function usually with FOR EACH ROW */

the affected values are selected by using NEW.field_name or OLD.field_name. Those 2 selector return the value of the affected field before and after the action. So, by having this thing said, the example given in the answer does the following:

  1. Creates a trigger named 'UPDATES'
  2. Sets the event to fire after every time a insert statement is executed for the table 'tbl_loan_application'.
  3. The function selects all the rows inserted, one by one, and does the same thing you id with php, it joins CURDATE(), aka the current date, the string "-LN/SS-" and the ID of the line, providing you have one.

Triggers, just like tables, are written once, usually in the mysql console, but some people use other tools, like phpmyadin, to simplify the process.

Triggers come in handy in many situations and can edit not just the monitored table, but any other table as well.

Long story short, it's a code that get executed every time you edit/insert new/delete a row inside the specified table.

Community
  • 1
  • 1
Hristo Valkanov
  • 1,689
  • 22
  • 33
-1

use this function to generate an well formatted id.

mt_rand(10,100);
user3146425
  • 25
  • 2
  • 9