I can't query my database and cannot update, may I know what is the mistake in my coding?

<html>
<head>
<title>Index</title>
</head>
<div style="position: absolute; width: 1146px; height: 842px; z-index: 1; left:10px; top:1px" id="layer2" align="center">
<body background="E-Filing%20Management%20System%20Picture/Snow.gif">
<p align="center">
<img border="0" src="E-Filing%20Management%20System%20Picture/logo-RR-gif.gif" width="957" height="245"></p>
<p align="center"><span lang="zh-cn">&nbsp;</span></p>
<p align="center"><span lang="zh-cn"><img border="0" src="E-Filing%20Management%20System%20Picture/Association.gif" width="491" height="74"></span></p>
<p align="center"><span lang="zh-cn">&nbsp;</span></p>
<form method="post" action="AssociationEdit.php">
<?php

$connection = mysql_connect("localhost", "root", "")
or die("<font color='#FFFFFF'>Could not connect to MySQL </font>" .mysql_error() );

$selection = mysql_select_db("efiling")
or die("<font color='#FFFFFF'>Unable to select database. </font>" .mysql_error());

echo "<font size='4' face='Arial Rounded MT Bold'><font color='#FFFFFF'><span lang='en-us'>Please select a group : </font></span>";

echo"<select name='an'>";
echo "<option>---Please Select---</option>";
echo "<option value='A - H' selected>A - H</option>";
echo "<option value ='I - Q'>I - Q</option>";
echo "<option value ='R - Z'>R - Z</option>";
echo "</select>  &nbsp";
echo "<input type='submit' name='submit' value='Proceed'>";

if(isset($_POST["submit"]))
{
$an = $_POST["an"];
$submit = $_POST["submit"];
if($submit=="proceed")
{
$connection = mysql_connect("localhost", "root", "")
or die("<font color='#FFFFFF'>Could not connect to MySQL</font>" .mysql_error() );

$selection = mysql_select_db("efiling")
or die("<font color='#FFFFFF'>Unable to select database.</font>" .mysql_error());

if(isset($_POST['an']))
{
$an=$_POST["an"];
if($an == "A - G")
{
$sql = "SELECT * from association Where AssociationName BETWEEN 'A%' AND 'H%' ORDER BY AssociationName";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo "<p>";
echo "ID &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;	: <input type='text' name='id1' value='$row[ID]' size='40'><br>";
echo "Association Name   &nbsp;&nbsp;&nbsp;&nbsp; 	: <input type='text' name='an1' value='$row[AssociationName]' size='40'><br>";
echo "<input type='submit' name='submit' value='update'>";

}

else if($an == "H - Q")
{
$sql = "SELECT * from association Where AssociationName BETWEEN 'H%' AND 'R%' ORDER BY AssociationName";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo "<p>";
echo "ID &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;	: <input type='text' name='id2' value='$row[ID]' size='40'><br>";
echo "Association Name   &nbsp;&nbsp;&nbsp;&nbsp; 	: <input type='text' name='an2' value='$row[AssociationName]' size='40'><br>";
echo "<input type='submit' name='submit' value='update'>";

}

else if($an == "R - Z")
{
$sql = "SELECT * from association Where AssociationName BETWEEN 'R%' AND 'Z%' OR AssociationName LIKE 'Z%' ORDER BY AssociationName";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo "<p>";
echo "ID &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;	: <input type='text' name='id3' value='$row[ID]' size='40'><br>";
echo "Association Name   &nbsp;&nbsp;&nbsp;&nbsp; 	: <input type='text' name='an3' value='$row[AssociationName]' size='40'><br>";
echo "<input type='submit' name='submit' value='update'>";
}
}
}

if($submit=="update")
{
$id1 = $_POST["id1"];
$an1 = $_POST["an1"];
$id2 = $_POST["id2"];
$an2 = $_POST["an2"];
$id3 = $_POST["id3"];
$an3 = $_POST["an3"];
$sql = "UPDATE association SET AssociationName = '$an1''$an2''$an3', ID  = '$'id1' '$'id2' '$'id3'";
$result = mysql_query($sql);

if (mysql_num_rows($result2) < 1)
{
echo "<hr><h2>Record Updated</h2>";
}

else
{
print"<p align='center'>&nbsp;</p>
<table width='400' height='200' border='1' align='center' bordercolor='#66FF00'bgcolor='#0000FF'>
<td><div align='center'><font color='#FF0000' size='5'><p>The Association Name Has Been Registered, Please Enter Other Association Name</p> </font></div></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table> ";

print "<p align='center'>&nbsp;</p>";
echo "<HTML><META HTTP-EQUIV='REFRESH' CONTENT='2; URL=AssociationAdd.php'>
<CENTER><p>&nbsp;</p><p>&nbsp;
<tr>
<td align='center'><h12>please wait...</h12></td>
</tr>
</table><p>&nbsp;</p><p>&nbsp;</p></CENTER></BODY></HTML>";
}
}
}
?>
</form>

