Update and Insert into MySQL database
I have an "Add note page" that is supposed to be able to add a note for a customer and update any relevant information, so the user will type a note in and use drop downs to make any changes, and click save and have the database update 2 different tables, but its not working too well. FYI the notes are in a one to many relationship with the customers information table. I am using the dreamweaver wizards for the php code, and javascript to adjust the customer information table while the user is typing the note. I also use javascript to submit both forms at the same time, which should then reload that persons main page... What am I doing wrong here?
I'm including an abbreveated version of the code, i am using dreamweaver php update and insert wizards...Here is the relevant code:
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "AddNoteForm")) {
$insertSQL = sprintf("INSERT INTO custnotes (NoteField1, NoteField2, NoteField3, NoteField4) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['NoteField1'], "int"),
GetSQLValueString($_POST['NoteField2'], "text"),
GetSQLValueString($_POST['NoteField3'], "date"),
GetSQLValueString($_POST['NoteField4'], "text"));
mysql_select_db($database_CustInfo_mySQL, $CustInfo_mySQL);
$Result1 = mysql_query($insertSQL, $CustInfo_mySQL) or die(mysql_error());
$insertGoTo = "CustHome.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "UpdateCustInfo")) {
$updateSQL = sprintf("UPDATE custinfo SET LastName=%s, FirstName=%s, Address=%s, Address2=%s, City=%s, WHERE EnrollNumber=%s",
GetSQLValueString($_POST['LastName'], "text"),
GetSQLValueString($_POST['FirstName'], "text"),
GetSQLValueString($_POST['Address'], "text"),
GetSQLValueString($_POST['Address2'], "text"),
GetSQLValueString($_POST['City'], "text"),
GetSQLValueString($_POST['EnrollNumber'], "text"));
mysql_select_db($database_CustInfo_mySQL, $CustInfo_mySQL);
$Result1 = mysql_query($updateSQL, $CustInfo_mySQL) or die(mysql_error());
$updateGoTo = "CustHome.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
$colname_CustInfo = "-1";
if (isset($_GET['EnrollNumber'])) {
$colname_CustInfo = $_GET['EnrollNumber'];
}
mysql_select_db($database_CustInfo_mySQL, $CustInfo_mySQL);
$query_CustInfo = sprintf("SELECT * FROM custinfo WHERE EnrollNumber = %s", GetSQLValueString($colname_CustInfo, "text"));
$CustInfo = mysql_query($query_CustInfo, $CustInfo_mySQL) or die(mysql_error());
$row_CustInfo = mysql_fetch_assoc($CustInfo);
$totalRows_CustInfo = mysql_num_rows($CustInfo);
$colname_CustNotes = "-1";
if (isset($_GET['EnrollNumber'])) {
$colname_CustNotes = $_GET['EnrollNumber'];
}
mysql_select_db($database_CustInfo_mySQL, $CustInfo_mySQL);
$query_CustNotes = sprintf("SELECT * FROM custnotes WHERE FKEnrollNumber = %s", GetSQLValueString($colname_CustNotes, "text"));
$CustNotes = mysql_query($query_CustNotes, $CustInfo_mySQL) or die(mysql_error());
$row_CustNotes = mysql_fetch_assoc($CustNotes);
$totalRows_CustNotes = mysql_num_rows($CustNotes);
?>
<html>
<head>
<title>Add Note</title>
<style>
.CustInfo{
display: none;
}
</style>
<script type="text/javascript">
function SaveAndClose();{
document.UpdateCustInfo.submit();
document.AddNoteForm.submit();
}
</script>
</head>
<body><!--Continue showing relevant information-->
<div class="CustInfo">
<form action="<?php echo $editFormAction; ?>" method="post" name="UpdateCustInfo" id="UpdateCustInfo">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">EnrollNumber:</td>
<td> </td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">LastName:</td>
<td><input type="text" name="LastName" id="LastName" value="<?php echo htmlentities($row_CustInfo['LastName'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">FirstName:</td>
<td><input type="text" name="FirstName" id="FirstName" value="<?php echo htmlentities($row_CustInfo['FirstName'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Address:</td>
<td><input type="text" name="Address" id="Address" value="<?php echo htmlentities($row_CustInfo['Address'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Address2:</td>
<td><input type="text" name="Address2" id="Address2" value="<?php echo htmlentities($row_CustInfo['Address2'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">City:</td>
<td><input type="text" name="City" id="City" value="<?php echo htmlentities($row_CustInfo['City'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
</tr>
<input type="hidden" name="MM_update" value="UpdateCustInfo" />
<input type="hidden" name="EnrollNumber" value="<?php echo $row_CustInfo['EnrollNumber']; ?>" />
</form>
</div>
<form action="<?php echo $editFormAction; ?>" method="post" name="AddNoteForm" id="AddNoteForm">
<table>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Field1:</td>
<td><input type="text" name="Field1" id="Field1" value="<?php echo $row_CustInfo['Field1']; ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Field2:</td>
<td><input type="text" name="Field2" id="Field2" value="<?php echo $row_CustInfo['Field2']; ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Field3:</td>
<td><input type="text" name="Field3" id="Field3" value="<?php echo $row_CustInfo['Field3']; ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Field4:</td>
<td><input type="text" name="Field4" id="Field4" value="<?php echo $row_CustInfo['Field4']; ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Field5:</td>
<td><input type="text" name="Field5" id="Field5" value="<?php echo $row_CustInfo['Field5']; ?>" size="32" /></td>
</tr>
<tr><td><input type="button" name="ValidateNote" value="Add Note" onclick="SaveAndClose();" /></td></tr>
</table>
<input type="hidden" name="MM_insert" value="AddNoteForm" />
</form>
</body>
</html>
<?php
mysql_free_result($CustInfo);
mysql_free_result($CustNotes);
?>
Any help would be appreciated, ive been working on this for 4 days now...
