0

I'm suppose to select any row from my database and delete the row but there is some problems with my coding.
Can anyone help me to amend my php code?
Thank you

<form method="post" action="deletestudent_p.php">
<?
// connect to MySQL database server
$connection = mysql_connect("localhost", "root", "")   or die("Could not connect to MySQL " .mysql_error() );
// select the specified database
$selection = mysql_select_db("studentwww")   or die("Unable to select database.  " .mysql_error());
$submit = $_POST["submit"];
if($submit=="Search")
{
    if(isset($_POST["MatricNo"]))
    { // create the query
      $Matrik = $_POST["MatricNo"];
      $sql = "SELECT * from addstudent where Matric ='$Matrik'";
      // execute query
      $result = mysql_query($sql) or die("SQL select statement failed");
      // retrieves a row data and returns it as an associative array
      if($row=mysql_fetch_array($result))
                   {  // display direct from array
                            echo "Matric No    : $row[Matric] <br>";
                       echo "Student Name : $row[StudentName]<br>";
                       echo "Address      : $row[Address]<br>";
                                     echo "<input type='hidden' name='MatricNo' value='$row[Matric]'>";
                                     echo "<input type ='submit' name='submit' value='confirm?'>";    }
     else { echo "No record found"; }
     }  
}
if($submit=="confirm?")
{    $Matric = $_POST["MatricNo"];
        $sql= "DELETE FROM Student WHERE Matric = '$Matric'";
        $result = mysql_query($sql);
        echo "<hr><h2>Record Deleted</h2><br>";
 }   ?></form></body></html>
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by hielo
0

I'm suppose to select any row from my database...

Where are you specifying which row/record to delete? I don't see you supplying an input field to supply the initial MatricNo to search for. For it to delete, first it needs to search, and for it to enter the "search" if clause, it needs to satisfy the following condition: if($submit=="Search") So, where is your:

<input type='text' name='MatricNo'/>
<input type='submit' name='submit' value='Search'/>

Instead of:

if(){
...
}

if(){
...
}

I suggest you use:

if(){
...//you already have this
}elseif(){
...//You already have this
}
else{
//here you put your initial search form along with the submit button with value='Search'
}

See code below:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
// connect to MySQL database server
$connection = mysql_connect("localhost", "root", "")   or die("Could not connect to MySQL " .mysql_error() );
// select the specified database
$selection = mysql_select_db("studentwww")   or die("Unable to select database.  " .mysql_error());
$submit = $_POST["submit"];
if($submit=="Search")
{
    if(isset($_POST["MatricNo"]))
    { // create the query
      $Matrik = $_POST["MatricNo"];
      $sql = "SELECT * from addstudent where Matric ='$Matrik'";
      // execute query
      $result = mysql_query($sql) or die("SQL select statement failed" . mysql_error() );
      // retrieves a row data and returns it as an associative array
      if($row=mysql_fetch_array($result))
                   {  // display direct from array
                            echo "Matric No    : $row[Matric] <br>";
                       echo "Student Name : $row[StudentName]<br>";
                       echo "Address      : $row[Address]<br>";
                                     echo "<input type='hidden' name='MatricNo' value='$row[Matric]'>";
                                     echo "<input type ='submit' name='submit' value='confirm?'>";    }
     else { echo "No record found"; }
     }  
}
elseif($submit=="confirm?")
{    $Matric = $_POST["MatricNo"];
        $sql= "DELETE FROM Student WHERE Matric = '$Matric'";
        $result = mysql_query($sql) or die(mysql_error());
        echo "<hr><h2>Record Deleted</h2><br>";
 }
 else
 {
 ?>
 <input type='text' name='MatricNo' value=''/>
 <input type='submit' name='submit' value='Search'/>
  <?php
 }
 ?>
 </form>
 </body>
 </html>

Edited by hielo: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.