简体   繁体   中英

enum value not updating in mysql table?

im using this script to update to columns in my table.

the first column 'close_account' is an enum value '0' or '1' and these appear as radio boxes in my database.

the second column 'account_status' is an enum value 'Active' or 'Deactivated' and this appears as a drop down box list in my database.

i'm not sure if the fact that they are a drop down box or radio boxes matters in this case.

however my problem is i am trying to get both close_account and account_status to update by running the following mysql query, close_account needs to update to '1' and account_status should update to 'Deactivated'. at the moment for some reason only 'close_account' is updating but not account_status can someone please tell me why?

thanks.

<? ob_start(); ?>
<?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_users WHERE user_id ='".$_SESSION['user_id']."' ");

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

mysql_query("UPDATE ptb_users SET account_status='Deactivated' WHERE user_id=".$_SESSION['user_id']."") 
or die(mysql_error());

header("Location: dashboard.php");

}
?>
<? ob_flush(); ?>

Using numeric values for enumerated data is always confusing. Especially when enumerated data is overlapping with enum index values.

In your case apparently 1 enum-data was interpreted as enum-index(numeric). Use strings for enum-data instead for better readability and usage.

Consider an example:

DROP TABLE enum_tb;

CREATE TABLE enum_tb (size ENUM('x-small', 'small', 'medium', 'large', 'x-large') NOT NULL);

INSERT INTO enum_tb
VALUES ('x-small'), (2), ('5');

SELECT * FROM enum_tb;

Output:

mysql> select * from enum_tb;
+---------+
| size    |
+---------+
| x-small |
| small   |
| x-large |
+---------+
3 rows in set (0.00 sec)

surprising?

MySQL stores the actual enum values when index is passed. Enum indexes begin from 1 and in the above case index 2 is small . Also '5' was considered as index to x-large .

The systematic way to approach problems like this is to first isolate the problem to PHP or to the database. The easiest way to do that is to go straight to the database without using PHP. Use the MySQL command-line utility ( mysql ), or use a graphical tool like phpmyadmin.

Use one of those tools to connect to your test database. (Not your production database. Always practice safe tests.) Run this query from a SQL prompt.

UPDATE ptb_users 
SET close_account='1', 
    account_status = 'Deactivated'
WHERE user_id = somenumber

Replace somenumber with an actual, existing user id number.

Having said that, it's unusual for an enum to be a number. It doesn't make much sense to have an enum declared like this.

create table foo (
  user_id integer not null,
  bar enum('0', '1')
);

Something like this is much more common.

create table foo (
  user_id integer not null,
  bar enum('open', 'closed')
);

To update such an enum, use the string values.

update foo
set bar = 'closed'
where user_id = somenumber and bar = 'open';

Again, replace somenumber with an actual user id number.

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