Create multiple rows from a single update statement containing an array
Hi,
I have a form setup with a multiple select field, I have the filed set as an array but I believe I need to set this in a loop to insert a separate row for each instance as I am not able to query the table using the comma separated values. The code is below:
<?php require_once('../Connections/ConnDS.php'); ?>
<?php require_once("../WA_DataAssist/WA_AppBuilder_PHP.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;
}
}
// *** Implode multiple selection
if (isset($_POST["handset_range"]) && is_array($_POST["handset_range"])) {
$_POST["handset_range"] = implode(", ", $_POST["handset_range"]);
$HTTP_POST_VARS["handset_range"] = $_POST["handset_range"];
}
// FELIXONE � 2002/2004 SB by Felice Di Stefano - www.felixone.it
?>
<?php
$maxRows_WADADynListPSRange = 1000;
$pageNum_WADADynListPSRange = 0;
if (isset($_GET['pageNum_WADADynListPSRange'])) {
$pageNum_WADADynListPSRange = $_GET['pageNum_WADADynListPSRange'];
}
$startRow_WADADynListPSRange = $pageNum_WADADynListPSRange * $maxRows_WADADynListPSRange;
mysql_select_db($database_ConnDS, $ConnDS);
$query_WADADynListPSRange = "SELECT PSRange_ID, PSRange FROM PSRange";
$query_limit_WADADynListPSRange = sprintf("%s LIMIT %d, %d", $query_WADADynListPSRange, $startRow_WADADynListPSRange, $maxRows_WADADynListPSRange);
$WADADynListPSRange = mysql_query($query_limit_WADADynListPSRange, $ConnDS) or die(mysql_error());
$row_WADADynListPSRange = mysql_fetch_assoc($WADADynListPSRange);
if (isset($_GET['totalRows_WADADynListPSRange'])) {
$totalRows_WADADynListPSRange = $_GET['totalRows_WADADynListPSRange'];
} else {
$all_WADADynListPSRange = mysql_query($query_WADADynListPSRange);
$totalRows_WADADynListPSRange = mysql_num_rows($all_WADADynListPSRange);
}
$totalPages_WADADynListPSRange = ceil($totalRows_WADADynListPSRange/$maxRows_WADADynListPSRange)-1;
?>
<?php
// WA Application Builder Insert
if (isset($_POST["Insert_x"])) // Trigger
{
$WA_connection = $ConnDS;
$WA_table = "Handsets";
$WA_sessionName = "WADA_Insert_Handsets";
$WA_redirectURL = "Handsets_Detail.php";
if (function_exists("rel2abs")) $WA_redirectURL = $WA_redirectURL?rel2abs($WA_redirectURL,dirname(__FILE__)):"";
$WA_keepQueryString = true;
$WA_indexField = "handset_ID";
$WA_fieldNamesStr = "handset_name|handset_type|handset_ethernet_port|handset_bluetooth|handset_headset|handset_handsfree|handset_range";
$WA_fieldValuesStr = "".((isset($_POST["handset_name"]))?$_POST["handset_name"]:"") ."" . "|" . "".((isset($_POST["handset_type"]))?$_POST["handset_type"]:"") ."" . "|" . "".((isset($_POST["handset_ethernet_port"]))?$_POST["handset_ethernet_port"]:"") ."" . "|" . "".((isset($_POST["handset_bluetooth"]))?$_POST["handset_bluetooth"]:"") ."" . "|" . "".((isset($_POST["handset_headset"]))?$_POST["handset_headset"]:"") ."" . "|" . "".((isset($_POST["handset_handsfree"]))?$_POST["handset_handsfree"]:"") ."" . "|" . "".((isset($_POST["handset_range"]))?$_POST["handset_range"]:"") ."";
$WA_columnTypesStr = "',none,''|',none,''|',none,''|none,none,NULL|none,none,NULL|none,none,NULL|',none,''";
$WA_fieldNames = explode("|", $WA_fieldNamesStr);
$WA_fieldValues = explode("|", $WA_fieldValuesStr);
$WA_columns = explode("|", $WA_columnTypesStr);
$WA_connectionDB = $database_ConnDS;
mysql_select_db($WA_connectionDB, $WA_connection);
if (!session_id()) session_start();
$insertParamsObj = WA_AB_generateInsertParams($WA_fieldNames, $WA_columns, $WA_fieldValues, -1);
$WA_Sql = "INSERT INTO `" . $WA_table . "` (" . $insertParamsObj->WA_tableValues . ") VALUES (" . $insertParamsObj->WA_dbValues . ")";
$MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());
$_SESSION[$WA_sessionName] = mysql_insert_id();
if ($WA_redirectURL != "") {
if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
$WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
}
header("Location: ".$WA_redirectURL);
}
}
?>
<body>
<div id="OuterWrapper">
<div id="header"><img src="../images/Logo.jpg" alt="Datasharp Telecom Business Phone Systems" />
<p class="Tel">Content Management System</p>
</div><!--Header-->
<div id="InnerWrapper">
<div id="Content"><!-- InstanceBeginEditable name="Content" -->
<div class="WADAInsertContainer">
<form action="Handsets_Insert.php" method="post" name="WADAInsertForm" id="WADAInsertForm">
<div class="WADAHeader">Insert Record</div>
<table class="WADADataTable" cellpadding="0" cellspacing="0" border="0">
<tr>
<th valign="top" class="WADADataTableHeader">Name:</th>
<td class="WADADataTableCell"><input type="text" name="handset_name" id="handset_name" value="" size="32" /></td>
</tr>
<tr>
<th valign="top" class="WADADataTableHeader">Type:</th>
<td class="WADADataTableCell"><input type="text" name="handset_type" id="handset_type" value="" size="32" /></td>
</tr>
<tr>
<th valign="top" class="WADADataTableHeader">Ethernet Port:</th>
<td class="WADADataTableCell"><input type="text" name="handset_ethernet_port" id="handset_ethernet_port" value="" size="32" /></td>
</tr>
<tr>
<th valign="top" class="WADADataTableHeader">Bluetooth:</th>
<td class="WADADataTableCell"><input type="checkbox" name="handset_bluetooth" id="handset_bluetooth" value="1" /></td>
</tr>
<tr>
<th valign="top" class="WADADataTableHeader">Headset:</th>
<td class="WADADataTableCell"><input type="checkbox" name="handset_headset" id="handset_headset" value="1" /></td>
</tr>
<tr>
<th valign="top" class="WADADataTableHeader">Handsfree:</th>
<td class="WADADataTableCell"><input type="checkbox" name="handset_handsfree" id="handset_handsfree" value="1" /></td>
</tr>
<tr>
<th valign="top" class="WADADataTableHeader">Range:</th>
<td class="WADADataTableCell">
<p>
<select name="handset_range[]" size="5" multiple="multiple" id="handset_range">
<?php
do {
?>
<option value="<?php echo $row_WADADynListPSRange['PSRange']?>"><?php echo $row_WADADynListPSRange['PSRange']?></option>
<?php
} while ($row_WADADynListPSRange = mysql_fetch_assoc($WADADynListPSRange));
$rows = mysql_num_rows($WADADynListPSRange);
if($rows > 0) {
mysql_data_seek($WADADynListPSRange, 0);
$row_WADADynListPSRange = mysql_fetch_assoc($WADADynListPSRange);
}
?>
</select>
</p>
<p>Hold Ctrl to select multiple options</p></td>
</tr>
</table>
<div class="WADAButtonRow">
<table class="WADADataNavButtons" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="WADADataNavButtonCell" nowrap="nowrap"><input type="image" name="Insert" id="Insert" value="Insert" alt="Insert" src="../WA_DataAssist/images/Pacifica/Ultramodern_insert.gif" /></td>
<td class="WADADataNavButtonCell" nowrap="nowrap"><a href="Handsets_Results.php" title="Cancel"><img border="0" name="Cancel" id="Cancel" alt="Cancel" src="../WA_DataAssist/images/Pacifica/Ultramodern_cancel.gif" /></a></td>
</tr>
</table>
<input name="WADAInsertRecordID" type="hidden" id="WADAInsertRecordID" value="" />
</div>
</form>
</div>
<!-- InstanceEndEditable --></div><!--Content-->
</div><!--InnerWraper-->
<div style="clear:both"></div>
</div><!--OuterWrapper-->
</body>
<!-- InstanceEnd --></html>
<?php
mysql_free_result($WADADynListPSRange);
?>