如何将 18 个变量传递到一个函数中以验证我的 PHP/MySQL 交互
How can I pass 18 variables into one function to validate my PHP/MySQL Interaction
我的网站上有一个个人资料页面,您可以在其中登录并更新您的帐户详细信息,例如:名字、姓氏、用户名、公司、地址行 1 等
见代码:
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$AccountID = $row["AccountID"];
$FName = $row["FName"];
$LName = $row["LName"];
$Username = $row["Username"];
$Company = $row["Company"];
$AddressL1 = $row["AddressL1"];
$AddressL2 = $row["AddressL2"];
$Town = $row["Town"];
$County = $row["County"];
$PostCode = $row["PostCode"];
$Password = $row["Password"];
$DFName = $row["DFName"];
$DLName = $row["DLName"];
$DAddressL1 = $row["DAddressL1"];
$DAddressL2 = $row["DAddressL2"];
$DTown = $row["DTown"];
$DCounty = $row["DCounty"];
$DPostCode = $row["DPostCode"];
}
if ($_SESSION['login_user']) {
如果他们已登录,那么它会将每个值回显到输入字段,您可以随意更改和更新它们...
而不是写出来
$login_session = stripslashes($login_session);
$login_session = mysql_real_escape_string($login_session);
$login_session = trim($login_session);
对于每个变量,最佳选择是什么?
你可以这样做:
function makeSafe($var)
{
$return = stripslashes($var);
$return = mysql_real_escape_string($return);
$return = trim($return);
return $return;
}
$login_session = makeSafe($login_session);
这将避免一个页面上有很多行代码,避免重复代码
正如我在评论中所说,尽量避免使用 mysql_* 功能并将其替换为 mysqli 或 PDO
我的网站上有一个个人资料页面,您可以在其中登录并更新您的帐户详细信息,例如:名字、姓氏、用户名、公司、地址行 1 等
见代码:
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$AccountID = $row["AccountID"];
$FName = $row["FName"];
$LName = $row["LName"];
$Username = $row["Username"];
$Company = $row["Company"];
$AddressL1 = $row["AddressL1"];
$AddressL2 = $row["AddressL2"];
$Town = $row["Town"];
$County = $row["County"];
$PostCode = $row["PostCode"];
$Password = $row["Password"];
$DFName = $row["DFName"];
$DLName = $row["DLName"];
$DAddressL1 = $row["DAddressL1"];
$DAddressL2 = $row["DAddressL2"];
$DTown = $row["DTown"];
$DCounty = $row["DCounty"];
$DPostCode = $row["DPostCode"];
}
if ($_SESSION['login_user']) {
如果他们已登录,那么它会将每个值回显到输入字段,您可以随意更改和更新它们...
而不是写出来
$login_session = stripslashes($login_session);
$login_session = mysql_real_escape_string($login_session);
$login_session = trim($login_session);
对于每个变量,最佳选择是什么?
你可以这样做:
function makeSafe($var)
{
$return = stripslashes($var);
$return = mysql_real_escape_string($return);
$return = trim($return);
return $return;
}
$login_session = makeSafe($login_session);
这将避免一个页面上有很多行代码,避免重复代码
正如我在评论中所说,尽量避免使用 mysql_* 功能并将其替换为 mysqli 或 PDO