Forum Moderators: coopster

Message Too Old, No Replies

PHP UPDATE Problem

         

kira4

4:43 pm on Oct 16, 2009 (gmt 0)

10+ Year Member



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">&#12288;</p>
<p align="center">&#12288;</p>
<p align="center">&#12288;</p>
<p align="center">&#12288;</p>
<p align="center">&#12288;</p>
</div>
</body>
</html>

skinsey

12:08 am on Oct 17, 2009 (gmt 0)

10+ Year Member



I'll take one at a time.

for your update

"UPDATE association SET AssociationName = '$an1''$an2''$an3', ID = '$id1''$id2''$id3' WHERE something = '$something'";

You need to put in your where clause or your entire table is updated. Also variables they sould be like this

'$id1' not '$'id1'

skinsey

12:39 am on Oct 17, 2009 (gmt 0)

10+ Year Member



On your select.

To my understanding BETWEEN is for numbers. and the wildcard is used on LIKE. The only thing I could think of is to have a huge or statement on like.

If anyone has any good ideas for this let him know.

SELECT * from association Where AssociationName LIKE "H%" OR AssociationName LIKE 'I%' ORDER BY AssociationName

skinsey

12:54 am on Oct 17, 2009 (gmt 0)

10+ Year Member



Also I would suggest creating two files to open and close your database that way you only have one place to change it if needed.

connect.php
<?
$DBName = "dbname";
$DBUser = "username";
$DBPass = "password";
$DBHost = "host";

$link = mysql_connect ( $DBHost, $DBUser, $DBPass )
or die("Unable to connect");
mysql_select_db ( $DBName );

?>

close.php

<?
mysql_close ( $link );
?>

Then on your pages.
at the top
include("connect.php");
and at the bottom
include("close.php");

kira4

1:36 pm on Oct 17, 2009 (gmt 0)

10+ Year Member



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

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>

skinsey

10:55 pm on Oct 17, 2009 (gmt 0)

10+ Year Member



When developing a system you need to make sure that you do have a primary key in all tables. The layout of all tables needs to be setup first when designing a system. Make sure you can create the ability to update and delete anything with a query.

Here is an example how to create the association table.

$query = "CREATE TABLE association (
PID INT(8) NOT NULL AUTO_INCREMENT,
AssociationName VARCHAR (30) NOT NULL,
ID INT (5) NOT NULL,
PRIMARY KEY (PID))";

$temp = mysql_query($query)
or die(mysql_error());

echo "Association Table created. <br>";

Here is an example to add a field.

$query = "ALTER TABLE association ADD(
PID INT(8) NOT NULL AUTO_INCREMENT)";

$temp = mysql_query($query)
or die(mysql_error());

The next step is for updates you need to determine who can update what. If anyone can update anything then there is no need for user authentication. If you are going to limit it, then a user table must be setup and workflow created to support it.

This is just for information.
For the current logic in your query. It will display only the last record found. Here is an example to show what your current results are.

while($row = mysql_fetch_array($result2))
{
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'> ";

Hope this helps, make sure your system is setup correctly. Then, if you get stuck go ahead and post your question. Don't give up it just takes a little time.

kira4

4:16 pm on Oct 18, 2009 (gmt 0)

10+ Year Member



[QUOTE=ardav;1018148]just create a primary key id. save yourself a lot of trouble.[/QUOTE]
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.

skinsey

12:19 am on Oct 19, 2009 (gmt 0)

10+ Year Member



Your primary key will be a hidden value on your form, they can't change it.

You would have them first do a search. Then when the results are displayed you would create your form with the hidded value of the PK and let them modify the id and name fields.

if($PK = NULL)
{
Do an insert because this is a new record
}
if($PK != NULL)
{
"UPDATE association SET ID = '$id', AssociationName = '$an' WHERE PriKey='$PK'";
}

The reason that it is not updating is because you are saying that if the number of results are less than 1 then update. So that means it will never update because it it only true when it is 0. Change to this.

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'>";
}

kira4

10:34 am on Oct 19, 2009 (gmt 0)

10+ Year Member



When I change to this, there is nothing happen when I click update. 'Revord Updated' also no appear.

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'>";
}

skinsey

10:54 am on Oct 19, 2009 (gmt 0)

10+ Year Member



I didn't take the time to review all the code.
Change this line to this.

$sql2 = "SELECT * from association WHERE PriKey='$PK'";

kira4

11:05 am on Oct 19, 2009 (gmt 0)

10+ Year Member



It straight away show that "The Association Name Has Been Registered, Please Enter Other Association Name".

skinsey

11:22 am on Oct 19, 2009 (gmt 0)

10+ Year Member



Give this a try

add this line in above your submit button and let me know what happens

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

skinsey

11:29 am on Oct 19, 2009 (gmt 0)

10+ Year Member



should be like this

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

add above the update button

kira4

11:38 am on Oct 19, 2009 (gmt 0)

10+ Year Member



Done.
Thank you.
This is my code.

<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'>";
echo "<input type='hidden' name='PK' value='$row[PriKey]'> ";
}

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'>";
echo "<input type='hidden' name='PK' value='$row[PriKey]'> ";
}

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>

$sql2 = "SELECT * from association Where AssociationName='$an'"; is correct.

kira4

11:52 am on Oct 19, 2009 (gmt 0)

10+ Year Member



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

skinsey

12:23 pm on Oct 19, 2009 (gmt 0)

10+ Year Member



Save this page as test.php to your server after you modify the mysql_connect

<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="test.php">
<?php
$submit = $_POST["submit"];
$PK = $_POST["PK"];
$id = $_POST["id"];
$an = $_POST["an"];
$GETPK = $_POST["GETPK"];

$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='GETPK' size='6'>";
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' name='submit' value='Search'>";

if(isset($_POST["submit"]))
{

if($submit=="Search")
{

$sql = "SELECT * FROM association where PriKey = '$GETPK'";
$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 "<input type='hidden' name='PK' value='$row[PriKey]'> ";
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")
{

$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)
{
$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'>self.location='Association.php'</script>";
}
}
?>
</form>
</div>
</body>
</html>

kira4

12:33 pm on Oct 19, 2009 (gmt 0)

10+ Year Member



Thanks a lot. Solved already. :)