Hi!
I am a total newbie who has just developed a db for storing infos about some machines.I have coded a program to display the details of a particular machine and update the details only if the submit button is pressed.I wonder why it is not getting updated,instead if i close without pressing the submit button;the record gets corrupted.I have been stuck with this problem for nearly 2 wks.Can anyone help me plsssssss?
Here's my code:

<html>
<head><TITLE>m2.php</TITLE>

<SCRIPT LANGUAGE="JavaScript">
redirTime = "5000";
redirURL = "thankyou.html";
function redirTimer() { self.setTimeout("self.location.href = redirURL;",redirTime); }
</script>

</head>
<body >
<form name="m2.php" method="GET">
<?
$reference=$_GET['ref'];

$db="mydb";
$link=mysql_connect("localhost","username","password") or die("couldn't connect to Mysql");
mysql_select_db($db) or die("Select Error:".mysql_error());
$query="SELECT * FROM machines where ref='$reference'";
$rt=mysql_query($query);
$nt=mysql_fetch_array($rt);
$id = $nt['id'];
print "<table class=internal>";

print "<tr><td class=internal><b class=intro3>Ref:</b></td>
<td><input type=text name=ref value=$nt[ref] size=10></td></tr>";

print "<tr><td class=internal><b class=intro3>IP:</b></td>
<td><input type=text name=IP value=$nt[IP] size=20></td></tr>";

print "<tr><td class=internal><b class=intro3>MAC:</b></td>
<td><input type=text name=MAC value=$nt[MAC] size=20></td></tr>";

print "<tr><td class=internal><b class=intro3>Name:</b></td>
<td><input type=text name=Name value=$nt[Name] size=20></td></tr>";

print "<tr><td class=internal><b class=intro3>Location:</b></td>
<td><input type=text name=Location value=$nt[Location] size=20></td></tr>";

print "<tr><td class=internal><b class=intro3>Authority:</b></td>
<td><input type=text name=Authority value=$nt[Authority] size=20></td></tr>";

print "<tr><td class=internal><b class=intro3>Operative:</b></td>
<td><input type=text name=Operative value=$nt[Operative] size=10></td></tr>";

print "<tr><td class=internal><b class=intro3>MachineType:</b></td>
<td><input type=text name=MachineType value=$nt[MachineType] size=20></td></tr>";

print "<tr><td class=internal><b class=intro3>Processor:</b></td>
<td><input type=text name=FreqProcessor value=$nt[FreqProcessor] size=20></td></tr>";

print "<tr><td class=internal><b class=intro3>Hard Disk:</b></td>
<td><input type=text name=HardDisk value=$nt[HardDisk] size=10></td></tr>";

print "<tr><td class=internal><b class=intro3>Memory:</b></td>
<td><input type=text name=Memory value=$nt[Memory] size=10></td></tr>";

print "<tr><td class=internal><b class=intro3>Operating System:</b></td>
<td><input type=text name=OS value=$nt[OS] size=20></td></tr>";

print "<tr><td class=internal><b class=intro3>Other Operating System:</b></td>
<td><input type=text name=OtherOS value=$nt[OtherOS] size=15></td></tr></table>";

print "<table><tr><td><input type=submit name=submit value=submit onclick=redirTimer()></td></tr></table>";
mysql_close($link);

if (isset($_GET['submit'])){

$db="mydb";
$link=mysql_connect("localhost","username","password") or die("couldn't connect to Mysql");
mysql_select_db($db) or die("Select Error:".mysql_error());
$query="SELECT * FROM machines where ref='$reference'";
$rt=mysql_query($query);
$nt=mysql_fetch_array($rt);
$id = $nt['id'];

$ref=$GET['ref'];
$ip=$
GET['IP'];
$mac=$GET['MAC'];
$name=$
GET['Name'];
$lo=$GET['Location'];
$aut=$
GET['Authority'];
$op=$GET['Operative'];
$ma=$
GET['MachineType'];
$pr=$GET['FreqProcessor'];
$hd=$
GET['HardDisk'];
$mem=$GET['Memory'];
$os=$
GET['OS'];
$oth=$_GET['OtherOS'];
$myref=$ref;
$myip=$ip;
$mymac=$mac;
$myname=$name;
$mylo=$lo;
$myaut=$aut;
$myop=$op;
$myma=$ma;
$mypr=$pr;
$myhd=$hd;
$mymem=$mem;
$myos=$os;
$myoth=$oth;

$test="update machines set IP='$myip',MAC='$mymac', Name='$myname',Location='$mylo', Authority='$myaut' , Operative='$myop' , MachineType='$myma', FreqProcessor='$mypr' , HardDisk='$myhd', Memory='$mymem' , OS='$myos' , OtherOS='$myoth' where ref = '$reference'";
$test1=mysql_query($test);
mysql_close($link);
}

