Hi, i have implemented a basic searchwhich does display the results, i want to add an edit function button, so that when the user click edit the results are displayed in input boxes so that the content can be edited.
Her is what iv got so far, im new to php:

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "root", "root")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$myq=$_POST['myq'];



if (isset($_POST['myq'])) {
    $myq = mysql_real_escape_string($_POST['myq']);
    $result = mysql_query("SELECT * FROM members WHERE Firstname LIKE '%$myq' OR Surname LIKE '%$myq'");

    while ($row = mysql_fetch_assoc($result)) {

echo "<p>You searched for: &quot;" . $myq . "&quot;</p>";


echo "<table border=\"1\" align=\"center\">";

echo "<tr><th>First Name</th>";
		echo "<td>";
        echo $row['Firstname'];
echo "</td>";

echo "<tr><th>Surname</th>";
		echo "<td>";
	    echo $row['Surname'];
echo "</td>";

echo "<tr><th>Username</th>";
		echo "<td>";
        echo $row['username'];
echo "</td>";

echo "<tr><th>Password</th>";
		echo "<td>";
        echo $row['password'];
echo "</td>";

echo "<tr><th>Email</th>";
		echo "<td>";
    	echo $row['email'];
echo "</td>";

echo "<tr><th>Level</th>";
		echo "<td>";
    	echo $row['level'];
echo "</td>";



    }
}

if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

if (mysql_num_rows($result) == 0) {
    echo "No records found";
    exit;
}


?>

Recommended Answers

All 3 Replies

When the user clicks on Edit button, pass that user's id in a hidden variable, query the table, fetch the record and print respective values in the textboxes. Is there any particular error ?

When the user clicks on Edit button, pass that user's id in a hidden variable, query the table, fetch the record and print respective values in the textboxes. Is there any particular error ?

Hi thanks for your reply, how would i pass this variable from this page to the edit page, im new to php.
Thanks.

Here is a simple example.

//this is test1.php
<html>
<body>
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("test");
$query = "select * from orders";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
	echo "<form method='post' action='test2.php'>";
	echo "<input type='hidden' name='orderid' value='".$row['order_id']."'>";
	echo $row['order_id'];
	echo "<input type='submit' name='submit' value='Edit'>";
	echo "</form>";
}
?>
</body>
</html>

In this script, it lists all the order_ids from order table. You can edit an order by clicking on Edit button.

//this is test2.php
<?php
	$con = mysql_connect("localhost","root","");
	mysql_select_db("test");
	$orderid = mysql_real_escape_string($_POST['orderid']);
	$query = "select * from orders where order_id = '$orderid'";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	echo "<form method='post' action='test3.php'>";
	echo "<input type='hidden' name='orderid' value=".$row['orderid'].">";
	echo "<input type='text' name='amount' value=".$row['amount'].">";
	echo "<input type='submit' name=submit value=update>";
	echo "</form>";
?>

order_id is passed from test1.php to this script. We get the amount of that particular order_id in this script,which is displayed in the textbox. The user can change the amount value and click on Update.

//this is test3.php
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("test");
$amount = mysql_real_escape_string($_POST['amount']);
$orderid = mysql_real_escape_string($_POST['orderid']);
$query = "update orders set amount = $amount where orderid = '$orderid'";
mysql_query($query);
header("location: test1.php");
?>

In this script, orderid and the edited amount is passed on.. Then the amount is updated in the table for that particular orderid and then redirected back to test1.php . I hope thats clear.. :)

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.