解释

2012-06-13 35 views
-1

嗨你们可以帮我打破这个Dreamweaver中插入代码向导特别的代码if (!function_exists("GetSQLValueString")) { ....}部分:解释

<?php 
if (!function_exists("GetSQLValueString")) { 
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",$theNotDefinedValue = "") 
{ 


$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); 

    switch ($theType) { 
    case "text": 
     $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
     break;  
    case "long": 
case "int": 
    $theValue = ($theValue != "") ? intval($theValue) : "NULL"; 
    break; 
case "double": 
    $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; 
    break; 
case "date": 
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
    break; 
case "defined": 
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; 
    break; 


} 
    return $theValue; 
} 
} 

$editFormAction = $_SERVER['PHP_SELF']; 
if (isset($_SERVER['QUERY_STRING'])) { 
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); 
} 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) { 
    $insertSQL = sprintf("INSERT INTO feedback (name, email, phone, service, message) VALUES (%s, %s, %s, %s, %s)", 
         GetSQLValueString($_POST['name'], "text"), 
         GetSQLValueString($_POST['email'], "text"), 
         GetSQLValueString($_POST['phone'], "text"), 
         GetSQLValueString($_POST['service'], "text"), 
         GetSQLValueString($_POST['message'], "text")); 

    mysql_select_db($database_kojexconsult, $kojexconsult); 
    $Result1 = mysql_query($insertSQL, $kojexconsult) or die(mysql_error()); 

    $insertGoTo = "success.php"; 
    if (isset($_SERVER['QUERY_STRING'])) { 
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; 
    $insertGoTo .= $_SERVER['QUERY_STRING']; 
    } 
    header(sprintf("Location: %s", $insertGoTo)); 
} 
?> 
+1

没有比[文档](http://php.net/)更好的地方开始了,特别是['function_exists'](http://us3.php。net/function_exists) –

+0

tereško,我知道你的评论笼罩在讽刺中,但我仍然不知道你为什么决定和我一起去南方。同样,谢谢 –

回答

1

我会看看我能不能打破它给你的。

初始if检查函数是否未定义(http://php.net/manual/en/function.function-exists.php)。然后它通过并定义函数GetSQLValueString。

功能GetSQLValueString执行以下操作:

  • 它执行的三元如果检查与get_magic_quotes_gpc(获取magic_quotes_gpc的当前的配置设置)。如果它通过,它会使$ theValue条纹,否则它只返回$ theValue。
  • 如果在执行mysql_real_escape_string或mysql_escape_string之前检查是否存在“mysql_real_escape_string”,则另一个三元组。
  • 它选择匹配$ theType变量的情况并将$ theValue设置为适当的值。
  • 最后,它返回值,以便它可以用于代码中的任何目的。

其余的代码是非常简单的db插入片段。

$ editFormAction是从php运行的任何脚本(如insert.php)设置的。如果检查以确认是否有查询字符串。如果有查询字符串,它会追加到$ editForm Action的末尾。使用上面的PHP_SELF,这可能是insert.php?name = Tim & email = [email protected] & phone = 8675309。

接下来的部分是将信息插入数据库的魔力所在。

如果在继续数据库插入之前检查是否设置了MM_insert并且其值为表单。这本质上是一个健全的检查。在那之后,它使用从表单传递的信息为变量$ insertSQL提供来自格式化字符串的值。你会看到它使用前面的函数GetSQLValueString来确保数据准备进入数据库。一旦$ insertSQL具有所有的值,它就选择数据库然后执行查询。

之后,success.php被设置为$ insertGoTo。 if检查是否将QUERY_STRING传递给此页面。如果有QUERY_STRING,它会将其追加到success.php(success.php?name = Tim & email = [email protected] & phone = 8675309)。在if检查之后,它通过调用标题来重定向页面。

我希望这些信息能帮助你理解脚本的细节。

-----个人注意这里-----

Dreamweaver是不是用于开发PHP代码的一个很大的应用。如果你想要一个优秀的IDE用于PHP,我推荐PHPStorm(http://www.jetbrains.com/phpstorm/)。我已经使用了两年多了,并且很喜欢它。我将它与一个出色的文本编辑器SublimeText2(http://www.sublimetext.com/2)结合在一起。

+0

Tim Aldridge,非常感谢。我真的很感谢 –

+0

不客气! – Telshin