简体   繁体   中英

PHP Mysql: Select column and add last current value with a new one

i am trying to add the last value from a specific column in mysql with a new value.

$result return me nothing.

ex: last value=100 , value5=50, new value should be 150.

        // Read values into table.
        $sql = "SELECT specific_column FROM data WHERE id=LAST_INSERT_ID()";
        $result = mysqli_query($conn, $sql);

        // Insert values into table.
        $sql = "INSERT INTO data (column1, column2, column3, column4, specific_colum)
        VALUES ($value1, $value2, $value3, $value4, $result+$value5 )";

$result is mysqli object .you can not get column value directly.You need to fetch row values first like this..

$row = mysqli_fetch_assoc($result);
$last_value = $row['specific_column'];
$update_value = $last_value+$value5;//now update $update_value in database

The mysqli_fetch_assoc() function fetches a result row as an associative array.

Are you using MysQLi?

Have you tried a :

$sql = "INSERT INTO ....";

if ($conn->query($sql) === TRUE) {
    $last_id = $conn->insert_id;
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

Or:

if (mysqli_query($conn, $sql)) {
    $last_id = mysqli_insert_id($conn);
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

Then use $last_id as you wish

LAST_INSERT_ID() does not return any values which you instered in row.but it only return ROW ID which you instered or updated in form of 0,1,2 etc or Returns the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement.

uses:-

Use this function after you have performed an INSERT statement into a table that contains an AUTO_INCREMENT field, or have used INSERT or UPDATE to set a column value with LAST_INSERT_ID(expr).

For more reference

Here, LAST_INSERT_ID will not return anything

As LAST_INSERT_ID() returns a BIGINT UNSIGNED (64-bit) value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement . The value of LAST_INSERT_ID() remains unchanged if no rows are successfully inserted.

So, in place of LAST_INSERT_ID you can use MAX

$sql = "SELECT specific_column FROM data WHERE id= (select MAX(id) from data)";
$result = mysqli_query($conn, $sql);

if you just need to update specific_column value with some N number then try this

UPDATE data SET
    specific_column  = @specific_column  := specific_column + $value5
WHERE ..... ;

So $value5 =50 then specific_column will be 100+50

$sql = "SELECT specific_column FROM data WHERE id= (select MAX(id) from data)";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$sql = "INSERT INTO data (column1, column2, column3, column4, specific_colum)
        VALUES ($value1, $value2, $value3, $value4, $row['specific_colum']+$value5 )";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM