我正在开发一个数据库/ php项目。 数据库中有三个表...一个有roleId和role ..第二个有userId和username ...所以这两个表都有一个用户名和角色的下拉列表...当用户做出选择时,数据应保存在具有roleId和userId的第三个表中。
Table1 : roleId, role
Table2: userId, username
Table3: roleId, userId
怎么做?
这是我到目前为止所尝试的......
<?php
require_once './queries.php';
$roleId = filter_input(INPUT_POST, "roleId");
$userId= filter_input(INPUT_POST, "userId");
$addname = filter_input(INPUT_POST, "addname");
$addrole = filter_input(INPUT_POST, "addrole");
$deletename = filter_input(INPUT_POST, "deletename");
$deleterole = filter_input(INPUT_POST, "deleterole");
$switch = filter_input(INPUT_POST, "switch");
switch ($switch) {
case 'add':
echo Queries::addRole($roleId, $role);
break;
}
?>
queries.php
public static function addRole($userId, $roleId) {
$dbUser = "";
$dbPass = "";
$dbConn = "(DESCRIPTION = (ADDRESS = (PROTOCOL=TCP)(HOST=)(PORT=))(CONNECT_DATA=(SID=)))";
$conn = oci_connect($dbUser, $dbPass, $dbConn);
$sql = oci_parse($conn, "INSERT INTO ROLE (roleId, userId) VALUES (:roleid, :userId)");
oci_bind_by_name($sql,':roleId', $roleId);
oci_bind_by_name($sql,':userId', $userId);
oci_execute($sql);
}
的.js
$("#selectrole").submit(function (event) {
event.preventDefault();
$.ajax({
url: 'functions/role-functions.php',
type: 'POST',
data: {"roleId": $('#roleId').val(),"userId": $('#userId').val(), "switch": "add"},
dataType: "json",
success: function () {
$('#selectdept').trigger('reset');
$("#userId").val('').trigger('change');
resultAlert('#adminResult', '#resultAdminContent', '<strong>You data was succefully saved!</strong>', 'alert-success');
}, error: function () {
resultAlert('#adminResult', '#resultAdminContent', '<strong>Error! Your data was not saved!</strong>', 'alert-danger');
}
});
});
形式
<div class="container">
<div class="panel panel-default">
<div class="panel-body">
<div id="selectrole" class="hide" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<div id="resultActivityContent"></div>
</div>
<form class="cmxform" id="addroles">
<legend>Add Roles</legend>
<label for="names">Select Name</label>
<select class="form-control" name="names" id="names">
<option selected disabled></option>
<?php
$dbUser = "";
$dbPass = "";
$dbConn = "(DESCRIPTION = (ADDRESS = (PROTOCOL=TCP)(HOST= )(PORT= ))(CONNECT_DATA=(SID= )))";
$conn = oci_connect($dbUser, $dbPass, $dbConn);
$sql = oci_parse($conn, "SELECT USER.USERNAME FROM USER");
oci_execute($sql, OCI_DEFAULT);
?>
<?php
while (oci_fetch($sql))
{ ?>
echo '<option value="<?php echo oci_result($sql, 'USERNAME')?>"><?=oci_result($sql,'USERNAME') ?></option>
<?php
}?>
</select>
<br>
<br>
<label for="role">Select Role</label>
<select class="form-control" name="role" id="role">
<option selected disabled></option>
<?php
$dbUser = " ";
$dbPass = " ";
$dbConn = "(DESCRIPTION = (ADDRESS = (PROTOCOL=TCP)(HOST= )(PORT= ))(CONNECT_DATA=(SID= )))";
$conn = oci_connect($dbUser, $dbPass, $dbConn);
$sql = oci_parse($conn, "SELECT USERROLE.ROLED FROM USERROLE");
oci_execute($sql, OCI_DEFAULT);
?>
<?php
while (oci_fetch($sql))
{ ?>
echo '<option value="<?php echo oci_result($sql, 'ROLE')?>"><?=oci_result($sql,'ROLE') ?></option>
<?php
}?>
</select>
<br>
<button class="btn btn-info btn-primary" type="submit">Add Role</button>
<br>
<br>
</form>
</div>
</div>
</div>
答案 0 :(得分:0)
是的,你可以。 我不是PHP开发人员,但我认为第3个表中保存(INSERT)值的操作将由表单中的操作触发。
如果是这样,您应该根据roleId
和userId
选择role
和username
为2个变量(例如:v_roleId
和v_userId
)然后,使用这两个变量执行INSERT into
第3个表。
如果没有,请提供更多详细信息,代码部分,您尝试过的所有内容......