<p align="center"> </p>
<p align="center"> </p>
<p align="center"> </p>
<p align="center"> </p>
<p align="center"> </p>
</div>
</body>
</html>

Recommended Answers

All 25 Replies

Your SQL update query is wrong. I don't know what you're trying to update, but you've quoted the $ mark as well. Also, when you run this update query, your whole DB will get updated. Is that intentional?

If you can throw some light on what is it that you are trying to update we can help you with the correct UPDATE query required

Member Avatar for diafol
UPDATE association SET AssociationName = '$an1''$an2''$an3', ID  = '$'id1' '$'id2' '$'id3'

This is just plain wrong. What are you trying to say here - give a "non variable" or hard-coded values example.

Thanks for reply.
Is that I can query all data to edit?
Because I can't found a primary key, my database just have ID and Name, but my name might have same ID. So can I query all data and give the user to edit what they want?
For example, my database have aaa, bbb, ccc, ddd. Can I query all then give the user to select which one to edit? Now I just can query aaa only...

this is my edited coding.

<html>
<head>
<title>Index</title>
</head>
<div style="position: absolute; width: 1146px; height: 842px; z-index: 1; left:10px; top:1px" id="layer2" align="center">
<body background="E-Filing%20Management%20System%20Picture/Snow.gif">
<p align="center">
<img border="0" src="E-Filing%20Management%20System%20Picture/logo-RR-gif.gif" width="957" height="245"></p>
<p align="center"><span lang="zh-cn"> </span></p>
<p align="center"><span lang="zh-cn"><img border="0" src="E-Filing%20Management%20System%20Picture/Association.gif" width="491" height="74"></span></p>
<p align="center"><span lang="zh-cn"> </span></p>
<form method="post" action="AssociationEdit.php">
<?php

    $connection = mysql_connect("localhost", "root", "")  
        or die("<font color='#FFFFFF'>Could not connect to MySQL </font>" .mysql_error() );

    $selection = mysql_select_db("efiling")  
        or die("<font color='#FFFFFF'>Unable to select database. </font>" .mysql_error());
   
   
          $sql = "SELECT * from association Where AssociationName LIKE 'A%' OR AssociationName LIKE 'B%' OR AssociationName LIKE 'C%' OR AssociationName LIKE 'D%' OR AssociationName LIKE 'E%' OR AssociationName LIKE 'F%' OR AssociationName LIKE 'G%' ORDER BY AssociationName";
          $result = mysql_query($sql);
                  $row = mysql_fetch_array($result);
            echo "<p align='center'><input type='text' name='id1' value='$row[ID]' size='11'>";
            echo "     <input type='text' name='an1' value='$row[AssociationName]' size='77'></p><br>";
            echo "<input type='submit' name='submit' value='Update'>                              ";
          echo "<input type='submit' name='submit' value='Back'>";
        

    if(isset($_POST["submit"]))
    {
        $submit = $_POST["submit"];
        $id1 = $_POST["id1"];
           $an1 = $_POST["an1"];
                       
        if($submit=="Update")
         {
               $sql2 = "SELECT * from association Where AssociationName='$an1'";
              $result2 = mysql_query($sql2)
                    or die("<font color='#FFFFFF'>SQL select statement failed</font>");
         
              if (mysql_num_rows($result2) < 1)
              {
                $id1 = $_POST["id1"];
                   $an1 = $_POST["an1"];
                 $sql = "UPDATE association SET AssociationName = '$an1', ID = '$id1' WHERE AssociationName='$an1'";
                    $result2 = mysql_query($sql);               
                   echo "<hr><h2><font color='#FFFFFF'>Record Updated</font></h2>";
                }
               
                else
                {
                print"<p align='center'> </p>
                            <table width='400' height='200' border='1' align='center' bordercolor='#66FF00'bgcolor='#0000FF'>
                                <td><div align='center'><font color='#FF0000' size='5'><p>The Association Name Has Been Registered, Please Enter Other Association Name</p> </font></div></td>
                                    </tr>
                                      </table></td>
                                             </tr>
                                            </table></td>
                                            </tr>
                                      </table></td>
                                  </tr>
                            </table> ";

                         print "<p align='center'> </p>";
                         echo "<HTML><META HTTP-EQUIV='REFRESH' CONTENT='2; URL=Association.php'>
                                 <CENTER><p> </p><p>
                                        <tr>
                                        <td align='center'><h12>please wait...</h12></td>
                                        </tr>
                                </table><p> </p><p> </p></CENTER></BODY></HTML>";
            }
         }
         
         if($submit=="Back")
         {
            print "<script language ='javascript'>window.location.href='Association.php'</script>";
         }
    }
