Hi

Plz help i'm new i want to edit a record from the database using the html form that comes with the record on the field when i click edit and be able to delete it when i click a delete i want a pop up message to ask if i'm sure.
Below its what i have done

//Insert data into the database 

$query= "INSERT INTO Client(Name,Email, Comment) VALUES('".$Name."','".$Email."','".$Comment."')";
$_result=mysql_query($query);
if($result = mysql_query($query ,$db)) 
{
   
} else { 
echo "error: ".mysql_error();
// DISPLAY FORM IF FORM HAS NOT BEEN SUBMITTED 
}
}
if(isset($_POST['save'])){
}

//Select data from the database.
$result = mysql_query("SELECT Name,Email FROM Client ");

echo "<table border='1'>
<tr>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
    echo "<tr>";
    echo "<td>" . $row['Name'] . "</td>";
    echo "<td>" . $row['Email'] . "</td>";
    echo "<td><a href='Edit.php'> Edit</a> <a href='Delete.php'> Delete</a></td>";
    echo "</tr>";
  }  
    echo "</table>";
    

mysql_close($db);
?>

Can you at least put your code in the code tag plz?
;)

// Use the post variable function to collect the data.

  $Name    = $_POST['Name'];
  $Email   = $_POST['Email'];
  $Comment = $_POST['Comment'];

if(isset($_POST))
{
   $Email = @$_POST['Email'];
   $Name    = @$_POST['Name'];
   $Comment = @$_POST['Comment'];

}
else
{
$Email = '';
$Name = '';
$Comment ='';
}

//Connect to the database

$db = mysql_connect("localhost","root","");
if (!$db)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("mydatabase", $db);


if (isset($_POST['save'])) { 

//Insert data into the database 

$query= "INSERT INTO Client(Name,Email, Comment) VALUES('".$Name."','".$Email."','".$Comment."')";
$_result=mysql_query($query);
if($result = mysql_query($query ,$db)) 
{

} else { 
echo "error: ".mysql_error();
// DISPLAY FORM IF FORM HAS NOT BEEN SUBMITTED 
}
}
if(isset($_POST['save'])){
}

//Select data from the database.
$result = mysql_query("SELECT Name,Email FROM Client ");

echo "<table border='1'>
<tr>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
    echo "<tr>";
    echo "<td>" . $row['Name'] . "</td>";
    echo "<td>" . $row['Email'] . "</td>";
    echo "<td><a href='Edit.php'> Edit</a> <a href='Delete.php'> Delete</a></td>";
    echo "</tr>";
  }  
    echo "</table>";

mysql_close($db);
?>

Add.php

<body>
<h1 style="color:black"> ADD CLIENTS</h1>

<form action="Mainpage.php" method="post" >
Name:<br />
<input type="text" name="Name" size="30" /><br />
Email:<br />
<input type="text" name="Email" size="30" /><br />
Comment:<br />
<input type="text" name="Comment" rows="120" cols="120" /><br />
<input type='submit' name="save" value="save" />

</form>

<?php
if (isset($_POST['save'])) { 
// INSERT DATA FROM FORM ONCE THE FORM HAS BEEN SUBMITTED 
} else { 
// DISPLAY FORM IF FORM HAS NOT BEEN SUBMITTED 
}

?>

Edit.php

h1 style="color:black"> EDIT CLIENTS</h1>
<form action="Mainpage.php" method="post" >
Name:<br />
<input type="text" name="Name" size="30"  /><br />
Email:<br />
<input type="text" name="Email" size="30"  /><br />
Comment:<br />
<input type="text" name="Comment" rows="120" cols="120" /><br />

<input type='submit' name="save" value="save" />

</form>

<?php


if(isset($_POST))
{
   $Email = @$_POST['Email'];
}
else
{
$Email = '';
}


//Connect to the database

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("myDatabase", $con);
   $result = mysql_query("SELECT Name,Email,Comment FROM Client where Email ='".$Email."' ");




 if(isset($_POST['save']))    //determine whether the button is clicked
 {    
 $Name = $_POST['Name'];       //gather the form values  
 $Email = $_POST['Email'];    
 $Comment = $_POST['Comment'];   


   //update query  
 $update = mysql_query("UPDATE Client Name, Comment WHERE Email='$Email'") or (mysql_error()); }

     echo " record has been updated";


mysql_close($con);  
?>

</body>
</html>

So where does your error lies?

When i click delete nothing happens but when i delete an empty record it delete.Again i want to be able to edit the selected row.

on line 31 where you have delete.php. you must add the data to be deleted.

<a href="delete.php?userID=$row['id']&name=$row['name']">delete</a>

The you get the values the run your delete query in the delete.php.
eg:

$userId=$_GET['userID']
$name=$_GET['userID']

## then connect to your sql db and run your delete query.

;)

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.