cottonbuds2005 Posted October 11, 2009 Share Posted October 11, 2009 Hi to all PHP experts, This is my second post and the guys here have been very helpful. I'm a newbie in PHP programming and I need help with a dynamic page Im creating. Instead of updating the MySQL table, the requirement is to insert an additional line of data into the table with the search results intact. Thanks in advance to those who are going to respond. _______________________________________________________________________________________ <?php require_once('../Connections/ITInventory.php'); ?> <?php //initialize the session session_start(); // ** Logout the current user. ** $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")){ //to fully log out a visitor we need to clear the session varialbles session_unregister('MM_Username'); session_unregister('MM_UserGroup'); $logoutGoTo = "../index.php"; if ($logoutGoTo) { header("Location: $logoutGoTo"); exit; } } ?> <?php //initialize the session // ** Logout the current user. ** $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")){ //to fully log out a visitor we need to clear the session varialbles session_unregister('MM_Username'); session_unregister('MM_UserGroup'); $logoutGoTo = "../index.php"; if ($logoutGoTo) { header("Location: $logoutGoTo"); exit; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Search and Create</title> <?php error_reporting(0); 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; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "SearchandUpdate")) { $updateSQL = sprintf("UPDATE assetsdb SET EquipmentClassification=%s, SBU=%s, Username=%s, Department=%s, Division=%s, Location=%s, Floor=%s, Brand=%s, Model=%s, HostName=%s, EquipmentSpecifications=%s, AssetNo=%s, InvNo=%s, InvDate=%s, WorkOrder=%s, WorksOrderDate=%s, Supplier=%s, Cost=%s, DeliveryDate=%s, DeliveryStatus=%s, WarrantyExpiryDate=%s, WarrantyCoverage=%s, Movement=%s, NewBUAssetNo=%s, UpdatedBy=%s, UpdatedDate=%s, ReviewedBy=%s, ReviewedDate=%s, Remarks=%s WHERE SerialNo=%s", GetSQLValueString($_POST['EquipmentClassification'], "text"), GetSQLValueString($_POST['SBU'], "text"), GetSQLValueString($_POST['Username'], "text"), GetSQLValueString($_POST['Dept'], "text"), GetSQLValueString($_POST['Div'], "text"), GetSQLValueString($_POST['Location'], "text"), GetSQLValueString($_POST['Floor'], "text"), GetSQLValueString($_POST['Brand'], "text"), GetSQLValueString($_POST['Model'], "text"), GetSQLValueString($_POST['HostName'], "text"), GetSQLValueString($_POST['EquipmentSpec'], "text"), GetSQLValueString($_POST['AssetNo'], "text"), GetSQLValueString($_POST['InvNo'], "text"), GetSQLValueString($_POST['InvDate'], "text"), GetSQLValueString($_POST['WO'], "text"), GetSQLValueString($_POST['WODate'], "text"), GetSQLValueString($_POST['Supplier'], "text"), GetSQLValueString($_POST['Cost'], "text"), GetSQLValueString($_POST['DeliveryDate'], "text"), GetSQLValueString($_POST['DeliveryStatus'], "text"), GetSQLValueString($_POST['WarrantyExpiryDate'], "text"), GetSQLValueString($_POST['WarrantyCoverage'], "text"), GetSQLValueString($_POST['Mov'], "text"), GetSQLValueString($_POST['BuNum'], "text"), GetSQLValueString($_POST['UpdatedBy'], "text"), GetSQLValueString($_POST['UpdatedDate'], "text"), GetSQLValueString($_POST['ReviewedBy'], "text"), GetSQLValueString($_POST['ReviewedDate'], "text"), GetSQLValueString($_POST['Remarks'], "text"), GetSQLValueString($_POST['SerialNo'], "text")); mysql_select_db($database_ITInventory, $ITInventory); $Result1 = mysql_query($updateSQL, $ITInventory); if($Result1!=1){ echo '<script language="JavaScript" type="text/javascript">alert("'.mysql_error().'")</script>'; }else if($Result1==1){ echo '<script language="JavaScript" type="text/javascript">alert("Serial No:'.$_POST['SerialNo'].' updated.")</script>'; header('refresh: 00; url=SearchandUpdate.php'); } } $colname_SearchBySerial = "-1"; if (isset($_POST['SerialNo'])) { $colname_SearchBySerial = $_POST['SerialNo']; } mysql_select_db($database_ITInventory, $ITInventory); $query_SearchBySerial = sprintf("SELECT * FROM assetsdb WHERE SerialNo = %s", GetSQLValueString($colname_SearchBySerial, "text")); $SearchBySerial = mysql_query($query_SearchBySerial, $ITInventory) or die(mysql_error()); $row_SearchBySerial = mysql_fetch_assoc($SearchBySerial); $totalRows_SearchBySerial = mysql_num_rows($SearchBySerial); $colname_SearchByAssetNo = "-1"; if (isset($_POST['AssetNo'])) { $colname_SearchByAssetNo = $_POST['AssetNo']; } mysql_select_db($database_ITInventory, $ITInventory); $query_SearchByAssetNo = sprintf("SELECT * FROM assetsdb WHERE AssetNo = %s", GetSQLValueString($colname_SearchByAssetNo, "text")); $SearchByAssetNo = mysql_query($query_SearchByAssetNo, $ITInventory) or die(mysql_error()); $row_SearchByAssetNo = mysql_fetch_assoc($SearchByAssetNo); $totalRows_SearchByAssetNo = mysql_num_rows($SearchByAssetNo); ?> <link rel="stylesheet" type="text/css" href="stylesheet/style.css"> </head> <body> <h3>Search and Create New Record</h3> <table> <tr> <td><form action="" method="post" name="SearchSerial"> <input name="SerialNo" type="text" /><input name="SearchBySerialNo" type="submit" value="Search By Serial No" class="btn"/> </form></td> <td><form action="" method="post" name="SearchAsset"> <input name="AssetNo" type="text" /><input name="SearchByAssetNo" type="submit" value="Search By Asset No" class="btn"/> <input type="button" name="backtomain2" value="Back to main" onclick="window.location.href='../main.php'" class="btn"/> </form></td> <td><form action="<?php echo $logoutAction ?>" method="post" name="logout"><input name="logout" type="submit" value="Logout" class="mainbtn"/></form> </td> </tr> </table> <form id="SearchandUpdate" name="SearchandUpdate" method="POST" action="<?php echo $editFormAction; ?>"> <table border="1"> <tr> <th> </th> <th>Equipment Classification</th> <th>SBU</th> <th>Username</th> <th>Department</th> <th>Division</th> <th>Location</th> <th>Floor</th> <th>Brand</th> <th>Model</th> <th>Host Name</th> <th>Equipment Specifications</th> <th>Serial No</th> <th>Asset No/Inventory No</th> <th>Invoice No</th> <th>Invoice Date</th> <th>Work Order/Purchase Order</th> <th>Works Order/Purchase Date</th> <th>Supplier</th> <th>Cost (S$)</th> <th>Delivery Date</th> <th>Delivery Status</th> <th>Warranty Expiry Date</th> <th>Warranty Coverage</th> <th>IT Movement </th> <th>New BU Asset Number (for BU Transfer)</th> <th>Updated By</th> <th>Updated Date</th> <th>Reviewed By</th> <th>Reviewed Date</th> <th>Remarks</th> <th> </th> </tr> <?php if ($totalRows_SearchBySerial > 0) { // Show if recordset not empty ?> <?php do { ?> <tr> <td><input type="submit" name="Update" id="Update" value="Update" class="btn"/></td> <td><input name="EquipmentClassification" type="text" id="Equipment Classification" value="<?php echo $row_SearchBySerial['EquipmentClassification']; ?>" /></td> <td><input name="SBU" type="text" id="SBU" value="<?php echo $row_SearchBySerial['SBU']; ?>" /></td> <td><input name="Username" type="text" id="Username" value="<?php echo $row_SearchBySerial['Username']; ?>" /></td> <td><input name="Dept" type="text" id="Dept" value="<?php echo $row_SearchBySerial['Department']; ?>" /></td> <td><input name="Div" type="text" id="Div" value="<?php echo $row_SearchBySerial['Division']; ?>" /></td> <td><input name="Location" type="text" id="Location" value="<?php echo $row_SearchBySerial['Location']; ?>" /></td> <td><input name="Floor" type="text" id="Floor" value="<?php echo $row_SearchBySerial['Floor']; ?>" /></td> <td><input name="Brand" type="text" id="Brand" value="<?php echo $row_SearchBySerial['Brand']; ?>" /></td> <td><input name="Model" type="text" id="Model" value="<?php echo $row_SearchBySerial['Model']; ?>" /></td> <td><input name="HostName" type="text" id="HostName" value="<?php echo $row_SearchBySerial['HostName']; ?>" /></td> <td><textarea name="EquipmentSpec" id="EquipmentSpec" cols="45" rows="3"><?php echo $row_SearchBySerial['EquipmentSpecifications']; ?></textarea></td> <td><input name="SerialNo" type="text" id="SerialNo" value="<?php echo $row_SearchBySerial['SerialNo']; ?>" readonly="readonly" /></td> <td><input name="AssetNo" type="text" id="AssetNo" value="<?php echo $row_SearchBySerial['AssetNo']; ?>" readonly="readonly" /></td> <td><input name="InvNo" type="text" id="InvNo" value="<?php echo $row_SearchBySerial['InvNo']; ?>" /></td> <td><input name="InvDate" type="text" id="InvDate" value="<?php echo $row_SearchBySerial['InvDate']; ?>" /></td> <td><input name="WO" type="text" id="WO" value="<?php echo $row_SearchBySerial['WorkOrder']; ?>" /></td> <td><input name="WODate" type="text" id="WODate" value="<?php echo $row_SearchBySerial['WorksOrderDate']; ?>" /></td> <td><input name="Supplier" type="text" id="Supplier" value="<?php echo $row_SearchBySerial['Supplier']; ?>" /></td> <td><input name="Cost" type="text" id="Cost" value="<?php echo $row_SearchBySerial['Cost']; ?>" /></td> <td><input name="DeliveryDate" type="text" id="DeliveryDate" value="<?php echo $row_SearchBySerial['DeliveryDate']; ?>" /></td> <td><input name="DeliveryStatus" type="text" id="DeliveryStatus" value="<?php echo $row_SearchBySerial['DeliveryStatus']; ?>" /></td> <td><input name="WarrantyExpiryDate" type="text" id="WarrantyExpiryDate" value="<?php echo $row_SearchBySerial['WarrantyExpiryDate']; ?>" /></td> <td><input name="WarrantyCoverage" type="text" id="WarrantyCoverage" value="<?php echo $row_SearchBySerial['WarrantyCoverage']; ?>" /></td> <td><input name="Mov" type="text" id="Mov" value="<?php echo $row_SearchBySerial['Movement']; ?>" /></td> <td><input name="BuNum" type="text" id="BuNum" value="<?php echo $row_SearchBySerial['NewBUAssetNo']; ?>" /></td> <td><input name="UpdatedBy" type="text" id="UpdatedBy" value="<?php echo $row_SearchBySerial['UpdatedBy']; ?>" /></td> <td><input name="UpdatedDate" type="text" id="UpdatedDate" value="<?php echo $row_SearchBySerial['UpdatedDate']; ?>" /></td> <td><input name="ReviewedBy" type="text" id="ReviewedBy" value="<?php echo $row_SearchBySerial['ReviewedBy']; ?>" /></td> <td><input name="ReviewedDate" type="text" id="ReviewedDate" value="<?php echo $row_SearchBySerial['ReviewedDate']; ?>" /></td> <td><input name="Remarks" type="text" id="Remarks" value="<?php echo $row_SearchBySerial['Remarks']; ?>" /></td> <td><input type="submit" name="Update" id="Update" value="Update" class="btn"/></td> </tr> <?php } while ($row_SearchBySerial = mysql_fetch_assoc($SearchBySerial)); ?> <?php } // Show if recordset not empty ?> <?php if ($totalRows_SearchByAssetNo > 0) { // Show if recordset not empty ?> <?php do { ?> <tr> <td><input type="submit" name="Update" id="Update" value="Update" class="btn"/></td> <td><input name="EquipmentClassification" type="text" id="Equipment Classification" value="<?php echo $row_SearchByAssetNo['EquipmentClassification']; ?>" /></td> <td><input name="SBU" type="text" id="SBU" value="<?php echo $row_SearchByAssetNo['SBU']; ?>" /></td> <td><input name="Username" type="text" id="Username" value="<?php echo $row_SearchByAssetNo['Username']; ?>" /></td> <td><input name="Dept" type="text" id="Dept" value="<?php echo $row_SearchByAssetNo['Department']; ?>" /></td> <td><input name="Div" type="text" id="Div" value="<?php echo $row_SearchByAssetNo['Division']; ?>" /></td> <td><input name="Location" type="text" id="Location" value="<?php echo $row_SearchByAssetNo['Location']; ?>" /></td> <td><input name="Floor" type="text" id="Floor" value="<?php echo $row_SearchByAssetNo['Floor']; ?>" /></td> <td><input name="Brand" type="text" id="Brand" value="<?php echo $row_SearchByAssetNo['Brand']; ?>" /></td> <td><input name="Model" type="text" id="Model" value="<?php echo $row_SearchByAssetNo['Model']; ?>" /></td> <td><input name="HostName" type="text" id="HostName" value="<?php echo $row_SearchByAssetNo['HostName']; ?>" /></td> <td><textarea name="EquipmentSpec" id="EquipmentSpec" cols="45" rows="3"><?php echo $row_SearchByAssetNo['EquipmentSpecifications']; ?></textarea></td> <td><input name="SerialNo" type="text" id="SerialNo" value="<?php echo $row_SearchByAssetNo['SerialNo']; ?>" readonly="readonly"/></td> <td><input name="AssetNo" type="text" id="AssetNo" value="<?php echo $row_SearchByAssetNo['AssetNo']; ?>" readonly="readonly" /></td> <td><input name="InvNo" type="text" id="InvNo" value="<?php echo $row_SearchByAssetNo['InvNo']; ?>" /></td> <td><input name="InvDate" type="text" id="InvDate" value="<?php echo $row_SearchByAssetNo['InvDate']; ?>" /></td> <td><input name="WO" type="text" id="WO" value="<?php echo $row_SearchByAssetNo['WorkOrder']; ?>" /></td> <td><input name="WODate" type="text" id="WODate" value="<?php echo $row_SearchByAssetNo['WorksOrderDate']; ?>" /></td> <td><input name="Supplier" type="text" id="Supplier" value="<?php echo $row_SearchByAssetNo['Supplier']; ?>" /></td> <td><input name="Cost" type="text" id="Cost" value="<?php echo $row_SearchByAssetNo['Cost']; ?>" /></td> <td><input name="DeliveryDate" type="text" id="DeliveryDate" value="<?php echo $row_SearchByAssetNo['DeliveryDate']; ?>" /></td> <td><input name="DeliveryStatus" type="text" id="DeliveryStatus" value="<?php echo $row_SearchByAssetNo['DeliveryStatus']; ?>" /></td> <td><input name="WarrantyExpiryDate" type="text" id="WarrantyExpiryDate" value="<?php echo $row_SearchByAssetNo['WarrantyExpiryDate']; ?>" /></td> <td><input name="WarrantyCoverage" type="text" id="WarrantyCoverage" value="<?php echo $row_SearchByAssetNo['WarrantyCoverage']; ?>" /></td> <td><input name="Mov" type="text" id="Mov" value="<?php echo $row_SearchByAssetNo['Movement']; ?>" /></td> <td><input name="BuNum" type="text" id="BuNum" value="<?php echo $row_SearchByAssetNo['NewBUAssetNo']; ?>" /></td> <td><input name="UpdatedBy" type="text" id="UpdatedBy" value="<?php echo $row_SearchByAssetNo['UpdatedBy']; ?>" /></td> <td><input name="UpdatedDate" type="text" id="UpdatedDate" value="<?php echo $row_SearchByAssetNo['UpdatedDate']; ?>" /></td> <td><input name="ReviewedBy" type="text" id="ReviewedBy" value="<?php echo $row_SearchByAssetNo['ReviewedBy']; ?>" /></td> <td><input name="ReviewedDate" type="text" id="ReviewedDate" value="<?php echo $row_SearchByAssetNo['ReviewedDate']; ?>" /></td> <td><input name="Remarks" type="text" id="Remarks" value="<?php echo $row_SearchByAssetNo['Remarks']; ?>" /></td> <td><input type="submit" name="Update" id="Update" value="Update" class="btn"/></td> </tr> <?php } while ($row_SearchByAssetNo = mysql_fetch_assoc($SearchByAssetNo)); ?> <?php } // Show if recordset not empty ?> </table> <input type="hidden" name="MM_update" value="SearchandUpdate" /> </form> </body> </html> <?php mysql_free_result($SearchBySerial); mysql_free_result($SearchByAssetNo); ?> Quote Link to comment https://forums.phpfreaks.com/topic/177295-from-update-to-insert-need-help-with-code/ Share on other sites More sharing options...
farkewie Posted October 12, 2009 Share Posted October 12, 2009 What errors are you getting? can you post just the code that you have tried and isn't working? Quote Link to comment https://forums.phpfreaks.com/topic/177295-from-update-to-insert-need-help-with-code/#findComment-935206 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.