logo
down
shadow

How do I get the just inserted row of a MySQL table in PHP?


How do I get the just inserted row of a MySQL table in PHP?

By : john
Date : November 22 2020, 10:33 AM
it fixes the issue The short answer is no, there's no builtin functionality for the behavior you describe.
It would be possible, however, to set a user-defined variable within a BEFORE INSERT or AFTER INSERT trigger, e.g.
code :
SET @new_mytable_identifier_val = NEW.identifier ;
SELECT @new_mytable_identifier_val
SET @new_mytable_identifier_val = '';
INSERT INTO mytable (col) VALUES (val);
SELECT @new_mytable_identifier_val;
SET @new_mytable_identifier_val = '';
SELECT get_mytable_new_identifier('fee','fi','fo') AS newid;

newid
-----
42
INSERT INTO mytable (identifier,b,c,d,...) VALUES (42,'fee','fi','fo',...)


Share : facebook icon twitter icon
mysql, java - Get last inserted auto increment id from one table and insert it again on another table

mysql, java - Get last inserted auto increment id from one table and insert it again on another table


By : kalaijagdai
Date : March 29 2020, 07:55 AM
To fix this issue Use getGeneratedKeys() method from your Statement object to identify the new auto generated values. Iterate the returned ResultSet object to get the newly generated key values in the order of batch statements.
Change:
code :
stmt.executeUpdate(sql);
int rowsAffected = 
  stmt.executeUpdate( sql, Statement.RETURN_GENERATED_KEYS );  
ResultSet rs = stmt.getGeneratedKeys();  

//******************************************************  
// in case of batch insert, you can check how many are inserted
rs.last();  
int rows = rs.getRow();  
System.out.println( "Generated keys count: " + rows );  
//******************************************************/  

int currentRow = 1;  
rs.beforeFirst();  
while( rs.next() ) {  
    System.out.println( /**/( currentRow++ ) + " = " + /**/rs.getInt( 1 ) );  
} // while rs  
java,mysql - why not working get last inserted id from one table and insert it again on another table?

java,mysql - why not working get last inserted id from one table and insert it again on another table?


By : edXiao
Date : March 29 2020, 07:55 AM
may help you . Its duplicate Question. I put again coz it gives me error , I solve the problem in this way....
code :
public void groupCreation(String groupname, int grouptype, int creator_id) {

    DatabaseService oDatabaseService = new DatabaseService();
    Connection connection = oDatabaseService.connect();
    Statement stmt = null;
    Statement stmt2 = null;
    Statement stmt3 = null;
    try {
        //Statement stmt = null;            
        stmt = connection.createStatement();
        String sql;


        sql = "INSERT INTO M_GROUPS(GROUP_NAME,GROUP_CREATOR_ID,GROUP_TYPE,CREATION_TIME)"
                + " VALUES ('"
                + groupname
                + "','"
                + creator_id
                + "','"
                + grouptype + "',NOW())";

       stmt.executeUpdate(sql);
       stmt.close();



    stmt2 = connection.createStatement();
    String sql2;

    sql2 ="select max(group_id)as group_id from m_groups where group_creator_id='"+ creator_id+ "'";

    //System.out.println("before rs for sql2");
    ResultSet rs = stmt2.executeQuery(sql2);
    //System.out.println("after rs for sql2");

    int groupId = 0 ;

    while (rs.next()) {
    groupId =rs.getInt("group_id");
    }

    System.out.println(groupId);

    stmt3 = connection.createStatement();
    String sql3;

     sql3 = "INSERT INTO M_GROUP_MEMBERS(GROUP_ID,MEMBER_ID,JOIN_TIME)"
            + " VALUES ('"
            + groupId
            + "','"
            + creator_id
             + "',NOW())";

    stmt3.executeUpdate(sql3);


    } catch (SQLException se) {
        se.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try{
             if(stmt!=null)
                stmt.close();
          }catch(SQLException se2){
          }// nothing  can do
        try{
             if(stmt2!=null)
                stmt2.close();
          }catch(SQLException se3){
          }// nothing  can do

        try{
             if(stmt3!=null)
                stmt3.close();
          }catch(SQLException se4){
          }// nothing  can do
        try {
            if (connection != null)
                connection.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }
}
Mysql trigger-Increasing a table's field by 1 every time sth is inserted in another table

Mysql trigger-Increasing a table's field by 1 every time sth is inserted in another table


By : mac.abhinav
Date : March 29 2020, 07:55 AM
may help you . Your ifnull() arguments are backwards. it's IFNULL(value_to_test, value_to_use_if_null). You're testing if 0 is null, which it isn't, so you're effectively doing
code :
... SET NO_EMP = 1 + 0
... SET NO_EMP = IFNULL(NO_EMP, 0) + 1
MYSQL Trigger when a order is inserted a notification inserted into another table

MYSQL Trigger when a order is inserted a notification inserted into another table


By : S_M
Date : March 29 2020, 07:55 AM
Hope this helps I have a table called Order and when a order gets inserted into it i want it to insert the sellerID(userID in the notifications) and productName combined with a message to be stored in Notification.Message. , Try:
code :
DELIMITER $$

CREATE TRIGGER `productTrigger` AFTER INSERT ON `Order`
FOR EACH ROW
BEGIN
  /*DECLARE ui,pn VARCHAR(50);

  SELECT productName INTO pn FROM Order;
  SELECT userID INTO ui FROM Product;
  INSERT INTO Notifications(userID,Message) VALUES(ui,pn);*/

  INSERT INTO `Notifications`
    (`userID`, `Message`)
  VALUES
    (NEW.`SellerID`, NEW.`productName`);
END$$

DELIMITER ;
Php: fetching last inserted row on mysql table to use for another mysql statement

Php: fetching last inserted row on mysql table to use for another mysql statement


By : Moché Uzan
Date : March 29 2020, 07:55 AM
I hope this helps you . I need to fetch the id of the last inserted row from a mysql table using php and use that id to enter a new entry on another table. I have this until now , Thing this is the main issue:
code :
$prep = $db_usr->prepare($query);   
$lastid = $prep->fetch();
$prep = $db_usr->prepare($query);   
$prep->execute();
$lastid = $prep->fetch();
Related Posts Related Posts :
  • How to make a sitemap using zend framework 1
  • Laravel: array to Model with relationship tree
  • $_SERVER['DOCUMENT_ROOT'] vs $path = $_SERVER['DOCUMENT_ROOT']
  • What is the best way to store multiple checkbox values in the database and work with it later on
  • Why does foreach copy the array when we did not modify it in the loop?
  • Receive Jquery/Ajax Post Request in PHP
  • How to better use the if(!empty()){ //codes} statement
  • PHP error mysql_close() boolean given
  • Move an image file from one server to another using PHP
  • PHP: Encoding non ISO-8859-1 charactrers into utf8
  • Handling MySQL errors in PHP
  • Object orientated mysql num_rows query in one line
  • Remove not closed html elements from end of text
  • Generate random ENUM value [PHP, MySQL]
  • Deleting data from table using jQuery and SQL
  • How to make sure the constructor is executed when I'm trying to reach a static property?
  • Trying to understand arrow next to PHP function
  • Form using external php file to show "Sucess"
  • WooCommerce - new shipping method - how to query individual product weights
  • Common integer representation over variable casting?
  • PHP - Prevent client from tampering the ID of a form
  • unexpected T_PUBLIC error
  • Types of testcases in PHPUnit
  • keeping a LDAP user logged in with PHP session
  • Detect if browser supports WebP format? (server side)
  • real_escape_string not cleaning up entered text
  • E-commerce products filter
  • Why having more than one test method in a file using Laravel raises error?
  • PHP Escaped special characters to html
  • Basic prepared statement not INSERTING
  • PHP combobox not showing in HTML table
  • MySql PHP row request to variable
  • Join two tables with one table has multiple rows matching
  • Looping through users with Instagram API requests is REALLY slow
  • Unable to add 30 days to date within loop
  • Return from mysqli_query() into array
  • php rename() function returns "No such file or directory" error
  • form submit inside navigation menu
  • PHP: reasons to use GET and POST request at the same time?
  • Is break; required after die() php
  • little depth of session in php
  • Authentication Logon page not working using PHP and Oracle on Apache / Windows 2008
  • Magento reindexing error product flat data
  • Laravel 4: Running composer when deploying to Heroku
  • Long Polling or WebSockets
  • How to use mysqli_stmt_bind_param for integers, boolean, etc
  • Determining the content of a link
  • MYSQL and PHP update events?
  • PHP explode all square bracket
  • Transparent hexagon-masked collage using PHP GD
  • Plugins or text do NOT show up on custom wordpress template
  • inserting data into mysqli database
  • Two arguments to closure function laravel
  • UnClear image exported by ffmpeg from mp4 file
  • is the time of computer and localhost same?
  • change custom data- value after ajax post success
  • Can We Restrict PHP Variables to accept only certain type of values
  • csv file is inserting a blank row in the last of other rows into mysql table
  • How to convert special characters in xml using php?
  • Mcrypt are very confusing
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org