更改变量的变量

时间:2014-01-21 10:09:28

标签: php sql

我正在尝试处理一个大型表格并遇到一些绊脚石。我已经尝试谷歌的答案,但我不太确定我的措辞是否正确。我的代码看起来像这样。

<?PHP

$exchange = $_POST['exchange'];
$estimate = $_POST['estimate'];
$wp = $_POST['wp'];
$label1 = $_POST['name3'];
$result1 = $_POST['fb1'];
$result2 = $_POST['fb2'];

$username = "-----";
$password = "-----";
$hostname = "-----";

$con = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$selected = mysql_select_db("-----", $con) or die("Could not select examples");



$query = "UPDATE btsec SET Status='$result1', TM='result2' WHERE Exchange='$exchange' AND Estimate='$estimate' AND WP='$label1' AND SectionID='$label1'";

if (!mysql_query($query,$con))
    {
    die('Error: ' . mysql_error($con));
    }
}   

echo "Sections updated for WP's $wp on $estimate on the $exchange Exchange!";

mysql_close($con);

?>

我需要做的是遍历查询,但每次都改变变量的内容。

$label1 = $_POST['name3']; needs to become $label1 = $_POST['name6'];
$result1 = $_POST['fb1']; needs to become $result1 = $_POST['fb10'];
$result1 = $_POST['fb2']; needs to become $result1 = $_POST['fb11'];

正如我所说,谷歌无法弥补我的不良措辞。

1 个答案:

答案 0 :(得分:0)

最好的解决方案是更改表单输入,使它们作为数组工作:

<input type="text" name="name[3]">
<input type="text" name="name[6]">
<input type="text" name="name[9]">


<input type="text" name="fb[1]">
<input type="text" name="fb[10]">
<input type="text" name="fb[19]">

然后,当您提交表单时,您可以迭代数据:

foreach ($_POST['name'] as $index => $name)
{

}

foreach ($_POST['fb'] as $index => $fb)
{

}

作为旁注,您还应该考虑使用prepared statements,或者至少使用escaping数据 - 您将面临SQL injection的风险。

相关问题