插入位标志值

时间:2013-11-16 00:10:08

标签: php mysql

我有一个表单,用户可以在其中为instructor和/或faculty人输入名称。根据提交的字段,我想将值存储到person表中。如果提交了教师和教师值,我想为每个值创建一个不同的行,并且能够识别他们的角色instructorfaculty。我在表格中有点标记来帮助确定role。但是如何在表中存储位值以确定它是instructor还是`faculty?

<?php

if(isset($_POST['submit'])){

    $bitmask = 1|2;

    $db_con = new mysqli(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
    $db_insert  = $db_con->prepare("INSERT INTO person (`person_name`,`instructor_role`) 
        VALUES (?,?)"); 
    $db_insert->bind_param('s', $_POST['instructor_name'], $bitmask);
        $db_insert->execute();
}    ?>
<form action="index.php" method="POST">

Instructor Name:<input type="text" name="instructor_name">
Faculty Name:<input type="text" name="contact_name">


<input value="SAVE" name="submit" type="submit">

</form>

CREATE TABLE IF NOT EXISTS `person` (
  `person_id` int(11) NOT NULL auto_increment,
  `contact_role` bit(8) NOT NULL,
  `instructor_role` bit(8) NOT NULL,
  `person_name` varchar(50) NOT NULL,
  PRIMARY KEY  (`person_id`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

1 个答案:

答案 0 :(得分:1)

这就是你想要的:

$bit =1;
$bit2 = 2;
$bit3= 4;
$bit4= 8;
//1+2+8=11
$result = $bit | $bit2 |$bit4;
//this is still 11
$result = $result | $bit;

如果您需要SQL部分,请告知,将更新

取决于您如何定义教师价值和教师

我将假设教师是1,教师是2

不确定是否是我或其他东西,所以请检查

$db_insert  = $db_con->prepare("INSERT INTO person (`person_name`,`instructor_role`) 
            VALUES (?,?)"); 
            $db_insert->bind_param('s', $_POST['instructor_name'] || $_POST['faculty_name']);
    $db_insert->bind_param('i',1|2);

$db_insert->execute();