?>

</form>
</body>
</html>

I realize that the loop following the statement isset(.. is not getting executed.Can anyone please point out my mistake.I even tried the following:

1.replaced the method=GET with method=PUT in the form declaration statement.
2.used isset($POST['submit']) instead of isset($GET['submit']) after declaring the method as PUT.
3.I have changed the registered globals to ON in .ini.

i use this method GET because I am using this single code for all my machines.the display is in a pop-up window.
Any help would be gratefully accepeted.

Thanks in advance,
mssmanian

    After a quick look i noticed you didn't specify an "action" for your form.
    Eg:
    <form action="test.php">

      Ok, i tooked a longer look and i realized that you code isn't going anywhere. Send me an pm and i may be able to help you .

        Think about the logic of your page, it's read from top to bottom, so each time it's rendered it never gets to the "if" statement. and each time you press submit it goes back to the top of the page and re-renders the page.

        This on the other hand checks if isset is set, if it is, it does the php bit, if it's not it renders the form.

        <?php
        
        if (isset($_GET['submit'])){
        
        $db="mydb";
        $link=mysql_connect("localhost","username","password") or die("couldn't connect to Mysql");
        mysql_select_db($db) or die("Select Error:".mysql_error());
        $query="SELECT * FROM machines where ref='$reference'";
        $rt=mysql_query($query);
        $nt=mysql_fetch_array($rt);
        $id = $nt['id'];
        
        $ref=$_GET['ref'];
        $ip=$_GET['IP'];
        $mac=$_GET['MAC'];
        $name=$_GET['Name'];
        $lo=$_GET['Location'];
        $aut=$_GET['Authority'];
        $op=$_GET['Operative'];
        $ma=$_GET['MachineType'];
        $pr=$_GET['FreqProcessor'];
        $hd=$_GET['HardDisk'];
        $mem=$_GET['Memory'];
        $os=$_GET['OS'];
        $oth=$_GET['OtherOS'];
        $myref=$ref;
        $myip=$ip;
        $mymac=$mac;
        $myname=$name;
        $mylo=$lo;
        $myaut=$aut;
        $myop=$op;
        $myma=$ma;
        $mypr=$pr;
        $myhd=$hd;
        $mymem=$mem;
        $myos=$os;
        $myoth=$oth;
        
        $test="update machines set IP='$myip',MAC='$mymac', Name='$myname',Location='$mylo', Authority='$myaut' , Operative='$myop' , MachineType='$myma', FreqProcessor='$mypr' , HardDisk='$myhd', Memory='$mymem' , OS='$myos' , OtherOS='$myoth' where ref = '$reference'";
        $test1=mysql_query($test);
        mysql_close($link);
        } else{
        ?>
        <html>
        <head><TITLE>m2.php</TITLE>
        
        
        <SCRIPT LANGUAGE="JavaScript">
        redirTime = "5000";
        redirURL = "thankyou.html";
        function redirTimer() { self.setTimeout("self.location.href = redirURL;",redirTime); }
        </script>
        
        </head>
        <body >
        <form name="m2.php" method="GET">
        <?
        $reference=$_GET['ref'];
        
        $db="mydb";
        $link=mysql_connect("localhost","username","password") or die("couldn't connect to Mysql");
        mysql_select_db($db) or die("Select Error:".mysql_error());
        $query="SELECT * FROM machines where ref='$reference'";
        $rt=mysql_query($query);
        $nt=mysql_fetch_array($rt);
        $id = $nt['id'];
        print "<table class=internal>";
        
        print "<tr><td class=internal><b class=intro3>Ref:</b></td>
        <td><input type=text name=ref value=$nt[ref] size=10></td></tr>";
        
        print "<tr><td class=internal><b class=intro3>IP:</b></td>
        <td><input type=text name=IP value=$nt[IP] size=20></td></tr>";
        
        print "<tr><td class=internal><b class=intro3>MAC:</b></td>
        <td><input type=text name=MAC value=$nt[MAC] size=20></td></tr>";
        
        print "<tr><td class=internal><b class=intro3>Name:</b></td>
        <td><input type=text name=Name value=$nt[Name] size=20></td></tr>";
        
        print "<tr><td class=internal><b class=intro3>Location:</b></td>
        <td><input type=text name=Location value=$nt[Location] size=20></td></tr>";
        
        print "<tr><td class=internal><b class=intro3>Authority:</b></td>
        <td><input type=text name=Authority value=$nt[Authority] size=20></td></tr>";
        
        print "<tr><td class=internal><b class=intro3>Operative:</b></td>
        <td><input type=text name=Operative value=$nt[Operative] size=10></td></tr>";
        
        print "<tr><td class=internal><b class=intro3>MachineType:</b></td>
        <td><input type=text name=MachineType value=$nt[MachineType] size=20></td></tr>";
        
        print "<tr><td class=internal><b class=intro3>Processor:</b></td>
        <td><input type=text name=FreqProcessor value=$nt[FreqProcessor] size=20></td></tr>";
        
        print "<tr><td class=internal><b class=intro3>Hard Disk:</b></td>
        <td><input type=text name=HardDisk value=$nt[HardDisk] size=10></td></tr>";
        
        print "<tr><td class=internal><b class=intro3>Memory:</b></td>
        <td><input type=text name=Memory value=$nt[Memory] size=10></td></tr>";
        
        print "<tr><td class=internal><b class=intro3>Operating System:</b></td>
        <td><input type=text name=OS value=$nt[OS] size=20></td></tr>";
        
        print "<tr><td class=internal><b class=intro3>Other Operating System:</b></td>
        <td><input type=text name=OtherOS value=$nt[OtherOS] size=15></td></tr></table>";
        
        print "<table><tr><td><input type=submit name=submit value=submit onclick=redirTimer()></td></tr></table>";
        mysql_close($link);
        
        
        
        ?>
        
        </form>
        </body>
        </html>
        <?php
        }
        ?>

          Thanks for responding..I tried using your code.But,it doesn't even display the form this time.It gives a blank page.

          Pls help me out!
          mssmanian

            May seem obvious, but have you changed the necessary details about database name password etc, because it works on my machine, obviously it doesn't show the details in the input boxes, but the form shows.

              Ofcourse,I have changed the username,pw etc...but the form doesn't show...Can you suggest an alternative like registering the changes once the change is made.I am sure that isset($_GET['submit']) is the problem...

                Well you could do away with the If isset altogether by splitting it into two pages, the form

                <html> 
                <head><TITLE>m2.php</TITLE> 
                
                
                <SCRIPT LANGUAGE="JavaScript"> 
                redirTime = "5000"; 
                redirURL = "thankyou.html"; 
                function redirTimer() { self.setTimeout("self.location.href = redirURL;",redirTime); } 
                </script> 
                
                </head> 
                <body > 
                <form name="m2.php" method="GET"> 
                <? 
                $reference=$_GET['ref']; 
                
                $db="mydb"; 
                $link=mysql_connect("localhost","username","password") or die("couldn't connect to Mysql"); 
                mysql_select_db($db) or die("Select Error:".mysql_error()); 
                $query="SELECT * FROM machines where ref='$reference'"; 
                $rt=mysql_query($query); 
                $nt=mysql_fetch_array($rt); 
                $id = $nt['id']; 
                print "<table class=internal>"; 
                
                print "<tr><td class=internal><b class=intro3>Ref:</b></td> 
                <td><input type=text name=ref value=$nt[ref] size=10></td></tr>"; 
                
                print "<tr><td class=internal><b class=intro3>IP:</b></td> 
                <td><input type=text name=IP value=$nt[IP] size=20></td></tr>"; 
                
                print "<tr><td class=internal><b class=intro3>MAC:</b></td> 
                <td><input type=text name=MAC value=$nt[MAC] size=20></td></tr>"; 
                
                print "<tr><td class=internal><b class=intro3>Name:</b></td> 
                <td><input type=text name=Name value=$nt[Name] size=20></td></tr>"; 
                
                print "<tr><td class=internal><b class=intro3>Location:</b></td> 
                <td><input type=text name=Location value=$nt[Location] size=20></td></tr>"; 
                
                print "<tr><td class=internal><b class=intro3>Authority:</b></td> 
                <td><input type=text name=Authority value=$nt[Authority] size=20></td></tr>"; 
                
                print "<tr><td class=internal><b class=intro3>Operative:</b></td> 
                <td><input type=text name=Operative value=$nt[Operative] size=10></td></tr>"; 
                
                print "<tr><td class=internal><b class=intro3>MachineType:</b></td> 
                <td><input type=text name=MachineType value=$nt[MachineType] size=20></td></tr>"; 
                
                print "<tr><td class=internal><b class=intro3>Processor:</b></td> 
                <td><input type=text name=FreqProcessor value=$nt[FreqProcessor] size=20></td></tr>"; 
                
                print "<tr><td class=internal><b class=intro3>Hard Disk:</b></td> 
                <td><input type=text name=HardDisk value=$nt[HardDisk] size=10></td></tr>"; 
                
                print "<tr><td class=internal><b class=intro3>Memory:</b></td> 
                <td><input type=text name=Memory value=$nt[Memory] size=10></td></tr>"; 
                
                print "<tr><td class=internal><b class=intro3>Operating System:</b></td> 
                <td><input type=text name=OS value=$nt[OS] size=20></td></tr>"; 
                
                print "<tr><td class=internal><b class=intro3>Other Operating System:</b></td> 
                <td><input type=text name=OtherOS value=$nt[OtherOS] size=15></td></tr></table>"; 
                
                print "<table><tr><td><input type=submit name=submit value=submit onclick=redirTimer()></td></tr></table>"; 
                mysql_close($link); 
                
                
                
                ?> 
                
                </form> 
                </body> 
                </html> 
                  

                and the M2.php form handler

                <?php 
                $db="mydb"; 
                $link=mysql_connect("localhost","username","password") or die("couldn't connect to Mysql"); 
                mysql_select_db($db) or die("Select Error:".mysql_error()); 
                $query="SELECT * FROM machines where ref='$reference'"; 
                $rt=mysql_query($query); 
                $nt=mysql_fetch_array($rt); 
                $id = $nt['id']; 
                
                $ref=$_GET['ref']; 
                $ip=$_GET['IP']; 
                $mac=$_GET['MAC']; 
                $name=$_GET['Name']; 
                $lo=$_GET['Location']; 
                $aut=$_GET['Authority']; 
                $op=$_GET['Operative']; 
                $ma=$_GET['MachineType']; 
                $pr=$_GET['FreqProcessor']; 
                $hd=$_GET['HardDisk']; 
                $mem=$_GET['Memory']; 
                $os=$_GET['OS']; 
                $oth=$_GET['OtherOS']; 
                $myref=$ref; 
                $myip=$ip; 
                $mymac=$mac; 
                $myname=$name; 
                $mylo=$lo; 
                $myaut=$aut; 
                $myop=$op; 
                $myma=$ma; 
                $mypr=$pr; 
                $myhd=$hd; 
                $mymem=$mem; 
                $myos=$os; 
                $myoth=$oth; 
                
                $test="update machines set IP='$myip',MAC='$mymac', Name='$myname',Location='$mylo', Authority='$myaut' , Operative='$myop' , MachineType='$myma', FreqProcessor='$mypr' , HardDisk='$myhd', Memory='$mymem' , OS='$myos' , OtherOS='$myoth' where ref = '$reference'"; 
                $test1=mysql_query($test); 
                mysql_close($link); 
                
                ?> 

                  I have split the code into two but in vain.There is no updating of the data.I tried printing the variables using $GET and $POST.I found that that only when using $GET,the variables are printed.Does updating work only with $POST?

                    mssmanian wrote:

                    I have split the code into two but in vain.There is no updating of the data.I tried printing the variables using $GET and $POST.I found that that only when using $GET,the variables are printed.Does updating work only with $POST?

                    you have not set action="target page"

                    <form action="m2.php" method="GET">
                    <!-- form to use GET, 
                    this is also the default, if method is not specified -->
                    </form>
                    
                    <form action="m2.php" method="POST">
                    <!-- 
                    action="m2.php"
                    tells where to go after form is submitted.
                    If action is not specified, or action=""
                    then comes back to this same page after submit
                    
                    method="POST"
                    form to use POST variables -->
                    </form>
                    

                      I guess update works only with $POST.I replaced the $GET with $_POST and the code works fine now.I have everything in a single code instead of splitting ..

                      Thanks for guiding me,
                      mssmanian

                        Write a Reply...