PHP,MYSQL选择行更新

时间:2017-03-29 23:05:43

标签: php mysql mysqli

我想更改所选图像的ID信息。我想让ID号比最后一个ID更多。

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>
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);
?>

1 个答案:

答案 0 :(得分:1)

$lastunilogoid不是ID的有效,它是mysqli_result对象。您需要致电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'];

但是你可以在一个查询中完成整个事情,你应该使用预备语句来完成它。

$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);