【问题标题】:Why does this regex fail at passing onto php/mysql?为什么这个正则表达式在传递到 php/mysql 时失败?
【发布时间】:2017-04-27 13:36:20
【问题描述】:
public static bool CheckFields(AS_AccountInfo regInfo, string passwordConfirm, 
string emailConfirm, ref string errorMessage)
{

    errorMessage = "";

    // Validate the data in the fields (make sure it matches their type
    if ( !regInfo.fields.CheckMySQLFields (ref errorMessage) )
        return false;

    if (regInfo.GetFieldValue("password") == null || 
        (AS_Preferences.askUserForEmail && regInfo.GetFieldValue("email") == null))
    {
        errorMessage = "Account info not set up correctly..! Missing fields..!";
        return false;
    }

    // Password must match
    if (regInfo.GetFieldValue ("password") != passwordConfirm) {
        errorMessage = "Passwords must match..!";
        return false;
    }
    // If an email has been entered
    else if (AS_Preferences.askUserForEmail && regInfo.GetFieldValue ("email") != "") {
        // It must be valid
        if (!new Regex (@"^[\w!#$%&*+\-/=?\^_`{|}~]+(\.[\w!#$%&*+\-/=?\^_`{|}~]+)*@"
            + "@leeds.ac.uk").Match (regInfo.GetFieldValue ("email")).Success) {
            errorMessage = "Invalid email..!";
            return false;
        } 

        // And match its confirm
        if (regInfo.GetFieldValue ("email") != emailConfirm) { 
            errorMessage = "Emails must match..!";
            return false;
        } 
    }

    // All good..!
    return true;
}

 <? php
   $link = try_mysql_connect($databaseHostname, $databaseUsername, 
$databasePassword, $databaseDbName, $databasePort);

$from = $emailAccount;

$info = $_POST['newAccountInfo'];
$requireEmailActivation = strtolower($_POST['requireEmailActivation']);

$fields = explode($fieldsSeparator, $info);


$fieldNames = "";
$fieldValues = "";
$email = "";
foreach ($fields as $field) {

$words = explode($fieldNameValueSeparator, $field);

$fieldName  = $words[0];
$fieldValue = $words[1];

$fieldName = stripslashes($fieldName);
$fieldValue = stripslashes($fieldValue);
$fieldName  = mysqli_real_escape_string($link,$fieldName);
$fieldValue = mysqli_real_escape_string($link,$fieldValue);

if ($fieldName == "")
continue;

}

if ($requireEmailActivation == "true") {
    if ($fieldName == "username")
    $username = $fieldValue;
    if ($fieldName == "email"){
    $email = $fieldValue;
}

// Store it
$fieldNames  .= "`$fieldName`,";
if ($fieldValue == "")
$fieldValues .= "NULL,";
else
    $fieldValues .= "'$fieldValue',";

 }
$fieldNames = rtrim( $fieldNames, ",");
$fieldValues = rtrim( $fieldValues, ",");

$query = "INSERT INTO accounts 
(" . $fieldNames .")
VALUES ( " . $fieldValues . " )";

try_mysql_query($link, $query);
?>

结果:

MySQL 错误 1054(“字段列表”中的“未知列”) 执行时

INSERT INTO accounts (``) VALUES ( NULL )

有人能解释一下吗?我试图对表达式中的各种字符进行分隔,但仍然没有成功。在我看来,reg 表达式应该可以工作,它只是 leeds.ac.uk 元素,它被绊倒了,但我不知道为什么?

【问题讨论】:

  • 你在哪里使用/存储正则表达式?
  • NB 我知道这不能防止注射!!!
  • 然而,在哪里使用这个正则表达式作为文字字符串或函数参数并不明显。
  • 道歉我现在已经包含了更多信息@revo

标签: javascript c# php regex


【解决方案1】:

我认为有些括号放错了位置。 PHP 代码的缩进和美化部分:

foreach($fields as $field) {
    $words = explode($fieldNameValueSeparator, $field);
    $fieldName = $words[0];
    $fieldValue = $words[1];
    $fieldName = stripslashes($fieldName);
    $fieldValue = stripslashes($fieldValue);
    $fieldName = mysqli_real_escape_string($link, $fieldName);
    $fieldValue = mysqli_real_escape_string($link, $fieldValue);
    if ($fieldName == "") continue;
}

if ($requireEmailActivation == "true") {
    if ($fieldName == "username") $username = $fieldValue;
    if ($fieldName == "email") {
        $email = $fieldValue;
    }

    // Store it

    $fieldNames.= "`$fieldName`,";
    if ($fieldValue == "") $fieldValues.= "NULL,";
    else $fieldValues.= "'$fieldValue',";
}

看看

if ($fieldName == "email"){

    if ($fieldName == "") continue;
}

【讨论】:

    【解决方案2】:

    您确定是正则表达式失败而不是 PHP 吗?您是否尝试在查询之前回显变量?

    我也不知道纯 javascript,但在 Jquery 中,您可以将正则表达式替换为:

    $var= something@leeds.ac.uk;
    
    if ($var.split("@")[1]!=="leeds.ac.uk")
    {
    //alert invalid email
    }
    

    【讨论】:

    • 我现在确定是 PHP 失败了。仍然抛出 php/mysql 错误 1064。与输入字段中的数据如何无法传递到我的数据库有关
    • 尝试注释掉查询然后回显 $fieldNames 和 $fieldValues 或尝试 print_r($fieldNames) 如果它们是数组并检查它们是否包含它们应该包含的内容。
    【解决方案3】:

    为确保轻松修复您的代码,请将错误信息作为解决方案的起点。 它特定于您的:

    $query = "INSERT INTO accounts (" . $fieldNames .")
    

    1- 然后回显 $query 的值以查看其中的内容。

    2-完成后忘记注释剩余代码,专注于当前点

    3- 我在这一行中看到了错误:

    $fieldNames  .= "`$fieldName`,";
    

    通过 echo ($fieldNames) 值在此处检查 (`) 用法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多