...

View Full Version : help me regarding update



saranvarma
11-02-2011, 05:24 AM
<?
$con= mysql_connect("localhost","root","root");
if(!$con)
{
die("connection failed:".mysql_error());
}
mysql_select_db("saran",$con);
if($_POST['submit'] == 'Insert')
{
move_uploaded_file($_FILES["file"]["tmp_name"],"images/".$_FILES['file']['name']);
$query = "INSERT INTO `student` (`name` ,`marks`, `image`) VALUES ('".$_POST['name']."', '".$_POST['marks']."','".$_FILES['file']['name']."')";
mysql_query($query)or die(mysql_error());
header('Location: all.php');
}
if($_POST['submit'] == 'Update')
{
if($_FILES['file']['name']=""||$_FILES['file']['name']=null)
{
move_uploaded_file($_FILES["file"]["tmp_name"],"images/".$_FILES['file']['name']);
$query = "update student set name='".$_POST['name']."',marks=".$_POST['marks']." where id=".$_GET['id'] ;
mysql_query($query)or die(mysql_error());
header('Location: all.php');
}
else
{
$query = "update student set name='".$_POST['name']."',marks=".$_POST['marks']." ,image='".$_FILES['file']['name']."' where id=".$_GET['id'] ;
mysql_query($query)or die(mysql_error());
header('Location: all.php');
}

}
if($_GET['action'] == 'delete')
{
mysql_query("DELETE FROM `student` WHERE `id` = ".$_GET['id'])or die(mysql_error());
header('Location: all.php');
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<? if(($_GET["action"] == 'edit') || ($_GET['action'] == 'insert')){

if($_GET["action"] == 'edit'){
$sql = mysql_query("SELECT * FROM student WHERE id =".$_GET['id']);
$sql_f = mysql_fetch_array($sql);
}
?>
<form action="" method="post" name="form2" enctype="multipart/form-data">
<table align="center">
<tr>
<td>name</td>
<td><input type="text" name="name" id="name" value="<?=$sql_f["name"]?>" /></td>
</tr>
<tr>
<td>marks</td>
<td><input type="text" name="marks" id="marks" value="<?=$sql_f["marks"]?>" /></td>

</tr>
<? if($_GET["action"] == 'edit'){ ?>
<tr>
<td colspan="2"><input type="file" id="file1" name="file1"/> </td>
<td><img src="images/<?=$sql_f["image"]?>" height="20" width="20"/></td>
<td colspan="2"><input type="submit" name="submit" value="Update" /></td>
</tr>
<? } ?>
<? if($_GET["action"] == 'insert'){ ?>
<tr>
<td colspan="2"><input type="file" id="file" name="file"></td>
<td colspan="2"><input type="submit" name="submit" value="Insert" /></td>
</tr>
<? } ?>
</table>
</form>
<? } else {

$result = mysql_query("select * from student order by Id DESC");
?>
<form method="post" name="form1" action="">
<table align="center" border="2">
<tr>
<td>Id</td>
<td>name</td>
<td>marks</td>
<td>image</td>
<td>Action</td>
</tr>
<?
while($rec = mysql_fetch_array($result)){
?>
<tr>
<td><?=$rec["id"]?></td>
<td><?=$rec["name"]?></td>
<td><?=$rec["marks"]?></td>
<td><img src="images/<?=$rec[image]?>" height="20" width="20"/></td>
<td><a href="all.php?action=edit&id=<?=$rec["id"]?>" >Edit</a> &nbsp <a href="all.php?action=delete&id=<?=$rec["id"]?>" >delete</a></td>
</tr>
<?
} ?>
<tr>
<td colspan="2"><a href="all.php?action=insert">Insert</a></td>
</tr>
<? }
?>
</table>
</form>
</body>
</html>
this is my code to insert , update, delete data im db. but when i update image after clicking edit old image erasses but new image is not displayed. plz help me

Old Pedant
11-02-2011, 05:56 AM
So what does your DEBUG show you?

What are the actual queries you are using, after substituting in values?

saranvarma
11-02-2011, 06:05 AM
So what does your DEBUG show you?

What are the actual queries you are using, after substituting in values?
there is no errors bro but image not updated

Old Pedant
11-02-2011, 07:56 AM
I didn't say there were any errors. I said what does your DEBUG show you?

FOr example:


...
$query = "update student set name='".$_POST['name']."',marks=".$_POST['marks']." ,image='".$_FILES['file']['name']."' where id=".$_GET['id'] ;
echo "<hr>DEBUG: " . $query . "<hr>\n";
mysql_query($query)or die(mysql_error());
// suppress the change to location while debugging
// header('Location: all.php')
...

I am guessing that maybe $_FILES['file']['name'] is not what it should be. So this will find out if I'm right. Or do the debugging however you choose. Use a debugger to step through the code, if you prefer. But do DEBUG.

BubikolRamios
11-02-2011, 03:23 PM
Allso you might click refresh a couple of times in browser & clear cache.
If all the rest is working OK.

saranvarma
11-03-2011, 02:48 PM
thank u its working



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum