Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/241658-issues-with-my-php-code/
Share on other sites

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.

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
?>




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.. ;)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.