简体   繁体   中英

decrement an auto increment column?

ok so i have this script which either updates or inserts the values into mysql and also auto increments a column and sets an enum value from 0 to 1.

Now im trying to work out how i might reverse it, so that the enum value is set back to 0 (which i think i know how to do) also i need to figure out how i can decrement or whatever the opposite of increment would be, is it possible to -1 in an auto increment column?

the values dont necessarily need to be deleted as long as i can decrement the auto increment column and chang ethe enum value from 1 to 0.

can anyone show me please how i might do this? thanks.

<?php

    require_once('includes/session.php');
    require_once('includes/functions.php');
    require('includes/_config/connection.php');

    session_start();

        confirm_logged_in();

        if (isset ($_GET['to'])) {
        $user_to_id = $_GET['to'];


    }


    if (!isset($_GET['to']))
        exit('No user specified.');

    $user_id = $_GET['to'];


    $result = mysql_query("SELECT * FROM ptb_likes WHERE liked_id ='".$user_to_id."' ");

    if( mysql_num_rows($result) > 0) {
    mysql_query("UPDATE ptb_likes SET likes = likes +1 WHERE liked_id = '".$user_to_id."' ");


        $user_to_id = mysql_query("ALTER TABLE likes AUTO_INCREMENT = $id");
    }
    else
    {
        mysql_query("INSERT INTO ptb_likes (user_id, liked_id) VALUES (".$_SESSION['user_id'].", ".$user_to_id.")"); 

    }



    if($result) 
    { 
    mysql_query("UPDATE ptb_likes SET user_id_has_liked='1' WHERE user_id=".$_SESSION['user_id']."") 
    or die(mysql_error());

    header("Location: {$_SERVER['HTTP_REFERER']}");

    }
    ?>

As suggested elsewhere

create table b
(
id int not null auto_increment primary key,
foo boolean not null
);

 insert into b (foo) values (true), (false), (false),  (true), (true);

update b set
foo = not foo;

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