i m newbie in web development plz help me to sort out the problem..
the problem is: in edit.php when control enters in if condition it never goes to else when i press the edit button.

i want to get id from view.php and display category name in edit.php textbox so i can change the category name by pressing edit button.

view.php

<?
		require("Datab.class.php");
		$db = new Datab("localhost", "root", "intaha", "subc");
		$db->connect();
	
		$result = $db->query("select * from category");
		
		?>
		<table border="1" cellpadding="0">
        <? 

		if(mysql_num_rows($result)){
			
			while($rows = mysql_fetch_row($result)){ ?>
                <tr>
                <td><? echo $rows[0]; ?></td> 
                <td><? echo $rows[1]; ?></td>
                <td><a href="edit.php?id=<?=$rows[0]?>">edit</a></td>                
                <td><a href="delete.php?id=<?=$rows[0]?>">delete</a></td>
          
                           
				</tr>
            <?	
			}
		}
		?> </table>

edit.php

<html>
<head>
<title></title>
</head>
<body>


<?php
		require("Datab.class.php");
		$db = new Datab("localhost", "root", "intaha", "subc");
		$db->connect();
		$id = $_GET['id'];
		echo "hello this is upper.... $id";			
		
		$result = $db->query("select name from category where category_id = $id");
		
		$name = "";
		if(mysql_num_rows($result) > 0){
			while($rows = mysql_fetch_row($result)){
				$name = $rows[0];
			}
		}
		
	if(!isset($_POST['submit'])){
		echo "<br> in !isset condition..... <br> ";
		?>	
			<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
			<input type="text" name="cat_name"  value= "<? echo $name; ?>" />
			<input type="submit" name="submit" value="edit" />
			</form>
		<?
	}else{
		echo "<br> in else condition..... <br> ";
		$uname= $_POST['cat_name'];
		echo "name = " .$uname;
		echo "in id = " . $id . "  ";
		$result = $db->query("update category SET name = '$uname' where category_id = $id");
		echo "Category name changed ";
	}
		?>
        

</body>
</html>

Dani AI

Generated

correctly pointed out that GET query strings are not the same as POSTed form data, and is right to stress using POST for updates. A reliable pattern is: on the initial GET load the row (using the id from the query string) and render a form that includes the id as a hidden field; on submission detect the request method (not just the submit-button name), validate input, run a parameterized UPDATE, then redirect (POST/Redirect/GET). This avoids losing the id on submit, removes brittle checks like isset($_POST['submit']), and improves security.

Example skeleton (use PDO or mysqli prepared statements, not mysql_* functions):

$pdo = new PDO('mysql:host=localhost;dbname=subc;charset=utf8mb4','user','pass',[
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $id   = $_POST['id'];
    $name = trim($_POST['cat_name']);
    $stmt = $pdo->prepare('UPDATE category SET name = ? WHERE category_id = ?');
    $stmt->execute([$name, $id]);
    header('Location: view.php'); // PRG pattern
    exit;
} else {
    $id = $_GET['id'] ?? null;
    $stmt = $pdo->prepare('SELECT name FROM category WHERE category_id = ?');
    $stmt->execute([$id]);
    $name = $stmt->fetchColumn();
}

Practical checks and tips:

  • Ensure the form uses method="post" and that the hidden id field is present so the id survives the POST. Echo user values into inputs with htmlspecialchars($value, ENT_QUOTES) to avoid HTML/XSS issues.
  • Use $_SERVER['REQUEST_METHOD'] === 'POST' to detect submission instead of relying on a button name (button names can be missing in some submit scenarios).
  • Switch away from deprecated mysql_* functions to PDO or mysqli prepared statements for safety and future compatibility (see PDO prepared statements and htmlspecialchars in the PHP manual: PDO prepared statements and htmlspecialchars).
  • If the form still behaves oddly, inspect the browser Network tab to confirm a POST request is sent and view the raw POST payload.

Recommended Answers

All 2 Replies

You are not posting the values from view.php, you are just sending through query string.

so in edit.php you need to take the data something like $_REQUEST

You are not submitting the data in a form (So why are you checking in if condition as $_POST)

I suggest you to send one more parameter kin view.php, such as

<a href="edit.php?id=<?=$rows[0]?>&action=edit">edit</a>

<a href="delete.php?id=<?=$rows[0]?>&action=delete">delete</a>

and in edit.php
check for $_REQUEST == 'edit'

It is as saiprem wrote,
the semantics of php do not consider, as people do in spoken language, that post get request are 'similar enough' to be interchangeable
$_post does not show the data sent in the query string, and is harder to fake
if the data being edited is subject to attack,
privacy issues,
security needs,
it is definitely better to $_post the data from one page to another,

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.