<html>
<head>
<title>Eastern Mediterranean University-Student's ID</title>
</head>
<body>
<h3>Edit Student</h3>
<?php
include("sonuc.php");
if(!isset($_POST['edit'])) {
	$num = $_GET['num'];
	$sql = "select * from students where num='$num'";
	$result = mysql_query($sql) or die('\"'.$sql.'\" Query failed:'.mysql_error());
	$row = mysql_fetch_array($result, MYSQL_BOTH);
	$id = $row['id'];
	$name = $row['name'];
	$surname = $row['surname'];
	$department = $row['department'];
	$cpa = $row['cpa'];
}
else {
	$num = $_POST['num'];
	$id = $_POST['id'];
	$name = $_POST['name'];
	$surname = $_POST['surname'];
	$department = $_POST['department'];
	$cpa = $_POST['cpa'];
	$sql = "update students set id='$id', name='$name', surname='$surname', department='$department', cpa='$cpa' where num='$num'";
	$result = mysql_query($sql) or die ('\"'.$sql.'\" Query failled: '.mysql_error());
	echo"<br><br>The student has been succesfully updated.<br><br>\n";
}

?>

<form method="post" action="edit.php" >
<input type="hidden" name="num" value="<?php echo $num ?>">
	<b>ID:</b><br>
	<input type="text" name="id" value="<?php echo $id ?>" size="10">
	<br>
	<b>Name:</b><br>
	<input type="text" name="name" value="<?php echo $name ?>" size="10">
	<br>
	<b>Surname:</b><br>
	<input type="text" name="surname" value="<?php echo $surname ?>" size="10">
	<br>
	<b>Department:</b><br>
	<input type="text" name="department" value="<?php echo $department ?>" size="10">
	<br>
	<br>
	<b>GPA:</b><br>
	<input type="text" name="cpa" value="<?php echo $cpa ?>" size="10">
	<br>
 	<br>
	<input type="submit" name="edit" value="Update information">
</form>
</body>
</html>

it doesn't working.. what is my problem?
thank you for helps :)

Member Avatar for rajarajan2017

Also use post here:

$num = $_GET['num'];

Echo your values and confirm whether your values are received or not

I tried it but it doest working :(
my promlem is that :
\"update students set id='', name='', surname='', department='', cpa='' where num=' ?'\" Query failled: Access denied for user 'www-data'@'localhost' (using password: NO)

Member Avatar for rajarajan2017

It seems to be a security issue, did you connected your database before?

Thanks rajarafan.. I found the problem.. I cant connect database :S

Member Avatar for rajarajan2017

Cool! Please mark the thread as solved if it is solved!

I can't connect to database.. :S

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

<?php



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


if(!isset($_POST['edit'])) {
	$num = $_GET['num'];
	$query = "select * from students where num='$num'";
	//$result = mysql_query($sql) or die('\"'.$sql.'\" Query failed:'.mysql_error());
	$row = mysql_fetch_array($result, MYSQL_BOTH);
	$result = $db->query($query);
	//$row = $result->fetch_assoc();
	$id = $row['id'];
	$name = $row['name'];
	$surname = $row['surname'];
	$department = $row['department'];
	$cpa = $row['cpa'];
}
else {
	$num = $_GET['num'];
	$id = $_POST['id'];
	$name = $_POST['name'];
	$surname = $_POST['surname'];
	$department = $_POST['department'];
	$cpa = $_POST['cpa'];


	$query = "update students set id='$id', name='$name', surname='$surname', department='$department', cpa='$cpa' where num='$num'";
	$result = $db->query($query);

	//$result = mysql_query($sql) or die ('\"'.$sql.'\" Query failled: '.mysql_error());
	//echo"<br><br>The student has been succesfully updated.<br><br>\n";
}


//$num_results=$result->num_rows;

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='echo $id' size='10'>
	<br>	
	<b>Name:</b><br>
	<input type='text' name='name' value='$name' size='10'>
	<br>
	<b>Surname:</b><br>
	<input type='text' name='surname' value='$surname' size='10'>
	<br>
	<b>Department:</b><br>
	<input type='text' name='department' value='$department' size='10'>
	<br>
	<b>GPA:</b><br>
	<input type='text' name='cpa' value='$cpa' size='10'>
	<br>
	<br>
	<input type='submit' name='update' value='Save'><br>
	</form></td>
	</tr><br />\n";
?>
</body>
</html>

the output is empty.. what can be problem on my code???
I think problem is in this part:

value='$name'

but I couldnt the correct code for this statement.
can you help me pleaseeee!!! =)

Member Avatar for rajarajan2017
value='echo $id'

remove the echo. Is that your database connected or not?

I connected to database and I did value='$id' but the table is empty now

Member Avatar for rajarajan2017
if(!isset($_POST['edit'])) {

'edit' now it should be 'update', you changed your submit name to update.

Also change your name='name' to something like name='fname'. May be that is a keyword. Get back soon.

I solved the problem :)

<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', '*****', '****');
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();

if($_POST['edit']) {
$name=$_POST['name'];
$surname=$_POST['surname'];
$id=$_POST['id'];
$department=$_POST['department'];
$cpa=$_POST['cpa'];
$query="update students set id='$id', name='$name', surname='$surname', department='$department', cpa='$cpa' where num='$num'";
$result = $db->query($query);

}

echo "<tr>

<form method='post' action='edit.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>
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.