robby11 Posted January 29, 2008 Share Posted January 29, 2008 Hello, Im new programming with php and I was trying to convert this from asp and I am stuck I cannot figure how to get my update and delete in this table to work. Any help will be appreciated. heres my code this is mit_strat.php <?php include("drawDataGrid.class.php"); if (isset($_REQUEST['editTbl'])) { $editTbl = $_REQUEST['editTbl']; } else { $editTbl=0; } if (isset($_REQUEST['newTbl'])) { $newTbl = $_REQUEST['newTbl']; } else { $newTbl=0; } if (isset($_REQUEST['rowNo'])) { $rowNo = $_REQUEST['rowNo']; } else { $rowNo=0; } if (isset($_REQUEST['curpage'])) $curpage = $_REQUEST['curpage']; else $curpage=0; if ($curpage == 0) { $curpage = 1; } $db_conn=mysql_connect("localhost","root","Cft67ygVBhu") OR die('Could not connect:' . mysql_error()); mysql_select_db("rmt",$db_conn); $query = "select * from monitorpoints"; $result=mysql_query($query,$db_conn); $num_rows = mysql_num_rows($result); $rowLower = 0; $rowUpper = $num_rows; $colLower = 0; $fields = mysql_list_fields("rmt", "monitorpoints", $db_conn); $columns = mysql_num_fields($fields); $colUpper = $columns + 2 ; $updateImg = true; $deleteImg = false; ?> <HTML> <HEAD> <title>Mitigation Strategies</title> <link rel="stylesheet" href="stylesheet.css" type="text/css"> <script> function cancel() { document.tmpFrm.rowNo.value = 0; document.tmpFrm.editTbl.value = 0; document.tmpFrm.newTbl.value = 0; document.tmpFrm.submit(); }//end of cancel function check(i) { document.tmpFrm.rowNo.value = i; document.tmpFrm.newTbl.value = 0; document.tmpFrm.submit(); }//end of check function checkNew(i) { document.tmpFrm.rowNo.value = 0; document.tmpFrm.editTbl.value = 0; document.tmpFrm.submit(); }//end of check function deleteYesNo(i) { var ok; ok = confirm("Are you sure you want to delete this record?"); if(ok) { document.dgridFrm.del.value = i; //val.value = i; //alert(val.value); document.dgridFrm.action = "delete.php"; document.dgridFrm.submit(); }//end if }//end of deleteYesNo() function newRecord() { if(document.dgridFrm.item(1).value == "") { alert("Please enter User Name!") document.dgridFrm.item(1).focus(); }//end if else { if(document.dgridFrm.item(2).value == "") { alert("Please enter Password!") document.dgridFrm.item(2).focus(); }//end if else { document.dgridFrm.action = "save.php"; document.dgridFrm.submit(); }//end else }//end else }//end new record </script> </HEAD> <BODY> <form name="tmpFrm" method="post"> <input type="hidden" name="editTbl" value="1"> <input type="hidden" name="newTbl" value="1"> <input type="hidden" name="rowNo"> </form> <form name="dgridFrm" method="post" action="update.php"> <input type="hidden" name="del"> <table width="60%" border="0"> <? if (isset($_POST['mess'])) { ?> <tr> <td align="center"> <? echo "<font color=\"red\" face=\"verdana\">".$_POST['mess']."</font><br>"; ?> </td> </tr> <tr> <td> </td> </tr> <? } ?> <tr> <td align="center"> <? $tblWidth = 100; $tblBorder = 0; $title = array(1=>"ID",2=>"Password",3=>"Email"); $dgrid = new drawDataGrid; $dgrid->TableStart($tblWidth); $dgrid->RowStart(0); for ($i=0; $i <= mysql_num_fields($result); $i++) //Table Header { if ($i ==0) { $dgrid->mainCellStart(2); echo "<a href='JavaScript:cancel();'><img src='recycle.gif' border='0' alt='Cancel'></a>"; $dgrid->mainCellEnd(); } else { if (($i ==2)||($i ==3)) { $dgrid->mainCellStart(0); echo "<b>".ucfirst(mysql_field_name($result, $i-1))."</b>"; $dgrid->mainCellEnd(); } else { $dgrid->mainCellStart(5); echo "<b>".ucfirst(mysql_field_name($result, $i-1))."</b>"; $dgrid->mainCellEnd(); } } } $dgrid->mainCellStart(5); echo " "; $dgrid->mainCellEnd(); $dgrid->mainCellStart(5); echo " "; $dgrid->mainCellEnd(); $dgrid->RowEnd(); for ($r = $rowLower; $r < $rowUpper; $r++) { $row = mysql_fetch_array($result); $dgrid->RowStart($r); for ($c = $colLower; $c <= $colUpper; $c++) { if ($c ==$colLower) { $dgrid->mainCellStart(0); echo " "; $dgrid->mainCellEnd(); } else { if ($row[0]==$rowNo) { if ($c <= $columns) { $dgrid->CellStart(); echo "<input type='text' name='".mysql_field_name($result, $c-1)."' taborder='0' value='".trim($row[$c - 1])."'>"; $dgrid->CellEnd(); } else if ($c==$columns + 1) { $dgrid->CellStart(); echo "<a href='JavaScript:document.dgridFrm.submit();'><img src='save.gif' border='0' alt='Save'></a>"; $dgrid->CellEnd(); } else { $dgrid->CellStart(); echo " "; $dgrid->CellEnd(); } } else if ($c <= $columns) { $dgrid->CellStart(); echo trim($row[$c - 1]); $dgrid->CellEnd(); } else if ($c==$columns + 1) { $dgrid->CellStart(); echo "<a href='JavaScript:check(".$row[$c - 4].");'><img src='edit.gif' border='0' alt='Edit'></a>"; $dgrid->CellEnd(); } else { $dgrid->CellStart(); echo "<a href='JavaScript:deleteYesNo(".$row[$c - 5].");'><img src='delete.gif' border='0' alt='Delete'></a>"; $dgrid->CellEnd(); } } } $dgrid->RowEnd(); } if ($newTbl == 0) { $dgrid->RowStart($num_rows); $dgrid->mainCellStart(0); echo "<a href='JavaScript:checkNew(".($r + 1).");'><img src='add.gif' border='0' alt='New Record'></a>"; $dgrid->mainCellEnd(); // echo $colLower." ".$colUpper; for ($c = $colLower; $c < $colUpper; $c++) { $dgrid->CellStart(); if ($c==$colLower) echo " "; else echo " "; $dgrid->CellEnd(); } $dgrid->RowEnd(); } if ($newTbl == 1) { $dgrid->RowStart($num_rows + 1); $dgrid->mainCellStart(0); echo " "; $dgrid->mainCellEnd(); for ($c = $colLower; $c < $colUpper; $c++) { if ($c==0) { $dgrid->CellStart(); mysql_data_seek($result, $num_rows-1); // $row = mysql_fetch_array($result); $row = mysql_fetch_row($result); echo $row[0]+1; $dgrid->CellEnd(); } else if (($c) < $columns) { $dgrid->CellStart(); echo "<input type='text' name='".mysql_field_name($result, $c-1)."' taborder='0'>"; $dgrid->CellEnd(); } else if ($c==$columns) { $dgrid->CellStart(); echo "<a href='JavaScript:newRecord();'><img src='save.gif' border='0' alt='Save'></a>"; $dgrid->CellEnd(); } else { $dgrid->CellStart(); echo " "; $dgrid->CellEnd(); } } $dgrid->RowEnd(); } $dgrid->TableEnd(); ?> </td> </tr> </table> </form> </BODY> </HTML> this is class <?php class drawDataGrid { var $headerColor="#6495ed"; var $cellColor="#008b8b"; var $borderSize=1; function TableStart($tblWidth) { echo "<table id='grdTbl' width='".$tblWidth."%' cellspacing='0' cellpadding='2' border='".$this->borderSize."' bordercolor='#000000' class='gridtbl'>"; echo chr(13); } function TableEnd() { echo "</table>"; echo chr(13); } function RowStart($idVal) { echo "<tr id='".$idVal."'>"; echo chr(13); } function RowEnd() { echo "</tr>"; echo chr(13); } function mainCellStart($cellWidth) { echo "<td class='tdInset' bgcolor='".$this->headerColor."' width='".$cellWidth."%'>"; echo chr(13); } function mainCellEnd() { echo "</td>"; echo chr(13); } function CellStart() { echo "<td bgcolor='".$this->cellColor."' class='tdInset'>"; echo chr(13); } function CellEnd() { echo "</td>"; echo chr(13); } } ?> this is delete.php <?php $sqlStm = "Delete from monitorpoints where ID = "; $sqlStmEnd=$_POST['ID']; /*$i=0; foreach($_POST as $varName => $value) { if ($i==0) { $sqlStmEnd = $value; } $i = $i + 1; }*/ $sqlStm = $sqlStm."".$sqlStmEnd; $db_conn=mysql_connect("localhost","root","Cft67ygVBhu") OR die('Could not connect:' . mysql_error()); mysql_select_db("rmt",$db_conn); $query = $sqlStm; $result=mysql_query($query,$db_conn); ?> <HTML> <HEAD> </HEAD> <BODY onLoad="document.tempFrm.submit();"> <form name="tempFrm" method="post" action="mit_strat.php"> <input type="hidden" name="mess" value="Record Deleted Successfully!"> </form> </BODY> </HTML> this is update.php <?php $upSqlSt = "Update monitorpoints set"; $upSql=""; $upSqlMn=""; $upSqlEn=""; $i=0; foreach($_POST as $varName => $value) { if ($i==1) $upSqlEn = $upSql." Where ".$varName." = ".$value.""; If ($i > 1) $upSqlMn = $upSqlMn." ".$varName." = '".$value."',"; $i++; } $upSql = $upSqlSt.substr($upSqlMn,0,strlen($upSqlMn)-1).$upSqlEn; $db_conn=mysql_connect("localhost","root","Cft67ygVBhu") OR die('Could not connect:' . mysql_error()); mysql_select_db("rmt",$db_conn); $query = $upSql; $result=mysql_query($query,$db_conn); ?> <HTML> <HEAD> </HEAD> <BODY> <form name="tempFrm" method="post" action="mit_strat.php"> <input type="hidden" name="mess" value="Record Updated Successfully!"> </form> <? echo "<script>document.tempFrm.submit();</script>"; ?> </BODY> </HTML> heres the the dbase im using CREATE TABLE `monitorpoints` ( `ID` int(11) NOT NULL auto_increment, `RiskID` int(11) NOT NULL default '0', `MonitorDate` datetime NOT NULL default '0000-00-00 00:00:00', `ReviewDate` datetime default NULL, `Pe` float(3,2) unsigned NOT NULL default '0.00', /* RMTv3.0: *** Deprecated */ `Pi` float(3,2) unsigned NOT NULL default '0.00', /* RMTv3.0: *** Deprecated */ `Lt` float(15,2) NOT NULL default '0.00', `Le` float(15,2) NOT NULL default '0.00', /* RMTv3.0: *** Deprecated */ `Actions` varchar(255) default NULL, `MRallocation` float(15,2) NOT NULL default '0.00', `MRcost` float(15,2) NOT NULL default '0.00', `AllocatedResources` varchar(255) NOT NULL default '', `Description` varchar(255) NOT NULL default '', `ScedStatusID` int(11) NOT NULL default '0', `MitigationPlanQAID` int(11) NOT NULL default '0', `CorrectiveActionPlan` text, `MPdesignator` varchar(5) NOT NULL default 'A', /* RMTv3.0 */ `Pe_effect` float(3,2) unsigned NOT NULL default '0.00', `Pi_effect` float(3,2) unsigned NOT NULL default '0.00', `Le_effect` float(15,2) NOT NULL default '0.00', `WBSnum` varchar(25) NOT NULL default '0', `Owner` varchar(25) NOT NULL default '', /* Mitigation Strategy Owner */ `MPname` varchar(25) NOT NULL default '', /* Mitigation Strategy Name */ `StartDate` datetime NOT NULL default '0000-00-00 00:00:00', `EndDate` datetime NOT NULL default '0000-00-00 00:00:00', `DriverNum` int(11) NOT NULL default '0', /* The unique driver ID associated with this monitor point (mitigation strategy) */ PRIMARY KEY (`ID`) ) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=latin1; -- -- Dumping data for table `monitorpoints` -- LOCK TABLES `monitorpoints` WRITE; /*!40000 ALTER TABLE `monitorpoints` DISABLE KEYS */; INSERT INTO `monitorpoints` (ID, RiskID, MonitorDate, ReviewDate, Pe, Pi, Lt, Le, Actions, MRallocation, MRcost, AllocatedResources, Description, ScedStatusID, MitigationPlanQAID, CorrectiveActionPlan, MPdesignator, Pe_effect, Pi_effect, Le_effect, WBSnum, Owner, MPname, StartDate, EndDate, DriverNum) VALUES (6,7,'2007-06-03 01:00:00',NULL,0.50,0.70,365.00,127.75,'Key person does nothing - just accepts the odds', 0.00,0.00,'','',0,0, NULL,'A', 0.00,0.00,0.00,'0.1','','','2007-06-03 01:00:00','2007-06-03 01:00:00',0), (8,8,'2007-11-10 00:00:00',NULL,0.90,0.90,30.00,24.30,'', 15.00,0.00,'','',1,0, NULL,'A', 0.00,0.00,0.00,'0.1','','','2007-11-10 00:00:00','2007-11-10 00:00:00',0), (9,8,'2007-12-07 00:00:00',NULL,0.70,0.90,30.00,18.90,NULL, 25.00,0.00,'','',2,4, NULL,'B', 0.00,0.00,0.00,'0.1','','','2007-12-07 00:00:00','2007-12-07 00:00:00',0), (10,8,'2007-12-14 00:00:00',NULL,0.10,0.90,30.00,2.70,'3/28/07 on track', 25.00,0.00,'','',3,2, NULL,'C', 0.00,0.00,0.00,'0.1','','','2007-12-14 00:00:00','2007-12-14 00:00:00',0), (11,8,'2007-12-23 00:00:00',NULL,0.10,0.50,30.00,1.50,'', 25.00,0.00,'','',1,1, NULL,'D', 0.00,0.00,0.00,'0.1','','','2007-12-23 00:00:00','2007-12-23 00:00:00',0); Im just tring to display add, updat and del records is all. Thank you Quote Link to comment 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.