PHP SQL数据插入问题

时间:2014-04-07 13:33:47

标签: php sql dreamweaver

我已经使用插入数据向导在Dreamweaver中创建了一个插入数据到mysql数据库的页面,现在当我想插入数据不能正常工作时,即使它没有进入我想要插入的页面,请帮帮我,下面是我的代码。

 <?php
 if (!function_exists("GetSQLValueString")) {
 function GetSQLValueString($theValue, $theType, $theDefinedValue = "",         $theNotDefinedValue = "") 
     {
   if (PHP_VERSION < 6) {
 $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"] == "form1")) {
$insertSQL = sprintf("INSERT INTO users (id, `First Name`, `Last Name`, `User ID`,     Password, `Secret Question`, Answer, `Date of Birth`, Gender, Profession, Phone, `Email ID`, Address, Time_Stamp, AdminLevel) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                   GetSQLValueString($_POST['id'], "int"),
                   GetSQLValueString($_POST['First_Name'], "text"),
                   GetSQLValueString($_POST['Last_Name'], "text"),
                   GetSQLValueString($_POST['User_ID'], "text"),
                   GetSQLValueString($_POST['Password'], "text"),
                   GetSQLValueString($_POST['Secret_Question'], "text"),
                   GetSQLValueString($_POST['Answer'], "text"),
                   GetSQLValueString($_POST['Date_of_Birth'], "date"),
                   GetSQLValueString($_POST['Gender'], "text"),
                   GetSQLValueString($_POST['Profession'], "text"),
                   GetSQLValueString($_POST['Phone'], "double"),
                   GetSQLValueString($_POST['Email_ID'], "text"),
                   GetSQLValueString($_POST['Address'], "text"),
                   GetSQLValueString($_POST['Time_Stamp'], "date"),
                   GetSQLValueString($_POST['AdminLevel'], "int"));

  mysql_select_db($database_user, $user);
  $Result1 = mysql_query($insertSQL, $user) or die(mysql_error());

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

 mysql_select_db($database_user, $user);
 $query_User = "SELECT * FROM users";
$User = mysql_query($query_User, $user) or die(mysql_error());
 $row_User = mysql_fetch_assoc($User);
 $totalRows_User = mysql_num_rows($User);
 ?>

1 个答案:

答案 0 :(得分:0)

我认为当你调用你的页面值时 $ _POST [&#34; MM_insert&#34;] = null或不&#34; form1&#34;

我认为你的病情很糟糕,因为如果&#34; MM_insert&#34;有价值&#34; form1&#34;永远是真的isset($ _ POST [&#34; MM_insert&#34;),因此我只会使用($ _POST [&#34; MM_insert&#34;] ==&#34; form1&#34;)< / p>

您的重定向   header(sprintf(&#34; Location:%s&#34;,$ insertGoTo));因为这个原因,如果值为if isn&t; t,则永远不会重定向到页面。

始终执行下一个选择 $ query_User =&#34; SELECT * FROM users&#34 ;; $ User = mysql_query($ query_User,$ user)或die(mysql_error()); 并且不会返回任何值,因为没有打印出带有var_dump的echo echo os。

我建议你使用var_dump($ _ POST [&#34; MM_insert&#34;])