为什么这个正则表达式在传递到php / mysql时失败了?

时间:2017-04-27 13:36:21

标签: javascript c# php regex

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(未知列&#39;&#39;&#39;字段列表&#39;)   执行时

     

INSERT INTO accounts (``) VALUES ( NULL )   

有人可以解释一下吗?我试图取消限制表达中的各种字符,但仍然没有成功。在我看来,reg表达式应该起作用,它只是leeds.ac.uk元素,它绊倒了,但我无法解决为什么?

3 个答案:

答案 0 :(得分:0)

我认为有些括号错误。缩进和美化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;
}

答案 1 :(得分:0)

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

此外,我不会在纯JavaScript中知道它,但在Jquery中你可以用以下内容替换正则表达式:

$var= something@leeds.ac.uk;

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

答案 2 :(得分:0)

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

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

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

2-完成忘记注意代码的剩余部分,专注于当前点

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

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

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