Hi

I'm new in php.I have created a Form to insert data into the database its working fine. The other is to show all records from the database its too fine. I don't know where to start, Want i want is to edit one record at a time when i click edit I want the form to appear with Name,Email and Attachments filled with the data of that record and be able to edit it and save. Here I update my codding briefly.

complete.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=utf-8" />
<title>My First Project in PHP</title>
</head>

<body bgcolor=tan>
<h1><font color="firebrick">PHP MySQL </font></h1>

<form action="success.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="box" value="100000">
<table width="500" border="1">
<tr><td><b><label for='name'>Name :</b></label></td> <td><input type="text" name="Uname" size="20" /></td></tr>
<tr><td><b><label for='mail'>Email :</b></label></td><td><input type="text" name="mail" size="20" /></td></tr>
 <tr><td><b> <label for='Updated_file'>Attachements:</b></label></td> <td>  <input type="file" name="Updated_file" id="Updated_file" /></td></tr>
<tr><td  colspan="2" align="center"><input type="Submit" value="Submit" name="Submit" id="Submit" onClick="return validate();"> <input type="reset" value="Reset" name="reset"> </td></tr>
</table>
</form>
<p>
  <a href="output.php">See all Attachment files</a>
  </p></body>
</html>

**success.php**

$con = mysql_connect("localhost", "root");    
mysql_select_db("connect_db", $con);

/*$sql = "CREATE TABLE upload1 (
id INT NOT NULL AUTO_INCREMENT,
User_Name VARCHAR(30) NOT NULL,
Email VARCHAR(30) NOT NULL,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
content MEDIUMBLOB NOT NULL,
PRIMARY KEY(id)
)";
mysql_query($sql,$con);*/


$Uname = $_POST['Uname'];
$mail = $_POST['mail'];
error_reporting(0);


$fileName = $fileName . basename ($_FILES['Updated_file']['name']);
$tmpName  = move_uploaded_file($_FILES['Updated_file']['tmp_name'], $fileName);
$fileSize = $_FILES['Updated_file']['size'];
$fileType = $_FILES['Updated_file']['type'];


$query = "INSERT INTO upload1 (User_Name, Email, name, size, type, content) VALUES ('".$Uname."', '".$mail."', '".$fileName."', '".$fileSize."', '".$fileType."', '".$content."' )";
mysql_query($query) or die("Error, query failed");

  if($query)
        {
          echo 'Success! Your file was successfully added in database!';
        }

         else
         {
             echo 'Error! Failed to insert the file'. "<pre>{$dbLink->error}</pre>";
         }


echo '<p>Click <a href="complete.php">here</a> to go back</p>';
mysql_close($con);

?>

output.php

<?php

$con = mysql_connect("localhost", "root");    
mysql_select_db("connect_db", $con);
error_reporting(0);

$fileName = $fileName . basename ($_FILES['Updated_file']['name']);
$tmpName  = move_uploaded_file($_FILES['Updated_file']['tmp_name'], $fileName);
$fileSize = $_FILES['Updated_file']['size'];
$fileType = $_FILES['Updated_file']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

$result = mysql_query("SELECT * FROM upload1");
while($row = mysql_fetch_array($result))

{

    echo "<font color='red'>Name :</font> ".$row['User_Name'].",<br >";

    echo "<font color='blue'>Email :</font>".$row['Email'].",<br >";

    echo "<font color='green'>Attachements :</font>"."<a href='{$row['name']}' > 'Download'</a>".".<br />";

    echo "<br/>";
    }
    mysql_close($con);


?>

Plz help me.

In your output.php just make another column for edit and delete in the while condition as follows...

while($row = mysql_fetch_array($result))

{

echo "<font color='red'>Name :</font> ".$row.",<br >";

echo "<font color='blue'>Email :</font>".$row.",<br >";

echo "<font color='green'>Attachements :</font>"."<a href='{$row}' > 'Download'</a>".".<br />";

$mail=$row;

echo "<a href='edit.php?mail_id=$mail'> Edit </a>"."<br>";

echo "<a href='del.php?mail_id=$mail'> Delete </a>"."<br>";

echo "<br/>";
}

By this code, you should have two different pages for edit record(edit.php) and delete record (del.php)... There you get this mail_id value by Post method and retrieve the datas which corresponds to the mail id.. Bcoz, mail id will be unique value.. By this, you can populate in edit.php and edit and save the values as your wish.. Additionally in del.php, you can delete record which is having the mail id you selected... Use table format to display better results..

Hope you understand this.. If still having probs, pls let me know...

Plz let me give the coding to edit.php

Hmmm... Here i give you the sample code... Use it for your application as briefly by understanding...

<?php
$mailid=$_REQUEST['mail_id'];
$conn=mysql_connect("hostname","user","pass") or die(mysql_error());
$db=mysql_select_db("database") or die(mysql_error());

$sql=mysql_query("select * from tablename where Email='$mailid'");  // This will get all values of corresponding mail id..
$ret=mysql_fetch_array($sql); 
echo "<form action='' name='myform' method='post'>";
// Here your code to show your selected values from database in text boxes...
echo "Name : <input type='text' name='name' value='".$ret['Name']."'>";
echo "Mail id : <input type='text' name='mail_id' value='".$ret['Email']."'>";

// Likewise, you just display all needed values... Then do what changes you need to edit and have a submit button to update...
echo "<input type='submit' value='Update'>";
echo "</form>";
......

// Edited values to be get from above form like this...
$uname=$_REQUEST['name'];
$mail=$_REQUEST['mail_id'];
if(isset($mail))
{
$sql1=mysql_query("update tablename set Name='$uname', Email='$mail' where Email='$mailid'");
if($sql1)
{
echo "Updated successfully..");
}
?>

If any probs, pls let me know....

Ho.. Nice and thanks.

Hmmm.. I hope you will do... If any probs means, let me know.. I am always with u...

Hey i did well in edit and save. How can change or modify the attachments ya. I am trying to do that now.

Nothing but how did you upload in your database... Just update with new file.. It will overwrite the path in the specified field of the table....

S. I did. Thanks a lot..

Hmmm.. Fine... Keep in touch... Have fun.. Enjoy...

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.