如何添加多个$ _POST ['row']和变量? [英] How do I add on multiple $_POST['row'] and variables?

查看:93
本文介绍了如何添加多个$ _POST ['row']和变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



/ *想要添加更多的$ $ _POST [''] * /

  if(isset($ _ POST ['check_prof'])&& $ _POST [ 'check_prof'] =='checked'){
$ check_prof =checked;
} else {
$ check_prof =unchecked;
}

/ *同样在这里,想要添加另外五个以及* / b
$ b $ pre $ code $查询=UPDATE`用户`SET`check_prof` ='。 $ check_prof。 'WHERE`id` ='。 $ auth-> id。 '限制1;
mysql_query($ query,$ connection);
$ auth-> refresh();
}


解决方案

  $ fields = array('check_prof','check_this','check_that','check_whatever'); 
$ b $ foreach($ fields as $ field){
isset($ _ POST [$ field] && $ _POST [$ field] =='checked')){
$$字段=检查;
} else {
$$ field ='unchecked';
}

$ query =UPDATE`用户`SET`$ field` ='。 $$字段。 'WHERE`id` ='。 $ auth-> id。 '限制1;
mysql_query($ query,$ connection);
$ auth-> refresh();
}

顺便说一下,不要在数据库中存储checked和unchecked varchar),你可以将它存储为tinyint(1)类型,只需使用0或1.它将占用更少的空间。

另一件事,这将会做为每个字段分别查询。如果需要考虑性能问题,编写一个查询以对所有字段进行更改效率更高。


I am struggling to find out the syntactically correct way in which to add on more variables and rows to these statements:

/* WANT TO ADD ON FIVE MORE $_POST[''] */

if(isset($_POST['check_prof']) && $_POST['check_prof'] == 'checked') {
$check_prof = "checked";
}else{
$check_prof = "unchecked";
} 

/* SAME HERE, WANT TO ADD THE OTHER FIVE IN HERE AS WELL */

$query = "UPDATE `Users` SET `check_prof` = '" . $check_prof . "' WHERE `id` = '" . $auth->id . "' LIMIT 1";
    mysql_query($query,$connection);
    $auth->refresh();
    }

解决方案

Do you mean like this?

$fields = array('check_prof', 'check_this', 'check_that', 'check_whatever');

foreach($fields as $field){
    isset($_POST[$field] && $_POST[$field] == 'checked')){
        $$field='checked';
    } else {
        $$field='unchecked';
    }

    $query = "UPDATE `Users` SET `$field` = '" . $$field . "' WHERE `id` = '" . $auth->id . "' LIMIT 1";
    mysql_query($query,$connection);
    $auth->refresh();
}

By the way, instead of storing "checked" and "unchecked" in your database (as a varchar) you can store it as type tinyint(1) and just use a 0 or a 1. It will take up way less space.

Another thing, this will do a separate query for each field. It's more efficient to write one query to do the changes for all fields if performance is a concern.

这篇关于如何添加多个$ _POST ['row']和变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