rajeshatbuzz Posted July 11, 2011 Share Posted July 11, 2011 Hi, As per the book, i have done everything correct but still my php code is not working ..Dont knw what is the issues? Any help? AddNewServer.php Code <?php $user = "root"; $pwd = ""; $db = "perforce"; $host = "localhost"; @mysql_connect($host,$user,$pwd) or die("Connection Failed"); @mysql_select_db($db) or die ("Could not select the database!"); ?> <!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>Perforce Server List</title> </head> <body bgcolor="pink"> <script language="javascript"> function setEditMode (ServerID, ServerAlias, ServerName, ServerIp) { document.FrmServer.hidServerId.value=ServerID; document.FrmServer.txtServerAlias.value=ServerAlias; document.FrmServer.txtServerName.value=ServerName; document.FrmServer.txtServerIp.value=ServerIp; document.FrmServer.hidMode.value='U'; document.FrmServer.cmdDelete.disabled=true; } function setMode ( ) { document.FrmServer.hidServerId.value=""; document.FrmServer.txtServerAlias.value=""; document.FrmServer.txtServerName.value=""; document.FrmServer.txtServerIp.value=""; for (i=0; i<document.forms[0].elements.length;i++) { if(document.forms[0].elements[i].type == "checkbox") { document.FrmServer.elements[i].checked = false; } } document.FrmServer.hidMode.value="I"; document.FrmServer.cmdDelete.disabled=false; } function setDeltMode ( ) { document.FrmServer.hidMode.value='D'; formDeleteValues('hidSelDel'); } function formDeletevalues (hideen) { var selValues = ""; for(i=0; i<document.forms[0].elements.length; i++) { if(document.forms[0].elements[i].type=="checkbox") { if(document.forms[0].elements[i].checked ==true) { selValues = selValues + document.forms[0].elements[i].value + ","; } } } if(selValues.length < 1) { alerts ("Please choose records you wish to delete."); } else { selValues = selValues.substring(0,selValues.length-1); eval("document.forms[0]."+hidden+".value = '"+selValues+"';"); document.forms[0].submit(); } } function chkBlanks () { for(i=0;i<document.forms[0].elements.length; i++) { if(document.forms[0].elements[i].type == "text") { if(document.forms[0].elements[i].value == "") { alert("Data Left Empty"); document.FrmServer.elements[i].focus(); return false; } } } } </script> <form action="dosql.php" method="post" name="FrmServer" onsubmit="return chkBlanks();"> <input name ="hidServerId" size="2" type="hidden"> <input name ="hidMode" type="hidden" value="I"> <input name ="hidSelDel" type ="hidden"> <table align="center" bgcolor="pink" border="0" cellpading="0" cellspacing="0" name="tblOuter" width="50%" > <tr height="300" valign="top"> <td align ="center" border="1" colspan="10"> <br> <table align="center" border="1" bordercolor="skyblue" cellpadding="2" cellspacing="0" name="tblFirstChild" width="100%"> <tr bgcolor ="black"> <td align ="left" colspan="2"> <font size="2" color="white"> <b> Add New Server </b> </font> </td> </tr> <tr> <td align ="right" width="25%">Server Alias: </td> <td align="left"> <input maxlength="35" name="txtServerAlias" type="text" size="25"> </td> </tr> <tr> <td align ="right">Server Name: </td> <td align="left"> <input maxlength="255" name="txtServerName" type="text" size="42"> </td> </tr> <tr> <td align ="right">Server IP: </td> <td align="left"> <input maxlength="255" name="txtServerIp" type="text" size="42"> </td> </tr> <tr> <td colspan="2" align="right"> <input name ="cmdSubmit" type="submit" value="Save"> <input name="cmsReset" onClick="setMode();" type="button" value="Cancel"> </td> </tr> </table> <?php $query = "select * from perforce_server ORDER BY ServerName"; $resultSet=mysql_query($query) or die("Execution of the SQL query failed"); $numrows=mysql_numrows($resultSet); if($numrows) { ?> <table align="center" border="1" bordercolor="skyblue" cellpadding="0" cellspacing="0" width="100%" name="tblSecondChild> <tr bgcolor="black"> <td width="12%" align="center"> <input name="cmdDelete" onclick="setDelMode();" type="button" value="Delete"> </td> <td><font color="#FFFFFF"> <b> Server Alias </b> </font></td> <td><font color="#FFFFFF"> <b> Server Name </b> </font></td> <td><font color="#FFFFFF"> <b> Server IP </b> </font></td> </tr> <?php $i=0; while($i<$numrows) { if($i%2) $bgcolor="pink"; else $bgcolor="lightpink"; ?> <tr bgcolor="<?php echo $bgcolor;?>" onmouseover="this.bgColor='#EFEEC9';" onmouseout="this.bgcolor='<?php echo $bgcolor;?>';"> <td> <input name="chk<?php echo ($i);?>" type="checkbox" value="<?php echo mysql_result($resultSet, $i, 'ServerID');?>"> </td> <td onmousedown="setEditMode( '<?php echo mysql_result($resultSet, $i, 'ServerID');?>', '<?php echo mysql_result($resultSet, $i, 'ServerAlias');?>', '<?php echo mysql_result($resultSet, $i, 'ServerName');?>', '<?php echo mysql_result($resultSet, $i, 'ServerIp');?>');"> <?php echo mysql_result($resultSet, $i, 'ServerAlias');?> </td> <td onmousedown="setEditMode( '<?php echo mysql_result($resultSet, $i, 'ServerID');?>', '<?php echo mysql_result($resultSet, $i, 'ServerAlias');?>', '<?php echo mysql_result($resultSet, $i, 'ServerName');?>', '<?php echo mysql_result($resultSet, $i, 'ServerIp');?>');"> <?php echo mysql_result($resultSet, $i, 'ServerName');?>' </td> <td onmousedown="setEditMode( '<?php echo mysql_result($resultSet, $i, 'ServerID');?>', '<?php echo mysql_result($resultSet, $i, 'ServerAlias');?>', '<?php echo mysql_result($resultSet, $i, 'ServerName');?>', '<?php echo mysql_result($resultSet, $i, 'ServerIp');?>');"> <?php echo mysql_result($resultSet, $i, 'ServerIp');?> </td> </tr> <?php ++$i; } ?> </table> <?php } mysql_close(); ?> </td> </tr> </table> </form> </body> </html> And dosql.php <?php $author = 'Rajesh Kumar'; $user = "root"; $pwd = ""; $db = "perforce"; $host = "localhost"; $hidMode = ""; @mysql_connect($host,$user,$pwd) or die("Connection Failed"); mysql_select_db($db) or die ("Could not select the database"); if($hidMode == "") { echo "Hidmode value is=bad"; } if($hidMode == "I") { $query ="select max(ServerID)+1 AID from perforce_server"; $resultSet = mysql_query($query) or die ("Execution of the SQL Query Failed"); $getID = mysql_fetch_array($resultSet); if(!$getID[0]) { $getID[0] = 1; } $query = "insert into perforce_server (ServerID, ServerAlias, ServerName, ServerIp) values('" .$getID[0] . "','".$_post['ServerAlias'] . "','".$_post['ServerName'] . "','".$_post['ServerIp'] . "')"; $resultSet = mysql_query($query) or die ("Execution of the SQL Query Failed"); header("location:AddNewServer.php"); } if($hidMode == "U") { $query = "update perforce_server SET ServerAlias = '".$txtServerAlias."',ServerName = '".$txtServerName."',ServerIp = '".$txtServerIp."' where ServerID=".$ServerID; $resultSet = mysql_query($query) or die ("Execution of the SQL Query Failed" ); header("location:AddNewServer.php"); } if($hidMode == "D") { $query = "delete from perforce_server where ServerID in (".$hidSelDel.")"; $resultSet = mysql_query($query) or die("Execution of the SQL Query Failed"); header("location.AddNewServer.php"); } ?> Any help? Quote Link to comment https://forums.phpfreaks.com/topic/241658-issues-with-my-php-code/ Share on other sites More sharing options...
trq Posted July 11, 2011 Share Posted July 11, 2011 Your going to need to describe the problem allot better than 'not working'. Quote Link to comment https://forums.phpfreaks.com/topic/241658-issues-with-my-php-code/#findComment-1241186 Share on other sites More sharing options...
trq Posted July 11, 2011 Share Posted July 11, 2011 Actually, a quick look at your code reveals that you need to buy yourself a newer book. Your code is relying upon functionality that has been removed from php by default over 8-9 years ago. Quote Link to comment https://forums.phpfreaks.com/topic/241658-issues-with-my-php-code/#findComment-1241188 Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 $numrows=mysql_numrows($resultSet); should cause a fatal error $numrows=mysql_num_rows($resultSet); Quote Link to comment https://forums.phpfreaks.com/topic/241658-issues-with-my-php-code/#findComment-1241203 Share on other sites More sharing options...
rajeshatbuzz Posted July 11, 2011 Author Share Posted July 11, 2011 Hi, Thanks for your reply. As I am still learning from the book and copied line by line so its very difficult to tell you what exactly issues is ...but i found that hidMode value is not getting call in dosql.php file which is causing enabling to if($hidMode == "") { echo "Hidmode value is=bad"; } but not Insert / update /delete... However, I have changed to $numrows=mysql_num_rows($resultSet); but still no expected output. Quote Link to comment https://forums.phpfreaks.com/topic/241658-issues-with-my-php-code/#findComment-1241219 Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 Oh god, does your book say anything about GLOBAL VARIABLES? ACK. Throw that book away. Quote Link to comment https://forums.phpfreaks.com/topic/241658-issues-with-my-php-code/#findComment-1241229 Share on other sites More sharing options...
rajeshatbuzz Posted July 11, 2011 Author Share Posted July 11, 2011 Book says to enable register_globals = On and i did it already but same issues. would you mind solving this code issues and help me to understand failure of this script. Quote Link to comment https://forums.phpfreaks.com/topic/241658-issues-with-my-php-code/#findComment-1241234 Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 Global variables is a security vulnerability as it allows variable modification by the user. You need to disable global variables and filter get and post variables. You might do something like this: if (isset($_GET['hidMode'])) { $hidMode =$_GET['hidMode']; } Quote Link to comment https://forums.phpfreaks.com/topic/241658-issues-with-my-php-code/#findComment-1241239 Share on other sites More sharing options...
rajeshatbuzz Posted July 11, 2011 Author Share Posted July 11, 2011 Hi Buddy, Thanks for your notes. Though i have not added your code till now coz i want first of my script should be working. However, I got INSERT and EDIT operation working as it was my fault in mysql syntax however still i am struggling with DELETE operation. Could you please look into it following JavaScript Function setDelMode( ) & formDeletevalues (hideen) & dosql.php to find problem area. This is the final version of the script till now... AddNewServer.php Code <?php $user = "root"; $pwd = ""; $db = "perforce"; $host = "localhost"; @mysql_connect($host,$user,$pwd) or die("Connection Failed"); @mysql_select_db($db) or die ("Could not select the database!"); ?> <!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>Perforce Server List</title> </head> <body bgcolor="pink"> <script language="javascript"> function setEditMode (ServerID, ServerAlias, ServerName, ServerIp) { document.FrmServer.hidServerId.value=ServerID; document.FrmServer.txtServerAlias.value=ServerAlias; document.FrmServer.txtServerName.value=ServerName; document.FrmServer.txtServerIp.value=ServerIp; document.FrmServer.hidMode.value='U'; document.FrmServer.cmdDelete.disabled=true; } function setMode( ) { document.FrmServer.hidServerId.value=""; document.FrmServer.txtServerAlias.value=""; document.FrmServer.txtServerName.value=""; document.FrmServer.txtServerIp.value=""; for (i=0; i<document.forms[0].elements.length;i++) { if(document.forms[0].elements[i].type == "checkbox") { document.FrmServer.elements[i].checked = false; } } document.FrmServer.hidMode.value='I'; document.FrmServer.cmdDelete.disabled=false; } function setDelMode( ) { document.FrmServer.hidMode.value='D'; formDeleteValues('hidSelDel'); } function formDeletevalues (hideen) { var selValues = ""; for(i=0; i<document.forms[0].elements.length; i++) { if(document.forms[0].elements[i].type=="checkbox") { if(document.forms[0].elements[i].checked == true) { selValues = selValues + document.forms[0].elements[i].value + ","; } } } if(selValues.length < 1) { alert ("Please choose records you wish to delete."); } else { selValues = selValues.substring(0,selValues.length-1); eval("document.forms[0]."+hidden+".value = '"+selValues+"';"); document.forms[0].submit(); } } function chkBlanks () { for(i=0;i<document.forms[0].elements.length; i++) { if(document.forms[0].elements[i].type == "text") { if(document.forms[0].elements[i].value == "") { alert("Data Left Empty"); document.FrmServer.elements[i].focus(); return false; } } } } </script> <form action="dosql.php" method="post" name="FrmServer" onsubmit="return chkBlanks();"> <input name ="hidServerId" size="2" type="hidden"> <input name ="hidMode" type="hidden" value="I"> <input name ="hidSelDel" type ="hidden"> <table align="center" bgcolor="pink" border="0" cellpading="0" cellspacing="0" name="tblOuter" width="50%" > <tr height="300" valign="top"> <td align ="center" border="1" colspan="10"> <br> <table align="center" border="1" bordercolor="skyblue" cellpadding="2" cellspacing="0" name="tblFirstChild" width="100%"> <tr bgcolor ="black"> <td align ="left" colspan="2"> <font size="2" color="white"> <b> Add New Server </b> </font> </td> </tr> <tr> <td align ="right" width="25%">Server Alias: </td> <td align="left"> <input maxlength="35" name="txtServerAlias" type="text" size="25"> </td> </tr> <tr> <td align ="right">Server Name: </td> <td align="left"> <input maxlength="255" name="txtServerName" type="text" size="42"> </td> </tr> <tr> <td align ="right">Server IP: </td> <td align="left"> <input maxlength="255" name="txtServerIp" type="text" size="42"> </td> </tr> <tr> <td colspan="2" align="right"> <input name ="cmdSubmit" type="submit" value="Save"> <input name="cmsReset" onClick="setMode();" type="button" value="Cancel"> </td> </tr> </table> <?php $query = "select * from perforce_server ORDER BY ServerName"; $resultSet=mysql_query($query) or die("Execution of the SQL query failed"); $numrows=mysql_num_rows($resultSet); if($numrows) { ?> <table align="center" border="1" bordercolor="skyblue" cellpadding="0" cellspacing="0" width="100%" name="tblSecondChild> <tr bgcolor="black"> <td width="12%" align="center"> <input name="cmdDelete" onclick="setDelMode();" type="button" value="Delete"> </td> <td><font color="#FFFFFF"> <b> Server Alias </b> </font></td> <td><font color="#FFFFFF"> <b> Server Name </b> </font></td> <td><font color="#FFFFFF"> <b> Server IP </b> </font></td> </tr> <?php $i=0; while($i<$numrows) { if($i%2) $bgcolor="pink"; else $bgcolor="lightpink"; ?> <tr bgcolor="<?php echo $bgcolor;?>" onmouseover="this.bgColor='#EFEEC9';" onmouseout="this.bgcolor='<?php echo $bgcolor;?>';"> <td> <input name="chk<?php echo ($i);?>" type="checkbox" value="<?php echo mysql_result($resultSet, $i, 'ServerID');?>"> </td> <td onmousedown="setEditMode( '<?php echo mysql_result($resultSet, $i, 'ServerID');?>', '<?php echo mysql_result($resultSet, $i, 'ServerAlias');?>', '<?php echo mysql_result($resultSet, $i, 'ServerName');?>', '<?php echo mysql_result($resultSet, $i, 'ServerIp');?>');"> <?php echo mysql_result($resultSet, $i, 'ServerAlias');?> </td> <td onmousedown="setEditMode( '<?php echo mysql_result($resultSet, $i, 'ServerID');?>', '<?php echo mysql_result($resultSet, $i, 'ServerAlias');?>', '<?php echo mysql_result($resultSet, $i, 'ServerName');?>', '<?php echo mysql_result($resultSet, $i, 'ServerIp');?>');"> <?php echo mysql_result($resultSet, $i, 'ServerName');?> </td> <td onmousedown="setEditMode( '<?php echo mysql_result($resultSet, $i, 'ServerID');?>', '<?php echo mysql_result($resultSet, $i, 'ServerAlias');?>', '<?php echo mysql_result($resultSet, $i, 'ServerName');?>', '<?php echo mysql_result($resultSet, $i, 'ServerIp');?>');"> <?php echo mysql_result($resultSet, $i, 'ServerIp');?> </td> </tr> <?php ++$i; } ?> </table> <?php } mysql_close(); ?> </td> </tr> </table> </form> </body> </html> dosql.php Code <?php $author = 'Rajesh Kumar'; $user = "root"; $pwd = ""; $db = "perforce"; $host = "localhost"; @mysql_connect($host,$user,$pwd) or die("Connection Failed"); mysql_select_db($db) or die ("Could not select the database"); if($hidMode == "I") { echo "Inserting"; $query ="select max(ServerID)+1 AID from perforce_server"; $resultSet = mysql_query($query) or die ("Execution of the SQL Query Failed"); $getID = mysql_fetch_array($resultSet); if(!$getID[0]) { $getID[0] = 1; } $query = "INSERT INTO perforce_server(ServerID, ServerAlias, ServerName, ServerIp) VALUES('".$getID[0]. "','".$_POST['txtServerAlias']."','".$_POST['txtServerName'] ."','".$_POST['txtServerIp']."')"; $resultSet = mysql_query($query) or die("Execution of the SQL Query Failed"); header("location:AddNewServer.php"); } if($hidMode == "U") { $query = "update perforce_server SET ServerAlias = '".$txtServerAlias."',ServerName = '".$txtServerName."',ServerIp = '".$txtServerIp."' where ServerID=".$hidServerId; $resultSet = mysql_query($query) or die ("Execution of the SQL Query Failed" ); header("location:AddNewServer.php"); } if($hidMode == "D") { echo "Inserting"; $query = "DELETE FROM perforce_server WHERE ServerID IN(".$hidSelDel.")"; $resultSet = mysql_query($query) or die("Execution of the SQL Query Failed"); header("location:AddNewServer.php"); } s ?> Quote Link to comment https://forums.phpfreaks.com/topic/241658-issues-with-my-php-code/#findComment-1241288 Share on other sites More sharing options...
rajeshatbuzz Posted July 11, 2011 Author Share Posted July 11, 2011 Hi Finally, It started working... By Replacing following Code... function formDeleteValues (hidden) { var selValues = ""; for(i=0; i<document.forms[0].elements.length; i++) { if(document.forms[0].elements[i].type=="checkbox") { if(document.forms[0].elements[i].checked == true) { selValues = selValues + document.forms[0].elements[i].value + ","; } } } Thanks a lot for all FORUM member of this help. My PHP learning has just started.. Quote Link to comment https://forums.phpfreaks.com/topic/241658-issues-with-my-php-code/#findComment-1241291 Share on other sites More sharing options...
trq Posted July 12, 2011 Share Posted July 12, 2011 Thanks a lot for all FORUM member of this help. My PHP learning has just started.. That code is not PHP. Quote Link to comment https://forums.phpfreaks.com/topic/241658-issues-with-my-php-code/#findComment-1241653 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.