简体   繁体   中英

PHP, MYSQL select row update

I want to change the ID information of the selected image. I want to make the ID number more than the last ID available.

in HTML;

<form action="netting/operations.php" method="post">
<input type="hidden" value="<?php echo $unilogo['uni_logo_id'] ?>" name="uni_logo_id">
<button value="use" name="use_again">Use Again</button>
</form>

in PHP;

<?php
if(isset($_POST['use_again'])){
    $unilogoid= $_POST['uni_logo_id'];
    $lastunilogoid = mysqli_query($conn,"SELECT uni_logo_id FROM uni_logo ORDER BY uni_logo_id DESC LIMIT 1");
    $unilogosql = "UPDATE uni_logo SET uni_logo_id='".$lastunilogoid."' WHERE uni_logo_id='".$unilogoid."'";
    $unilogosira=mysqli_query($conn,$unilogosql,MYSQLI_USE_RESULT);
?>

$lastunilogoid isn't the valid of the ID, it's a mysqli_result object. You need to call mysqli_fetch_assoc :

$result = mysqli_query($conn,"SELECT uni_logo_id FROM uni_logo ORDER BY uni_logo_id DESC LIMIT 1");
$row = mysqli_fetch_assoc($result);
$lastunilogoid = $row['uni_logi_id'];

But you can do the whole thing in one query, and you should use a prepared statement to do it.

$stmt = mysqli_prepare($conn, "
    UPDATE uni_logo AS u1
    JOIN (SELECT MAX(uni_logo_id) AS max_id
          FROM uni_logo) AS u2
    SET u1.uni_logo_id = u2.max_id + 1
    WHERE u1.uni_logo_id = ?");
mysqli_stmt_bind_param($stmt, "i", $unilogoid);
mysqli_stmt_execute($stmt);

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