how i can delete record from database using html forms

preetg 0 Tallied Votes 438 Views Share

hi......
i am using window xp.....apache server
m new in php
i inserted record in database through forms .now i m facing problem to delete the records from database in php code..... above code is not working properly...give me solution within 2 days....

[code=php]
<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","root");

//select which database you want to edit
mysql_select_db("mydb");

//If cmd has not been initialized
if(!isset($cmd))
{
   //display all the news
   $result = mysql_query("select * from stuinfo order by FirstName");

   //run the while loop that grabs all the news scripts
   while($r=mysql_fetch_array($result))
   {
      //grab the title and the ID of the news
      //$title=$r["title"];//take out the title
      $FirstName=$r["FirstName"];//take out the id

	 //make the title a link
      echo "<a href='delete.php cmd=delete&FirstName=$FirstName'></a>";
      echo "<br>";
    }
}
?>
[/code]

[code=html]
<HTML>
<HEAD>
 <TITLE>New Document</TITLE>
</HEAD>
<BODY>
<? php

if($_GET["cmd"]=="delete")
{
    $sql = "DELETE FROM stuinfo WHERE FirstName=$FirstName";
    $result = mysql_query($sql);
    echo "Row deleted!";
}
?>
</BODY>
</HTML>[/code]

Dani AI

Generated

Short diagnosis and safe plan

Original code has several issues: PHP short tags that may be disabled, a malformed delete link, a test of an undefined $cmd, deletion keyed on FirstName (not unique), SQL built without parameterization, and use of the old mysql_* API (removed in modern PHP). fixed link/quoting and correctly pointed out the unset variable check, but the thread needs a safer, modern pattern: a numeric primary key, form POST for destructive actions, prepared statements, and a confirmation/CSRF step.

Minimal, secure workflow to follow

  • Add an id INT PRIMARY KEY AUTO_INCREMENT to stuinfo and use it to identify rows.
  • List rows and render a small inline POST form per row with a hidden id field and a confirm step (javascript or a confirmation page).
  • Server-side: accept only POST, cast id to int, then run a parameterized DELETE (PDO or mysqli).
  • Turn on error reporting while developing and keep backups before running deletes.

Example (PDO) — listing + delete handler in one file

<?php
$pdo = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8mb4','root','root',[
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['id'])) {
  $id = (int) $_POST['id'];
  $stmt = $pdo->prepare('DELETE FROM stuinfo WHERE id = ?');
  $stmt->execute([$id]);
  echo 'Row deleted.';
}

foreach ($pdo->query('SELECT id, FirstName, LastName FROM stuinfo ORDER BY FirstName') as $row) {
  echo htmlspecialchars($row['FirstName']) . ' ';
  echo '<form method="post" style="display:inline">'
     . '<input type="hidden" name="id" value="'.(int)$row['id'].'">'
     . '<button type="submit" onclick="return confirm(\'Delete this record?\')">Delete</button></form><br>';
}

Troubleshooting notes

  • Replace all <? with <?php to avoid short-tag issues.
  • If migration to PDO/mysqli is not possible immediately, at least escape inputs and quote values—but migrate as soon as feasible.
  • Keep a dump/backup of the table before testing deletes.
CJesusSaves 6 Light Poster

Since I personally don't like mixing PHP and HTML you can try the following edited code if you so wish. Compare with yours to see what was changed.

Hope it helps.

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","root");

//select which database you want to edit
mysql_select_db("mydb");

//If cmd has not been initialized
if(!isset($cmd))
{
   //display all the news
   $result = mysql_query("select * from stuinfo order by FirstName");

   //run the while loop that grabs all the news scripts
   while($r=mysql_fetch_array($result))
   {
      //grab the title and the ID of the news
      $title=$r['title'];//take out the title
      $FirstName=$r['FirstName'];//take out the id
   ?>
	 //make the title a link
      <a href="delete.php?cmd=delete&FirstName=<?php echo $FirstName; ?>" title="<?php echo $title; ?>"><?php echo $title; ?></a>
      <br />
    <?php }
}
?>

<HTML>
<HEAD>
 <TITLE>New Document</TITLE>
</HEAD>
<BODY>
<? php

if($_GET['cmd']== "delete")
{
    $FirstName = $_GET['FirstName'];
    $sql = "DELETE FROM stuinfo WHERE FirstName='$FirstName'";
    $result = mysql_query($sql);
    echo "Row deleted!";
}
?>
</BODY>
</HTML>
didier_m 0 Newbie Poster

Hi,

to preetg => first of all, a please or a thank you can be more friendly

On line 11 you test on $cmd.
Where do you define $cmd? $cmd is at that time not set, you should better test on $_GET.
Hope this help
Didier

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.