douzhaishan5462 2014-11-18 13:55
浏览 261
已采纳

php mysqli_insert_id()错误

I am trying to get the last inserted query's id.. after some searching i found the

<?php mysqli_insert_id($getCarInfo); ?>

command, but i could not get it to work.

I am new to php and am using DreamWeaver CS6, this is the connection code that the dreamweaver created:

<?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); 
?>

i heard that there are different APIs and i seen that the connection is mysql_pconnect() and i am using mysqli_..();, i do not know how to handle those nor if that is the problem.

This is the generated error:

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.

This is line 170:

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

the data gets inserted successfully with no errors.. but i cannot get the ID back and that is very important for the registration to continue.

also, should i start a session? i need the ID to be paresed to another page so i can ask for the images to upload them and associate them to the ID.. or how is it done exactly?

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.
", mysqli_insert_id($getCarInfo)); ?>


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

1条回答 默认 最新

  • duanqian8867 2014-11-18 14:00
    关注

    Try looking at the example supplied by PHP.net:

    <?php
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s
    ", 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.
    ", $mysqli->insert_id);
    
    /* drop table */
    $mysqli->query("DROP TABLE myCity");
    
    /* close connection */
    $mysqli->close();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab