Page 1 of 1

update query via form not working

Posted: Tue Apr 15, 2008 7:51 pm
by gammaman
I have the following three pages that work together to update a table. It does not work. I think the problem is that the data is not being sent from the second page to the third.

Code: Select all

 
<html>
<body>
<table border="1">
<tr><th>Dept ID</th><th>Dept Name</th><th>Modify</th></tr>
<?php
$conn=mysql_connect("localhost", "fierm", "13183");
if (!$conn){
    echo "failed";
}else{
    mysql_select_db("fierm");
    $result=mysql_query("SELECT DeptID, DeptName FROM department");
    
    while($row=mysql_fetch_array($result)){
        $strUrl="<a href=\"second.php?id=$row[0]&dp=$row[1]\">Edit</a>";
        echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$strUrl</td></tr>\n"; }
}
mysql_free_result($result);
mysql_close($conn);
?>
</table>
</body>
</html>
 

Code: Select all

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
 
<html>
<head>
    <title>Untitled</title>
</head>
<?php
    $Conn=mysql_connect("localhost","fierm","13183");
    mysql_select_db("fierm");
    $DeptID= $_GET["id"];
        $DeptName=$_GET["dp"];
        
       
    
?>
<body>
<form action="third.php" method="post">
    ID:
    <input id="id" type="text" readonly="true" value="<?php echo $_GET["id"];?>"/><br />
    
    
    Department:<input id="dp" type="text" value="<?php echo $_GET["dp"]?>"/><br />
    <br />
    &nbsp;<input id="Submit1" type="submit" value="submit" /><br />
 
</form>
 
</body>
</html>
 

Code: Select all

 
<?php
    $Conn=mysql_connect("localhost","fierm","13183");
    mysql_select_db("fierm");
 
    
    
        $new=$_GET["dp"];
    $id= $_GET["id"];
        
    $result=mysql_query("Update department SET DeptName ='$new' WHERE DeptID='$id'");
    if($result){
        echo "one record is updated";   
    }else{
        echo "cannot update the record";
    }
?>
 

Re: update query via form not working

Posted: Tue Apr 15, 2008 7:54 pm
by ianhull
You need to terminate the $_GET in your second page with ";"

Code: Select all

 
 
Department:<input id="dp" type="text" value="<?php echo $_GET["dp"]?>"/>
 
 

Re: update query via form not working

Posted: Wed Apr 16, 2008 3:21 am
by Phoenixheart
Since it's one line of PHP, I don't think we need the semi-colon there.
The reason is, you specified the form method as POST in second.php, and then try to retrieve its data via $_GET in third.php.
Btw, I would recommend escaping your data before calling that query :)

Re: update query via form not working

Posted: Wed Apr 16, 2008 4:40 pm
by gammaman
Thanks to both of you, I got it working, Thanks :D