0

有人可以解释为什么这会给我以下错误吗?

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 ' )' 附近使用正确的语法

我尝试在没有 Dreamweaver 添加的所有垃圾的情况下重新创建,但我不完全了解如何提交表单并将变量传递给我的查询。Dreamweaver 的代码让我很困惑,我想不出在不破坏整个程序的情况下可以删除什么。

<?php require_once('Connections/hh.php'); ?>
<?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 data_customer (first_name, last_name) VALUES (%s, %s)",
                       GetSQLValueString($_POST['first_name'], "text"),
                       GetSQLValueString($_POST['last_name'], "text"));

  mysql_select_db($database_hh, $hh);
  $Result1 = mysql_query($insertSQL, $hh) or die(mysql_error());

$cid = mysqli_insert_id();

  $insertSQL = sprintf("INSERT INTO data_phone_number (phone_number) VALUES (%s)",
                       GetSQLValueString($_POST['phone_number'], "text"));

  mysql_select_db($database_hh, $hh);
  $Result1 = mysql_query($insertSQL, $hh) or die(mysql_error());

$pid = mysqli_insert_id();

  $insertSQL = sprintf("INSERT INTO join_customer_phone (customer_id, phone_id) VALUES ($cid, $pid)");
  mysql_select_db($database_hh, $hh);
  $Result1 = mysql_query($insertSQL, $hh) or die(mysql_error());

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
 <table align="center">
  <tr valign="baseline">
   <td nowrap="nowrap" align="right">First_name:</td>
   <td><input type="text" name="first_name" value="" size="32" /></td>
  </tr>
  <tr valign="baseline">
   <td nowrap="nowrap" align="right">Last_name:</td>
   <td><input type="text" name="last_name" value="" size="32" /></td>
  </tr>
  <tr valign="baseline">
   <td nowrap="nowrap" align="right">Phone_number:</td>
   <td><input type="text" name="phone_number" value="" size="32" /></td>
  </tr>
  <tr valign="baseline">
   <td nowrap="nowrap" align="right">&nbsp;</td>
   <td><input type="submit" value="Insert record" /></td>
  </tr>
 </table>
 <input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
</body>
</html>
4

2 回答 2

0

您正在将 mysql 函数与 mysqli 函数混合使用。坚持使用 mysqli(因为不推荐使用 mysql)。

于 2013-01-09T23:35:58.280 回答
0

你为什么不试试:

$insertSQL = "INSERT INTO data_customer (first_name, last_name) VALUES ('" . 
                       GetSQLValueString($_POST['first_name'], "text") . "', '" .
                       GetSQLValueString($_POST['last_name'], "text") .  "';";

如果它不起作用,请将您%s的 s 放在“这些”括号中。我确信您应该将值放在这些括号中。

于 2013-01-09T23:34:07.017 回答