?>
</form>

<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
</div>
</body>
</html>
Member Avatar for diafol
if (mysql_num_rows($result2) < 1)
              {
                $id1 = $_POST["id1"];
                   $an1 = $_POST["an1"];
                 $sql = "UPDATE association SET AssociationName = '$an1', ID = '$id1' WHERE AssociationName='$an1'";
                    $result2 = mysql_query($sql);               
                   echo "<hr><h2><font color='#FFFFFF'>Record Updated</font></h2>";
                }

if number of rows < 1, it means there are no records, therefore no records to update, which means UPDATE won't work.

Change '< 1' to '> 0'.

if (mysql_num_rows($result2) < 1)
              {
                $id1 = $_POST["id1"];
                   $an1 = $_POST["an1"];
                 $sql = "UPDATE association SET AssociationName = '$an1', ID = '$id1' WHERE AssociationName='$an1'";
                    $result2 = mysql_query($sql);               
                   echo "<hr><h2><font color='#FFFFFF'>Record Updated</font></h2>";
                }

if number of rows < 1, it means there are no records, therefore no records to update, which means UPDATE won't work.

Change '< 1' to '> 0'.

But there is no problem when I using < 1, now my problem is:
I don't have primary key, so can I query all data from table and give user to edit what they want to edit? Although I already write that AssociationName LIKE = 'A%' OR AssociationName LIKE = 'B%' OR AssociationName LIKE = 'C%' OR AssociationName LIKE = 'D%' OR AssociationName LIKE = 'E%' OR AssociationName LIKE = 'F%' OR AssociationName LIKE = 'G%'; but I just can query the first data only. For example, my database's AssociationName got aaa, bbb, ccc, ddd and eee. When I query, It just show aaa for me to edit.

I don't have primary key because my database just have ID and AssociationName, and my ID might same.

Member Avatar for diafol

just create a primary key id. save yourself a lot of trouble.

just create a primary key id. save yourself a lot of trouble.

I created primary key already. But why when I click update, it shows 'Record Updated', but my database didn't change?
Below is my coding:

<html>
<head>
<title>Index</title>
</head>
<div style="position: absolute; width: 1146px; height: 842px; z-index: 1; left:10px; top:1px" id="layer2" align="center">
<body background="E-Filing%20Management%20System%20Picture/Snow.gif">
<p align="center">
<img border="0" src="E-Filing%20Management%20System%20Picture/logo-RR-gif.gif" width="957" height="245"></p>
<p align="center"><span lang="zh-cn">&nbsp;</span></p>
<p align="center"><span lang="zh-cn"><img border="0" src="E-Filing%20Management%20System%20Picture/Association.gif" width="491" height="74"></span></p>
<p align="center"><span lang="zh-cn">&nbsp;</span></p>
<form method="post" action="AssociationEdit.php">
<?php

	$connection = mysql_connect("localhost", "root", "")  
		or die("<font color='#FFFFFF'>Could not connect to MySQL </font>" .mysql_error() );

	$selection = mysql_select_db("efiling")  
		or die("<font color='#FFFFFF'>Unable to select database. </font>" .mysql_error());
	
	echo "<font size='3' face='Arial Rounded MT Bold'><font color='#FFFFFF'>Please insert Primary Key :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></font>";
	echo"<input type='text' name='PK' size='6'>";
	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' name='submit' value='Search'>"; 


	if(isset($_POST["submit"]))
	{
		$submit = $_POST["submit"];
		$PK = $_POST["PK"];
		  
		if($submit=="Search")
  		{
   			$PK = $_POST["PK"];
			$sql = "SELECT * FROM association where PriKey = '$PK'";
			$result = mysql_query($sql)
				or die("<font color='#FFFFFF'>SQL select statement failed</font>");
				
			if($row = mysql_fetch_array($result))
			{
				echo "<p><br><br>";
				echo "<font size='3' face='Arial Rounded MT Bold'><font color='#FFFFFF'>Index :&nbsp;&nbsp;</font></font><input type='text' name='id' value='$row[ID]' size='11'> ";
				echo "<font size='3' face='Arial Rounded MT Bold'><font color='#FFFFFF'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Association Name :&nbsp;&nbsp;</font></font><input type='text' name='an' value='$row[AssociationName]' size='77'> ";
				echo "<br><br><br><input type='submit' name='submit' value='Update'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				echo "<input type='submit' name='submit' value='Back'>";
			}
			
			else
			{
				print"<p align='center'>&nbsp;</p>
							<table width='400' height='200' border='1' align='center' bordercolor='#66FF00'bgcolor='#0000FF'>
              		          <td><div align='center'><font color='#FF0000' size='5'><p>You might enter a wrong Primary Key, please check back.</p> </font></div></td>
               				     </tr>
         					         </table></td>
        		       				      </tr>
      								      </table></td>
       									 </tr>
     								 </table></td>
 								 </tr>
							</table> ";

 						print "<p align='center'>&nbsp;</p>";
						 echo "<HTML><META HTTP-EQUIV='REFRESH' CONTENT='2; URL=AssociationEdit.php'>
                                 <CENTER><p>&nbsp;</p><p>&nbsp;
                                        <tr>
                                        <td align='center'><h12>please wait...</h12></td>
                                        </tr>
                                </table><p>&nbsp;</p><p>&nbsp;</p></CENTER></BODY></HTML>";

			}
    	}

   			        
		if($submit=="Update")
 		{
 			$PK = $_POST["PK"];
 			$id = $_POST["id"];
 			$an = $_POST["an"];
   		    $sql2 = "SELECT * from association Where AssociationName='$an'";
			$result2 = mysql_query($sql2)
      	 	 	or die("<font color='#FFFFFF'>SQL select statement failed</font>");
          
			if (mysql_num_rows($result2) <1)
			{
				$PK = $_POST["PK"];
				$id = $_POST["id"];
   				$an = $_POST["an"];
			 	$sql = "UPDATE association SET ID = '$id',  AssociationName = '$an' WHERE PriKey='$PK'";
   			 	$result = mysql_query($sql);
  			 	echo "<hr><h2><font color='#FFFFFF'>Record Updated</font></h2>";
  			 	echo "<br><input type='submit' name='submit' value='Back'>";
   			}
   			 
   			else
   			{
				print"<p align='center'>&nbsp;</p>
							<table width='400' height='200' border='1' align='center' bordercolor='#66FF00'bgcolor='#0000FF'>
              		          <td><div align='center'><font color='#FF0000' size='5'><p>The Association Name Has Been Registered, Please Enter Other Association Name</p> </font></div></td>
               				     </tr>
         					         </table></td>
        		       				      </tr>
      								      </table></td>
       									 </tr>
     								 </table></td>
 								 </tr>
							</table> ";

 						print "<p align='center'>&nbsp;</p>";
						 echo "<HTML><META HTTP-EQUIV='REFRESH' CONTENT='2; URL=AssociationEdit.php'>
                                 <CENTER><p>&nbsp;</p><p>&nbsp;
                                        <tr>
                                        <td align='center'><h12>please wait...</h12></td>
                                        </tr>
                                </table><p>&nbsp;</p><p>&nbsp;</p></CENTER></BODY></HTML>";
			}
 		} 
 		
 		if($submit=="Back")
 		{
			print "<script language ='javascript'>window.location.href='Association.php'</script>";
 		}
	}
?>
</form>

<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
</div> 
</body>
</html>

Besides, when I search with primary key again, the data it query still the previous one, means no updated.

Member Avatar for diafol

I'd suggest you clean up your HTML first, it's a bit of a mess. However, will have a look at the php - back in in a few minutes.

Member Avatar for diafol

No sorry Kira, your code is such a mess, I don't know where to start.

Suggestions:

1. Take out all the unnecessary html attributes - forget trying to style this thing until it actually works - get rid of all those needless font and div tags
2. Remove all padding (&nbsp; )
3. Decide whether you need tables - probably not
4. Use a header() command rather than "http refresh header"
5. Do not have html tags before the body tag
6. Try to separate html and php as much as possible - it's best to put as much of your php as possible before the <html> tag.

Once you've done this, perhaps you can resubmit the code as it will be more 'readable'.

I created primary key already. But why when I click update, it shows 'Record Updated', but my database didn't change?
Below is my coding:

