Forums

PHP

This topic is locked

PHP MySql conditional insert (if field empty)

Posted 30 Aug 2007 21:13:46
1
has voted
30 Aug 2007 21:13:46 tech mama posted:
Hi all, PHP rookie here!

I have a form that inserts data into two tables (patients and tags).
The data that goes into tags is an array where I have two fields per row -- one field is inputed from the form, the other is a variable derived which always stays the same.

My problem is that, while I have 10 rows, not all of them will be used every time (actually, most of the times it will be only 2 or 3, but I need to have all 10 of them.)
but they all get inserted into the database -- the records for the ones with values have the correct value in the "tag_tag" field, and the records for the ones that are left empty have an empty "tag_tag" field.

I understand that the problem lies in the declaration of the "ptID_tag" field, because it always gets the variable $nextid regardless of the state of "tag_tag" and that I need to insert a condition, but I am stumped. I think it has more to do with the fact that I'm very new at PHP than that I've looked at this code too much.

The relevant code is below. Can anybody point me in the right direction?
Thank you!





<?php require_once('Connections/mv.php'); ?><?php
if (!isset($_SESSION)) {
session_start();
}
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != ""){
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true"){
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);

$logoutGoTo = "logout.php";
if ($logoutGoTo) {
header("Location: $logoutGoTo";
exit;
}
}
?>
<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "admin";
$MM_donotCheckaccess = "false";

function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
$isValid = False;

if (!empty($UserName)) {
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "" && false) {
$isValid = true;
}
}
return $isValid;
}

$MM_restrictGoTo = "login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?") $MM_qsChar = "&";
if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
$MM_referrer .= "?" . $QUERY_STRING;
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
<?php
$sql = "SHOW TABLE STATUS LIKE '$mem_patients'" ;
$result = mysql_query( $sql ) ;
$nextid = mysql_result( $result, 0, 'Auto_increment' ) ;

