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

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.
azunoman
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
[L=phpfreaks]http://www.phpfreaks.com[/L]
WBCS
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,
%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?
azunoman
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'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",
$theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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 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
die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<label> </label>
<label> </label>
<p>&nbsp;</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input name="clubname" type="text" id="clubname" size="10">
</td>
<td><input name="addr1" type="text" id="addr1" size="10"></td>
<td>&nbsp;</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>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><label>
<input type="submit" name="Submit" value="Submit">
</label></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<p>
<label> </label>
</p>
<input type="hidden" name="MM_insert" value="form1">
</form>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>