<html>
<head>
<title>Index</title>
</head>
<div style="position: absolute; width: 1146px; height: 842px; z-index: 1; left:10px; top:1px" id="layer2" align="center">
<body background="E-Filing%20Management%20System%20Picture/Snow.gif">
<p align="center">
<img border="0" src="E-Filing%20Management%20System%20Picture/logo-RR-gif.gif" width="957" height="245"></p>
<p align="center"><span lang="zh-cn"> </span></p>
<p align="center"><span lang="zh-cn"><img border="0" src="E-Filing%20Management%20System%20Picture/Association.gif" width="491" height="74"></span></p>
<p align="center"><span lang="zh-cn"> </span></p>
<form method="post" action="AssociationEdit.php">
<?php

    $connection = mysql_connect("localhost", "root", "")  
        or die("<font color='#FFFFFF'>Could not connect to MySQL </font>" .mysql_error() );

    $selection = mysql_select_db("efiling")  
        or die("<font color='#FFFFFF'>Unable to select database. </font>" .mysql_error());
   
    echo "<font size='3' face='Arial Rounded MT Bold'><font color='#FFFFFF'> : Please insert Primary Key :      </font></font>";
    echo"<input type='text' name='PK' size='6'>";
    echo "       <input type='submit' name='submit' value='Search'>";


    if(isset($_POST["submit"]))
    {
        $submit = $_POST["submit"];
        $PK = $_POST["PK"];
         
        if($submit=="Search")
          {
               $PK = $_POST["PK"];
            $sql = "SELECT * FROM association where PriKey = '$PK'";
            $result = mysql_query($sql)
                or die("<font color='#FFFFFF'>SQL select statement failed</font>");
               
            if($row = mysql_fetch_array($result))
            {
                echo "<p><br><br>";
                echo "<font size='3' face='Arial Rounded MT Bold'><font color='#FFFFFF'>Index :  </font></font><input type='text' name='id' value='$row[ID]' size='11'> ";
                echo "<font size='3' face='Arial Rounded MT Bold'><font color='#FFFFFF'>          Association Name :  </font></font><input type='text' name='an' value='$row[AssociationName]' size='77'> ";
                echo "<br><br><br><input type='submit' name='submit' value='Update'>                         ";
                echo "<input type='submit' name='submit' value='Back'>";
            }
            
            else
            {
                print"<p align='center'> </p>
                            <table width='400' height='200' border='1' align='center' bordercolor='#66FF00'bgcolor='#0000FF'>
                                <td><div align='center'><font color='#FF0000' size='5'><p>You might enter a wrong Primary Key, please check back.</p> </font></div></td>
                                    </tr>
                                      </table></td>
                                             </tr>
                                            </table></td>
                                            </tr>
                                      </table></td>
                                  </tr>
                            </table> ";

                         print "<p align='center'> </p>";
                         echo "<HTML><META HTTP-EQUIV='REFRESH' CONTENT='2; URL=AssociationEdit.php'>
                                 <CENTER><p> </p><p>
                                        <tr>
                                        <td align='center'><h12>please wait...</h12></td>
                                        </tr>
                                </table><p> </p><p> </p></CENTER></BODY></HTML>";

            }
        }

                       
        if($submit=="Update")
         {
             $PK = $_POST["PK"];
             $id = $_POST["id"];
             $an = $_POST["an"];
             $sql2 = "SELECT * from association Where AssociationName='$an'";
             $result2 = mysql_query($sql2)
                    or die("<font color='#FFFFFF'>SQL select statement failed</font>");
         
            if (mysql_num_rows($result2) <1)
            {
                $PK = $_POST["PK"];
                $id = $_POST["id"];
                   $an = $_POST["an"];
                 $sql = "UPDATE association SET ID = '$id',  AssociationName = '$an' WHERE PriKey='$PK'";
                    $result = mysql_query($sql);
                   echo "<hr><h2><font color='#FFFFFF'>Record Updated</font></h2>";
                   echo "<br><input type='submit' name='submit' value='Back'>";
               }
               
               else
               {
                print"<p align='center'> </p>
                            <table width='400' height='200' border='1' align='center' bordercolor='#66FF00'bgcolor='#0000FF'>
                                <td><div align='center'><font color='#FF0000' size='5'><p>The Association Name Has Been Registered, Please Enter Other Association Name</p> </font></div></td>
                                    </tr>
                                      </table></td>
                                             </tr>
                                            </table></td>
                                            </tr>
                                      </table></td>
                                  </tr>
                            </table> ";

                         print "<p align='center'> </p>";
                         echo "<HTML><META HTTP-EQUIV='REFRESH' CONTENT='2; URL=AssociationEdit.php'>
                                 <CENTER><p> </p><p>
                                        <tr>
                                        <td align='center'><h12>please wait...</h12></td>
                                        </tr>
                                </table><p> </p><p> </p></CENTER></BODY></HTML>";
            }
         }
         
         if($submit=="Back")
         {
            print "<script language ='javascript'>window.location.href='Association.php'</script>";
         }
    }
?>
</form>

<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
</div>
</body>
</html>

Besides, when I search with primary key again, the data it query still the previous one, means no updated.

