php mysqli_insert_id()错误

时间:2014-11-18 13:55:53

标签: php mysql mysqli

我正在尝试获取最后插入的查询的ID ..经过一些搜索,我找到了

<?php mysqli_insert_id($getCarInfo); ?>

命令,但我无法让它工作。

我是php新手,我正在使用DreamWeaver CS6,这是Dreamweaver创建的连接代码:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_getCarInfo = "localhost";
$database_getCarInfo = "cars";
$username_getCarInfo = "root";
$password_getCarInfo = "*******";
$getCarInfo = mysql_pconnect($hostname_getCarInfo, $username_getCarInfo, $password_getCarInfo) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

我听说有不同的API,我看到连接是mysql_pconnect(),我正在使用mysqli _ ..();,我不知道如何处理这些,如果这是问题。

这是生成的错误:

Warning: mysqli_insert_id() expects parameter 1 to be mysqli, resource given in H:\AppServ\www\cars\insertData.php on line 170
New Record has id 0.

这是第170行:

<?php printf ("New Record has id %d.\n", mysqli_insert_id($getCarInfo)); ?>

数据成功插入且没有错误..但我无法获取ID,这对于注册继续非常重要。

另外,我应该开始一个会话吗?我需要将ID写入另一个页面,这样我就可以要求将图像上传并将它们与ID相关联。或者它是如何完成的?

EDIT ---

Full code:

getCarInfo.php:

    <?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_getCarInfo = "localhost";
$database_getCarInfo = "cars";
$username_getCarInfo = "root";
$password_getCarInfo = "****";
$getCarInfo = mysql_pconnect($hostname_getCarInfo, $username_getCarInfo, $password_getCarInfo) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

insertData.php:

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

  mysql_select_db($database_getCarInfo, $getCarInfo);
  $Result1 = mysql_query($insertSQL, $getCarInfo) or die(mysql_error());
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
  $insertSQL = sprintf("INSERT INTO car_specifications (id, manafacturer, model, `year`, type, cylinder, cylinder_cap, trans_count, trans_type, drive_type, fuel, hpower, valves, int_color, ext_color, img1, img2, img3, img4, img5, img6, img7, img8, img9) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($_POST['manafacturer'], "text"),
                       GetSQLValueString($_POST['model'], "text"),
                       GetSQLValueString($_POST['year'], "int"),
                       GetSQLValueString($_POST['type'], "text"),
                       GetSQLValueString($_POST['cylinder'], "int"),
                       GetSQLValueString($_POST['cylinder_cap'], "double"),
                       GetSQLValueString($_POST['trans_count'], "int"),
                       GetSQLValueString($_POST['trans_type'], "text"),
                       GetSQLValueString($_POST['drive_type'], "text"),
                       GetSQLValueString($_POST['fuel'], "text"),
                       GetSQLValueString($_POST['hpower'], "int"),
                       GetSQLValueString($_POST['valves'], "text"),
                       GetSQLValueString($_POST['int_color'], "text"),
                       GetSQLValueString($_POST['ext_color'], "text"),

                       GetSQLValueString($_POST['image1'], "text"),
                       GetSQLValueString($_POST['image2'], "text"),
                       GetSQLValueString($_POST['image3'], "text"),
                       GetSQLValueString($_POST['image4'], "text"),
                       GetSQLValueString($_POST['image5'], "text"),
                       GetSQLValueString($_POST['image6'], "text"),
                       GetSQLValueString($_POST['image7'], "text"),
                       GetSQLValueString($_POST['image8'], "text"),
                       GetSQLValueString($_POST['image9'], "text"),
                       GetSQLValueString($_POST['image10'], "text"));




  mysql_select_db($database_getCarInfo, $getCarInfo);
  $Result1 = mysql_query($insertSQL, $getCarInfo) 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 id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
  <input name="Manufacturer" type="text" size="30" maxlength="30" />
  <input name="sbmit" type="submit" />
  <input type="hidden" name="MM_insert" value="form1" />

</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
  <table align="center">
    <tr valign="baseline">
      <td align="left" nowrap="nowrap">*Manafacturer:</td>
      <td><input type="text" name="manafacturer" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">*Model:</td>
      <td><input type="text" name="model" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">*Year:</td>
      <td><input type="text" name="year" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">*Type:</td>
      <td><select name="type">
        <option value="Sports" <?php if (!(strcmp("Sports", ""))) {echo "SELECTED";} ?>>Sports</option>
        <option value="Saloon" <?php if (!(strcmp("Saloon", ""))) {echo "SELECTED";} ?>>Saloon</option>
        <option value="convertable" <?php if (!(strcmp("convertable", ""))) {echo "SELECTED";} ?>>convertable</option>
        <option value="truck" <?php if (!(strcmp("truck", ""))) {echo "SELECTED";} ?>>truck</option>
        <option value="sedan" <?php if (!(strcmp("sedan", ""))) {echo "SELECTED";} ?>>sedan</option>
      </select></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">*Cylinder:</td>
      <td><input type="text" name="cylinder" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">Cylinder cap:</td>
      <td><input type="text" name="cylinder_cap" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">*Transmissions:</td>
      <td><input type="text" name="trans_count" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">*Trans. type:</td>
      <td><input type="text" name="trans_type" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">*Drive_type:</td>
      <td><input type="text" name="drive_type" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">*Fuel:</td>
      <td><input type="text" name="fuel" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">Horse Power:</td>
      <td><input type="text" name="hpower" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">*Valves:</td>
      <td><input type="text" name="valves" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">*Interior color:</td>
      <td><input type="text" name="int_color" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">*Exterior color:</td>
      <td><input type="text" name="ext_color" 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>

<?php printf ("New Record has id %d.\n", mysqli_insert_id($getCarInfo)); ?>


  <input type="hidden" name="MM_insert" value="form2" />
</form>
<p>&nbsp;</p>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试查看PHP.net提供的示例:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$mysqli->query("CREATE TABLE myCity LIKE City");

$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);

printf ("New Record has id %d.\n", $mysqli->insert_id);

/* drop table */
$mysqli->query("DROP TABLE myCity");

/* close connection */
$mysqli->close();
?>