我在一个页面上发布了一个选择选项,在我的实际服务器代码(PHP)上,我想首先获取POST名称,例如,$ _POST [&#39 ; selection_value']其次,检查它是否等于选择中的值,如:if($ _ POST [' selection_value'] ==' time' ){}。 (时间是选择选项的值)。我该怎么做呢?
我的POST抓取代码:
标记
<div class="form-group" style="padding-left: 4%;">
<div class="form-group m-r-12">
<label class="col-sm-12 control-label";">Add User</label>
</div>
<input type="text" name="username" id="username" placeholder="Username" class="form-control" required />
<input type="text" name="email" id="email" placeholder="Email" class="form-control" required />
<input type="text" name="cpukey" id="cpukey" placeholder="CPUKey" class="form-control" required />
<button onclick="addUser()" class="btn btn-success"><i class="fa fa-user-plus"></i> Add User</button>
<select class="form-control" id="selection_value" name="selection_value">
<option value="account_credits">Account Credits</option>
<option value="free_gifted_credits">Free Gifted Credits</option>
<option value="time">Member Server Days</option>
</select>
<input type="text" name="add_to_all_value" id="add_to_all_value" placeholder="Value to add to current" class="form-control" required />
<button id="button1" onclick="add_to_all()" class="btn btn-primary"><i class="fa fa-user-plus"></i> Add To All Users</button>
AJAX
function add_to_all() {
myOutput = document.getElementById('add_user_result');
var member_selection = $('#selection_value :selected').text()
var member_value = $('#add_to_all_value').val();
if(member_selection != "" & member_value != "") {
$.ajax ({
type: "POST",
url: 'includes/ajax_data_add_to_all.php',
data: { selection: member_selection, value: member_value },
success:function(response) {
$("#add_user_result").show();
$('#add_user_result').fadeOut(3000).html(response);
header('Location: admin_members.php');
},
error: function() {
$("#add_user_result").show();
$('#add_user_result').fadeOut(3000).html(response);
header('Location: admin_members.php');
}
});
} else {
$("#add_user_result").show();
$('#add_user_result').fadeIn(3000).fadeOut(3000);
myOutput.innerHTML = "<font style='color: red;'>You must fill in all the blanks.</font>";
}
return false;
}
现在我的添加时间代码(服务器代码):
function grab_all_time() {
global $con;
$usersTime = "SELECT time FROM users";
$result = $con->query($usersTime) or die("Error");
while ($row = mysqli_fetch_assoc($result)) {
return $row['time'];
}
}
$AllusersTime = grab_all_time();
if(!empty($_POST) && isset($_POST)) {
$selection = mysqli_real_escape_string($con, $_POST['selection']);
$value = mysqli_real_escape_string($con, $_POST['value']);
$membersDate = new DateTime($AllusersTime);
$membersDate->add(new DateInterval('P'.$value.'D'));
$finishedDT = $membersDate->format('Y-m-d') . "\n";
if($selection == 'Member Server Days') {
$insert_query = mysqli_query($con, "UPDATE users SET '".$selection."' + '".$value."'");
if($insert_query) {
echo '<font style="color: green;">Successfully Added to all users</font>';
echo '<META HTTP-EQUIV="Refresh" CONTENT="3; URL=admin_members.php">';
}
else {
echo '<font style="color: red;">Failed to add to all users</font>';
echo '<META HTTP-EQUIV="Refresh" CONTENT="3; URL=admin_members.php">';
}
} else {
$insert_query = mysqli_query($con, "UPDATE users SET time = '".$finishedDT."'");
if($insert_query) {
echo '<font style="color: green;">Successfully Added to all users</font>';
echo '<META HTTP-EQUIV="Refresh" CONTENT="3; URL=admin_members.php">';
}
else {
echo '<font style="color: red;">Failed to add to all users</font>';
echo '<META HTTP-EQUIV="Refresh" CONTENT="3; URL=admin_members.php">';
}
}
答案 0 :(得分:0)
要在函数grab_all_time
中返回查询的所有记录,您可以使用数组,将每个结果添加到它并返回该数组:
function grab_all_time() {
global $con;
$usersTime = "SELECT time FROM users";
$result = $con->query($usersTime) or die("Error");
$results = array();
while( $row = mysqli_fetch_assoc($result) ) {
$results[]=$row['time'];
}
return $results;
}
这显然会返回一个数组,因此您不能简单地将返回值指定为DateTime
对象,您稍后会继续这样做。如果它只是您需要处理的查询的结果,那么为什么要获取所有行?
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['selection'], $_POST['value'] ) ){
$selection = mysqli_real_escape_string( $con, $_POST['selection'] );
$value = mysqli_real_escape_string( $con, $_POST['value'] );
$AllusersTime = grab_all_time();
foreach( $AllusersTime as $i => $time ){
$membersDate = new DateTime( $time );
$membersDate->add( new DateInterval('P'.$value.'D') );
$finishedDT = $membersDate->format('Y-m-d') . "\n";
/* .... the rest of you code .... */
/* dont add the `meta refresh` */
}
}
还有一点与您实际想做的事情相混淆 - 希望我已经正确理解,因为您希望根据所选的值和选项更新每个用户的记录。使用update语句的where子句使用每个记录中的预先存在的内容单独更新每条记录似乎是合乎逻辑的。
我认为我的困惑源于从函数中的users表中检索数据的sql。它最初的结构方式意味着尽管选择了所有记录,但您只返回记录集中的第一个记录,并将其用作剩余代码的基础。如果,我说IF,你打算返回所有记录并单独处理它们,然后从你的函数返回一个数组似乎是最好的选择。
您将注意到我(再次)修改了您的函数以获取与每条记录相关联的user_id
- 将其替换为正确的列名。
function grab_all_time() {
global $con;
/* NOTE: assumed a column `user_id` !! */
$usersTime = "SELECT `user_id`,`time` FROM `users`;";
$result = $con->query($usersTime) or die("Error");
$results = array();
while( $row = mysqli_fetch_assoc($result) ) {
/* NOTE: assumed column `user_id` !! */
$results[ $row['user_id'] ]=$row['time'];
}
return $results;
}
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['selection'], $_POST['value'] ) ){
$selection = mysqli_real_escape_string( $con, $_POST['selection'] );
$value = mysqli_real_escape_string( $con, $_POST['value'] );
$AllusersTime = grab_all_time();
$results = array();
foreach( $AllusersTime as $user_id => $time ){
$membersDate = new DateTime( $time );
$membersDate->add( new DateInterval('P'.$value.'D') );
$finishedDT = $membersDate->format('Y-m-d');
/* .... the rest of you code .... */
if( $selection == 'Member Server Days' ) {
$sql='update `users` set `'.$selection.'`=`'.$selection.'` + '.$value.' where `user_id`="'.$user_id.'";';
} else {
$sql='update `users` set `time`="'.$finishedDT.'" where `user_id`="'.$user_id.'";';
}
$results[ $user_id ]=mysqli_query( $con, $sql ) ? 'Success' : 'Fail';
}
echo '<pre>',print_r( $results, true ),'</pre>';
echo '<META HTTP-EQUIV="Refresh" CONTENT="3; URL=admin_members.php">';
}