No sorry Kira, your code is such a mess, I don't know where to start.

Suggestions:

1. Take out all the unnecessary html attributes - forget trying to style this thing until it actually works - get rid of all those needless font and div tags
2. Remove all padding (&nbsp; )
3. Decide whether you need tables - probably not
4. Use a header() command rather than "http refresh header"
5. Do not have html tags before the body tag
6. Try to separate html and php as much as possible - it's best to put as much of your php as possible before the <html> tag.

Once you've done this, perhaps you can resubmit the code as it will be more 'readable'.

If no use &nbsp, the position is not nice wor...
I need table. If not the position will run when I use it at other computers.

If no use &nbsp, the position is not nice wor...
I need table. If not the position will run when I use it at other computers.

No, he meant that it should be removed so we can get a clear picture of what you are trying to do. First fix the example, then move on to all your presentation needs.

No, he meant that it should be removed so we can get a clear picture of what you are trying to do. First fix the example, then move on to all your presentation needs.

I removed already. At #12

You forgot to include the primary key as a hidden input in the FORM (after you do the search). Insert this bit of code after line number 42.

echo "<input type='hidden' name='PK' value='$row[PriKey]'> ";

This works for me (I actually set up the DB on my localhost :P )

Close if this solves your problem, please

Also, just so you know. It's better if you access your arrays like so (correct me if I'm wrong)

echo "<input type='hidden' name='PK' value='{$row['PriKey']}'> ";

Another thing, there is a bug in your code that stops people from searching for more records if you already have a search result open. Although it actually searches for the record, you are given the same result because there are two inputs with name PK. Changing one of those two to something else should solve the problem.

It solved. But now I encounter another problem.
When I click enter the Primary Key and search, it will query the data come out. But when I search for second time, it still remain the previous data. I need to Back to previous page and search again. May I know how to solve it?

It solved. But now I encounter another problem.
When I click enter the Primary Key and search, it will query the data come out. But when I search for second time, it still remain the previous data. I need to Back to previous page and search again. May I know how to solve it?

Another thing, there is a bug in your code that stops people from searching for more records if you already have a search result open. Although it actually searches for the record, you are given the same result because there are two inputs with name PK. Changing one of those two to something else should solve the problem.

I'm facing this problem now, but I'm not catch what you mean? What should I change?

It solved. But now I encounter another problem.
When I click enter the Primary Key and search, it will query the data come out. But when I search for second time, it still remain the previous data. I need to Back to previous page and search again. May I know how to solve it?

Like I said earlier, just moving around the POSTed variables will fix it. Here is the full code of the example.