if (!function_exists("GetSQLValueString") {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = ""
{
$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;
}
}

$colname_tag = "-1";
if (isset($_SESSION['$nextID'])) {
$colname_tag = $_SESSION['$nextID'];
}
mysql_select_db($database_mv, $mv);
$query_tag = sprintf("SELECT ptID_tag, tag_tag FROM mem_tags, mem_patients WHERE ptID_tag = %s", GetSQLValueString($colname_tag, "int");
$tag = mysql_query($query_tag, $mv) or die(mysql_error());
$row_tag = mysql_fetch_assoc($tag);
$totalRows_tag = mysql_num_rows($tag);

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "addme") {
for($i=0; $i < count($_POST["tag_tag"]); $i++) {
$insertSQL = sprintf("INSERT INTO mem_tags (tag_tag, ptID_tag) VALUES ('%s', '$nextid')",
$_POST['tag_tag'][$i],
$_POST['$nextid'][$i]);


mysql_select_db($database_mv, $mv);
$Result1 = mysql_query($insertSQL, $mv) or die(mysql_error());

}
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "addme") {
$insertSQL = sprintf("INSERT INTO mem_patients (FirstName_pt, LastName_pt, FolderName_pt, Medicalrecord_pt) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['FirstName_pt'], "text",
GetSQLValueString($_POST['LastName_pt'], "text",
GetSQLValueString($_POST['FolderName_pt'], "text",
GetSQLValueString($_POST['Medicalrecord_pt'], "text");

mysql_select_db($database_mv, $mv);
$Result1 = mysql_query($insertSQL, $mv) or die(mysql_error());


$insertGoTo = "confirmation.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3.org/TR/html4/loose.dtd">
<html><!-- InstanceBegin template="/Templates/template.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- InstanceBeginEditable name="doctitle" -->
<title>m e m o r y v i s i o n | n e w p a t i e n t</title>
<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
<link href="css/style.css" rel="stylesheet" type="text/css">
<link href="css/formstyle.css" rel="stylesheet" type="text/css">
<link href="css/ie.css" rel="stylesheet" type="text/css">
</head>

<body>
<table width="100%" border="0">
<tr>
<td><form action="found.php" method="get" name="search" id="search" class="form">
<input name="SearchString" type="text" id="SearchString" size="15" class="searchfield">
<br />
<input type="submit" name="button" id="button" value="GO" class="buttonsm">
</form>
</td>
</tr>
</table>

</td>
<td width="84%" class="main_text"><!-- InstanceBeginEditable name="main" -->
<script language="JavaScript" type="text/JavaScript">
function setFoldername() {
var fname = document.addme.FirstName_pt.value;
var lname = document.addme.LastName_pt.value;
var nonn =('<?php echo $nextid ?>');
var xname = lname + fname.charAt(0) + nonn;
document.addme.FolderName_pt.value = xname.toLowerCase();
}
</script>
<h1>New Patient</h1>
<form action="<?php echo $editFormAction; ?>" method="POST" name="addme" id="addme" class="form">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><strong>Patient ID <?php echo $nextid; ?></strong></td>
<td> </td>
</tr>
<tr>
<td><strong>First Name</strong><br>
<input type="text" name="FirstName_pt" value="" size="32" class="textfield" tabindex="1"></td>
<td><strong>Last Name</strong><br>
<input type="text" name="LastName_pt" value="" size="32" onBlur="setFoldername();" class="textfield" tabindex="2"></td>
</tr>
<tr>
<td><strong>Medical Record</strong><br>
<input type="text" name="Medicalrecord_pt" value="" size="32" class="textfield" tabindex="3"></td>
<td><strong>Folder Name *</strong><br>
<input type="text" name="FolderName_pt" value="" size="32" class="textfield" tabindex="99"> </td>
</tr>
<tr>
<td></td>
<td>*This value is set automatically. Please do not modify. </td>
</tr>
</table>
<p> </p>

<table>
<tr>
<td colspan="2"></td>
<td colspan="2"></td>
</tr>
<tr>
<td width="9%"><strong>Tag 1</strong></td>
<td width="29%"><input name="tag_tag[]" type="text" id="tag_tag1" value="" size="10" class="textfield" tabindex="4"></td>
<td width="11%"><strong>Tag 2</strong></td>
<td width="51%"><input name="tag_tag[]" type="text" id="tag_tag2" value="" size="10" class="textfield" tabindex="5"></td>
</tr>
<tr>
<td><strong>Tag 3 </strong></td>
<td><input name="tag_tag[]" type="text" id="tag_tag3" value="" size="10" class="textfield" tabindex="6"></td>
<td><strong>Tag 4 </strong></td>
<td><input name="tag_tag[]" type="text" id="tag_tag4" value="" size="10" class="textfield" tabindex="7"></td>
</tr>
<tr>
<td><strong>Tag 5 </strong></td>
<td><input name="tag_tag[]" type="text" id="tag_tag5" value="" size="10" class="textfield" tabindex="8"></td>
<td><strong>Tag 6 </strong></td>
<td><input name="tag_tag[]" type="text" id="tag_tag6" value="" size="10" class="textfield" tabindex="9"></td>
</tr>
<tr>
<td><strong>Tag 7 </strong></td>
<td><input name="tag_tag[]" type="text" id="tag_tag7" value="" size="10" class="textfield" tabindex="10"></td>
<td><strong>Tag 8</strong></td>
<td><input name="tag_tag[]" type="text" id="tag_tag8" value="" size="10" class="textfield" tabindex="11"></td>
</tr>
<tr>
<td><strong>Tag 9 </strong></td>
<td><input name="tag_tag[]" type="text" id="tag_tag9" value="" size="10" class="textfield" tabindex="12"></td>
<td><strong>Tag 10 </strong></td>
<td><input name="tag_tag[]" type="text" id="tag_tag10" value="" size="10" class="textfield" tabindex="13"></td>
</tr>
<tr>
<td colspan="2"><div align="right"></div></td>
<td colspan="2"><input type="submit" name="Submit" id="Submit" value="Add patient" class="button"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="addme">
</form>
<p> </p><!-- InstanceEndEditable --></td>
</tr>
</table>
</body>
<!-- InstanceEnd --></html>
<?php
mysql_free_result($tag);
?&gt;<font face='Verdana'></font id='Verdana'>

---
So much to learn, so little time.

Reply to this topic