Hi everyone, can you help me how to add update form in this code?

<html>
<head>
<title>Eastern Mediterranean University-Student's ID</title>
</head>
<body>
<h3>Edit Student</h3>

<?php
$num=$_POST['num'];


@ $db = new mysqli('localhost', 'root', 'ilyas1987', 'dau');
if (mysqli_connect_errno()) {
	echo 'Error: Could not connect to database. Please try again.';
	exit;
}

$query = "select * from students where num='$num'";
$result = $db->query($query);
$row = $result->fetch_assoc();
echo $query;

echo "<tr>

<form method='post' action='sonuc.php' >
<input type='hidden' name='num' value='$num'>
	<b>ID:</b><br>
	<input type='text' name='id' value='".$row['id']."' size='10'>
	<br>	
	<b>Name:</b><br>
	<input type='text' name='name' value='".$row['name']."' size='10'>
	<br>
	<b>Surname:</b><br>
	<input type='text' name='surname' value='".$row['surname']."' size='10'>
	<br>
	<b>Department:</b><br>
	<input type='text' name='department' value='".$row['department']."' size='10'>
	<br>
	<b>GPA:</b><br>
	<input type='text' name='cpa' value='".$row['cpa']."' size='10'>
	<br>
	<br>
	<input type='submit' name='edit' value='Save'><br>
	</form>
	</tr><br />\n";
$result->free();
$db->close();
?>
</body>
</html>

Recommended Answers

All 2 Replies

Its simple as you do code for insert same thing should be there for update.
Like insert query here you will have update query.

<html>
<head>
<title>Eastern Mediterranean University-Student`s ID</title>
</head>
<body>
<h3>Edit Student</h3>

<?php
$num=$_POST['num'];


@ $db = new mysqli('localhost', 'root', 'ilyas1987', 'dau');
if (mysqli_connect_errno()) {
	echo 'Error: Could not connect to database. Please try again.';
	exit;
}

//-------------- update is clicked ----------------
if(isset($_REQUEST['edit']))
{
	$id = $_REQUEST['id'];
	$name = $_REQUEST['name'];
	$surname = $_REQUEST['surname'];
	$department = $_REQUEST['department'];
	$cpa = $_REQUEST['cpa'];
	
	$q="update students set 
		name = '".$name."',
		surname = '".$surname."',
		department = '".$department."',
		cpa = '".$cpa."'
		where id = ".$id;
	mysql_query($q);
	
	header('Location:succupdate.php');
	exit;
	
}

$query = "select * from students where num='$num'";
$result = $db->query($query);
$row = $result->fetch_assoc();
echo $query;

echo "<tr>

<form method='post' action='sonuc.php' >
<input type='hidden' name='num' value='$num'>
	<b>ID:</b><br>
	<input type='text' name='id' value='".$row['id']."' size='10'>
	<br>	
	<b>Name:</b><br>
	<input type='text' name='name' value='".$row['name']."' size='10'>
	<br>
	<b>Surname:</b><br>
	<input type='text' name='surname' value='".$row['surname']."' size='10'>
	<br>
	<b>Department:</b><br>
	<input type='text' name='department' value='".$row['department']."' size='10'>
	<br>
	<b>GPA:</b><br>
	<input type='text' name='cpa' value='".$row['cpa']."' size='10'>
	<br>
	<br>
	<input type='submit' name='edit' value='Save'><br>
	</form>
	</tr><br />\n";
$result->free();
$db->close();
?>
</body>
</html>

it is working now :)
Thank you my friend :)

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.