<html>
<head>
<title>Index</title>
</head>
<div style="position: absolute; width: 1146px; height: 842px; z-index: 1; left:10px; top:1px" id="layer2" align="center">
<body background="E-Filing%20Management%20System%20Picture/Snow.gif">
<p align="center">
<img border="0" src="E-Filing%20Management%20System%20Picture/logo-RR-gif.gif" width="957" height="245"></p>
<p align="center"><span lang="zh-cn"> </span></p>
<p align="center"><span lang="zh-cn"><img border="0" src="E-Filing%20Management%20System%20Picture/Association.gif" width="491" height="74"></span></p>
<p align="center"><span lang="zh-cn"> </span></p>
<form method="post" action="test.php">
<?php

    $connection = mysql_connect("localhost", "root", "")  
        or die("<font color='#FFFFFF'>Could not connect to MySQL </font>" .mysql_error() );

    $selection = mysql_select_db("efiling")  
        or die("<font color='#FFFFFF'>Unable to select database. </font>" .mysql_error());
   
    echo "<font size='3' face='Arial Rounded MT Bold'><font color='#FFFFFF'> : Please insert Primary Key :      </font></font>";
    echo"<input type='text' name='PK' size='6'>";
    echo "       <input type='submit' name='submit' value='Search'>";


    if(isset($_POST["submit"]))
    {
        $submit = $_POST["submit"];
        $PK = $_POST["PK"];
         
        if($submit=="Search")
          {
               $PK = $_POST["PK"];
            $sql = "SELECT * FROM association where PriKey = '$PK'";
	 
            $result = mysql_query($sql)
                or die("<font color='#FFFFFF'>SQL select statement failed</font>");
               
            if($row = mysql_fetch_array($result))
            {
			
 
                echo "<p><br><br>";
                echo "<font size='3' face='Arial Rounded MT Bold'><font color='#FFFFFF'>Index :  </font></font><input type='text' name='id' value='$row[ID]' size='11'> ";
				echo "<input type='hidden' name='PKF' value='{$row['PriKey']}'> ";
                echo "<font size='3' face='Arial Rounded MT Bold'><font color='#FFFFFF'>          Association Name :  </font></font><input type='text' name='an' value='$row[AssociationName]' size='77'> ";
                echo "<br><br><br><input type='submit' name='submit' value='Update'>                         ";
                echo "<input type='submit' name='submit' value='Back'>";
            }
            
            else
            {
                print"<p align='center'> </p>
                            <table width='400' height='200' border='1' align='center' bordercolor='#66FF00'bgcolor='#0000FF'>
                                <td><div align='center'><font color='#FF0000' size='5'><p>You might enter a wrong Primary Key, please check back.</p> </font></div></td>
                                    </tr>
                                      </table></td>
                                             </tr>
                                            </table></td>
                                            </tr>
                                      </table></td>
                                  </tr>
                            </table> ";

                         print "<p align='center'> </p>";
                         echo "<HTML><META HTTP-EQUIV='REFRESH' CONTENT='2; URL=AssociationEdit.php'>
                                 <CENTER><p> </p><p>
                                        <tr>
                                        <td align='center'><h12>please wait...</h12></td>
                                        </tr>
                                </table><p> </p><p> </p></CENTER></BODY></HTML>";

            }
        }

                       
        if($submit=="Update")
         {
 
             $PK = $_POST["PKF"];
             $id = $_POST["id"];
             $an = $_POST["an"];
             $sql2 = "SELECT * from association Where AssociationName='$an'";
             $result2 = mysql_query($sql2)
                    or die("<font color='#FFFFFF'>SQL select statement failed</font>");

            if (mysql_num_rows($result2) <1)
            {         	
                $PK = $_POST["PKF"];
                $id = $_POST["id"];
                   $an = $_POST["an"];
                 $sql = "UPDATE association SET ID = '$id',  AssociationName = '$an' WHERE PriKey='$PK'";
 
                    $result = mysql_query($sql);
                   echo "<hr><h2><font color='#FFFFFF'>Record Updated</font></h2>";
                   echo "<br><input type='submit' name='submit' value='Back'>";
               }
               
               else
               {
                print"<p align='center'> </p>
                            <table width='400' height='200' border='1' align='center' bordercolor='#66FF00'bgcolor='#0000FF'>
                                <td><div align='center'><font color='#FF0000' size='5'><p>The Association Name Has Been Registered, Please Enter Other Association Name</p> </font></div></td>
                                    </tr>
                                      </table></td>
                                             </tr>
                                            </table></td>
                                            </tr>
                                      </table></td>
                                  </tr>
                            </table> ";

                         print "<p align='center'> </p>";
                         echo "<HTML><META HTTP-EQUIV='REFRESH' CONTENT='2; URL=AssociationEdit.php'>
                                 <CENTER><p> </p><p>
                                        <tr>
                                        <td align='center'><h12>please wait...</h12></td>
                                        </tr>
                                </table><p> </p><p> </p></CENTER></BODY></HTML>";
            }
         }
         
         if($submit=="Back")
         {
            print "<script language ='javascript'>window.location.href='Association.php'</script>";
         }
    }
?>
</form>

<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
</div>
</body>
</html>

Like I said earlier, just moving around the POSTed variables will fix it. Here is the full code of the example.

