(too old to reply)
Help inserting a record with auto increment field
2004-02-27 17:41:15 UTC

I'm using DWMX 2004, PHP, and mySQL. I have simple table with an auto
increment ID field and two text fields. Everything works fine except when I use
the Insert Record behavior. I keep getting an error saying I'm trying to insert
a duplicate value (0) into the ID field.

Can anyone help? Thanks in advance.
2004-02-27 17:56:23 UTC
Without seeing the code I can only guess at your problem.

Do not pass the field that contains the auto increment.

A better forum for php is [L=phpbuilder]http://www.phpbuilder.com[/L] and
2004-02-27 18:28:57 UTC
Hi azunoman,

Thanks for replying. I asked the question here because I want to accomplish
this in the context of DWMX 2004. I guess I should have asked if Dreamweaver
has a way to insert an auto increment field. Here is the code:

$insertSQL = sprintf("INSERT INTO grocer_items (Name, Price) VALUES (%s,
GetSQLValueString($_POST['textfield'], "text"),
GetSQLValueString($_POST['textfield2'], "text"));

You'll notice it is not passing the field that contains the auto increment,
unless DW is adding it in somewhere else. So, specifically, does the Insert
Record behavior have a way to handle an auto increment field?
2004-02-27 21:10:10 UTC
Uhm...you only supplied some of the code. I ran a quick test...adding a club
to my clubs and it worked fine. Make sure that your autoincrement fields in
set to null and is an integer. Howerver I will say, after doing the test..how
easy it is, but when looking at the code...well that is only 1/10th the battle.
The other 9/10's is veryifying what the user input is valid data. Does not
already exist etc.

I code pure php and don't use the DW MX behaviours.

I attached the DW MX came up with: the attach files and attach code buttons
don't seem to work here.

<?php require_once('../Connections/StatsKeeper.php'); ?>
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",
$theNotDefinedValue = "")
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" :
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
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 clubs (club_name, address1, address2,
city, stateprov, country, zip) VALUES (%s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['clubname'], "text"),
GetSQLValueString($_POST['addr1'], "text"),
GetSQLValueString($_POST['addr2'], "text"),
GetSQLValueString($_POST['city'], "text"),
GetSQLValueString($_POST['state'], "text"),
GetSQLValueString($_POST['country'], "text"),
GetSQLValueString($_POST['zip'], "text"));

mysql_select_db($database_StatsKeeper, $StatsKeeper);
$Result1 = mysql_query($insertSQL, $StatsKeeper) or die(mysql_error());

mysql_select_db($database_StatsKeeper, $StatsKeeper);
$query_Recordset1 = "SELECT sport_id, club_name, date_added, address1,
address2, city, stateprov, country, zip, website FROM clubs";
$Recordset1 = mysql_query($query_Recordset1, $StatsKeeper) or
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<label> </label>
<label> </label>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<td><input name="clubname" type="text" id="clubname" size="10">
<td><input name="addr1" type="text" id="addr1" size="10"></td>
<td><input name="addr2" type="text" id="addr2" size="10"></td>
<td><input name="city" type="text" id="city" size="10"></td>
<td><input name="state" type="text" id="state" size="10"></td>
<td><input name="country" type="text" id="country" size="10"></td>
<td><input name="zip" type="text" id="zip" size="10"></td>
<input type="submit" name="Submit" value="Submit">
<label> </label>
<input type="hidden" name="MM_insert" value="form1">