我一直在寻找这个答案大约一个小时,找不到我要找的东西......
我在INSERT表单上有3个相关的选择框。(drop_1,drop_2,drop_3)它们的每个值(int)代表数据库中名为category
的相同字段。
我正在寻找一种方法,只选择三者中的最高值作为插入category
字段的值。
这是我一直在尝试的(显然不起作用):
$drop_1 = ($_POST['drop_1']);
$drop_2 = ($_POST['drop_2']);
$drop_3 = ($_POST['drop_3']);
$category = max($drop_1, $drop_2, $drop_3);
仅当我从3个框中的每个框中选择一个选项时才有效,但是如果我只从1个或2个框中选择一个选项,则表单将以空白值提交给DB。
答案 0 :(得分:0)
你的php似乎很好:
<?php
$_POST['drop_1'] = 100;
$_POST['drop_2'] = null;
$drop_1 = ($_POST['drop_1']);
$drop_2 = ($_POST['drop_2']);
$drop_3 = ($_POST['drop_3']);
$category = max($drop_1, $drop_2, $drop_3);
echo $category;
会输出:
100
所以问题在于表格。
答案 1 :(得分:0)
$drop_1 = mysql_real_escape_string($_POST['drop_1']);
$drop_2 = mysql_real_escape_string($_POST['drop_2']);
$drop_3 = mysql_real_escape_string($_POST['drop_3']);
$query = "INSERT INTO table1 (category)
SELECT GREATEST('$drop_1', '$drop_2', '$drop_3') as new_cat ";