<html>
<head>
<title>Index</title>
</head>
<div style="position: absolute; width: 1146px; height: 842px; z-index: 1; left:10px; top:1px" id="layer2" align="center">
<body background="E-Filing%20Management%20System%20Picture/Snow.gif">
<p align="center">
<img border="0" src="E-Filing%20Management%20System%20Picture/logo-RR-gif.gif" width="957" height="245"></p>
<p align="center"><span lang="zh-cn"> </span></p>
<p align="center"><span lang="zh-cn"><img border="0" src="E-Filing%20Management%20System%20Picture/Association.gif" width="491" height="74"></span></p>
<p align="center"><span lang="zh-cn"> </span></p>
<form method="post" action="test.php">
<?php

    $connection = mysql_connect("localhost", "root", "")  
        or die("<font color='#FFFFFF'>Could not connect to MySQL </font>" .mysql_error() );

    $selection = mysql_select_db("efiling")  
        or die("<font color='#FFFFFF'>Unable to select database. </font>" .mysql_error());
   
    echo "<font size='3' face='Arial Rounded MT Bold'><font color='#FFFFFF'> : Please insert Primary Key :      </font></font>";
    echo"<input type='text' name='PK' size='6'>";
    echo "       <input type='submit' name='submit' value='Search'>";


    if(isset($_POST["submit"]))
    {
        $submit = $_POST["submit"];
        $PK = $_POST["PK"];
         
        if($submit=="Search")
          {
               $PK = $_POST["PK"];
            $sql = "SELECT * FROM association where PriKey = '$PK'";
	 
            $result = mysql_query($sql)
                or die("<font color='#FFFFFF'>SQL select statement failed</font>");
               
            if($row = mysql_fetch_array($result))
            {
			
 
                echo "<p><br><br>";
                echo "<font size='3' face='Arial Rounded MT Bold'><font color='#FFFFFF'>Index :  </font></font><input type='text' name='id' value='$row[ID]' size='11'> ";
				echo "<input type='hidden' name='PKF' value='{$row['PriKey']}'> ";
                echo "<font size='3' face='Arial Rounded MT Bold'><font color='#FFFFFF'>          Association Name :  </font></font><input type='text' name='an' value='$row[AssociationName]' size='77'> ";
                echo "<br><br><br><input type='submit' name='submit' value='Update'>                         ";
                echo "<input type='submit' name='submit' value='Back'>";
            }
            
            else
            {
                print"<p align='center'> </p>
                            <table width='400' height='200' border='1' align='center' bordercolor='#66FF00'bgcolor='#0000FF'>
                                <td><div align='center'><font color='#FF0000' size='5'><p>You might enter a wrong Primary Key, please check back.</p> </font></div></td>
                                    </tr>
                                      </table></td>
                                             </tr>
                                            </table></td>
                                            </tr>
                                      </table></td>
                                  </tr>
                            </table> ";

                         print "<p align='center'> </p>";
                         echo "<HTML><META HTTP-EQUIV='REFRESH' CONTENT='2; URL=AssociationEdit.php'>
                                 <CENTER><p> </p><p>
                                        <tr>
                                        <td align='center'><h12>please wait...</h12></td>
                                        </tr>
                                </table><p> </p><p> </p></CENTER></BODY></HTML>";

            }
        }

                       
        if($submit=="Update")
         {
 
             $PK = $_POST["PKF"];
             $id = $_POST["id"];
             $an = $_POST["an"];
             $sql2 = "SELECT * from association Where AssociationName='$an'";
             $result2 = mysql_query($sql2)
                    or die("<font color='#FFFFFF'>SQL select statement failed</font>");

            if (mysql_num_rows($result2) <1)
            {         	
                $PK = $_POST["PKF"];
                $id = $_POST["id"];
                   $an = $_POST["an"];
                 $sql = "UPDATE association SET ID = '$id',  AssociationName = '$an' WHERE PriKey='$PK'";
 
                    $result = mysql_query($sql);
                   echo "<hr><h2><font color='#FFFFFF'>Record Updated</font></h2>";
                   echo "<br><input type='submit' name='submit' value='Back'>";
               }
               
               else
               {
                print"<p align='center'> </p>
                            <table width='400' height='200' border='1' align='center' bordercolor='#66FF00'bgcolor='#0000FF'>
                                <td><div align='center'><font color='#FF0000' size='5'><p>The Association Name Has Been Registered, Please Enter Other Association Name</p> </font></div></td>
                                    </tr>
                                      </table></td>
                                             </tr>
                                            </table></td>
                                            </tr>
                                      </table></td>
                                  </tr>
                            </table> ";

                         print "<p align='center'> </p>";
                         echo "<HTML><META HTTP-EQUIV='REFRESH' CONTENT='2; URL=AssociationEdit.php'>
                                 <CENTER><p> </p><p>
                                        <tr>
                                        <td align='center'><h12>please wait...</h12></td>
                                        </tr>
                                </table><p> </p><p> </p></CENTER></BODY></HTML>";
            }
         }
         
         if($submit=="Back")
         {
            print "<script language ='javascript'>window.location.href='Association.php'</script>";
         }
    }
?>
</form>

<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
<p align="center"></p>
</div>
</body>
</html>

Solved...
Thanks a lot.
Thank you so much. :)

No problem. Please mark as solved if you're done with it.

No SQL injection prevention; not a smart idea. Someone could delete your entire database. PHP should be using single quotes instead of double quotes, but that's just me being picky. HTML is a little sloppy and it seems that CSS would be able to fix your spacing issues.

You major concern should be SQL injection prevention. Beef up the security; your script is a threat waiting to happen.

No SQL injection prevention; not a smart idea. Someone could delete your entire database. PHP should be using single quotes instead of double quotes, but that's just me being picky. HTML is a little sloppy and it seems that CSS would be able to fix your spacing issues.

You major concern should be SQL injection prevention. Beef up the security; your script is a threat waiting to happen.

How to do the SQL injection prevention?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.