0

guys i am new to php and have a trouble about validation of a text box. i have to create a student database and its interface capable of insert/delete/view/update option. say now am trying to delete a student from the database i should enter the register number to delete. in this case, if i enter a register number not in the database then too the file redirects to success page. i have made validation for non-zero entry of register number. now my problem is if i want to delete a student who is not in the database it should be redirected to a error page. this should be done using php, javascript and mysql. how am to do this?

here is my delete student code:

<!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>Students Database</title>
<meta author="" content="">
<link rel="stylesheet" type="text/css" href="view.css" media="all">
</head>
<body id="main_body" >
    <img id="top" src="top.png" alt="">
    <div id="form_container">
    <h1><a>Students Database</a></h1>
    <form name="admin4" class="appnitro"  method="post" action="delete1.php">
    <div class="form_description">
    <center><h2>Students Database</h2></center>
    <p><center><font size='3'>
    <?php
    $con=mysqli_connect("localhost","admin","123456","mop");
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con,"SELECT * FROM student");
    echo "<table border='1'>
    <tr>
    <th>Register No</th>
    <th>Name &nbsp &nbsp &nbsp </th>
    <th>Department &nbsp </th>
    <th>Class &nbsp </th>
    </tr>";
    while($row = mysqli_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['RegNo'] . "</td>";
    echo "<td>" . $row['Name'] . "</td>";
    echo "<td>" . $row['Department'] . "</td>";
    echo "<td>" . $row['Class'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";
    mysqli_close($con);
    ?>
    </center></font></p>
    </div>  
    <b>Enter Register Number <font color='red'>*</font> </b> <input type="text" name="regno"><p>
    <ul >
    <center><li class="buttons">
    <input type="hidden" name="form_id" value="768845" />
    <input id="saveForm" class="button_text" type="submit" name="submit" value="Delete" /></center>
    </li>
    </ul>
    </form> 
    </div>
    <img id="bottom" src="bottom.png" alt="">
</body>
</html>

the above is the interface code and here is the processing part code:

<?php
$con=mysqli_connect("localhost","admin","123456","mop");
if ($_POST['regno'] == '')
    {
    header("location:admin4_2.php");
    }
    else
    {
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$sql1="DELETE from student where regno = ".intval($_POST["regno"]);
if (!mysqli_query($con,$sql1))
  {
  die('Error: ' . mysqli_error($con));
  }
  else
  {
    header("location:admin6_1.php");
  }
  }
mysqli_close($con);
?>

as u see i have done it to check for non-zero entry. is it possible to check with table for valid register number? can anyone explain me how to do in detail with the code because am new to php and this is my first project. i dont mind using javascript or php for it. if javascript is being used can anyone also show me the code of javascript too??

8
  • This is baffling >>> "now my problem is if i want to delete a student who is not in the database it should throw up a error page" Commented Jan 26, 2014 at 4:27
  • If you want to check if a user exists, you can use something like this if(mysqli_num_rows($query) > 0){ echo "User exists"; } Commented Jan 26, 2014 at 4:29
  • sorry guys!! i mean i should be redirected to a page where it says inavlid register number!! Commented Jan 26, 2014 at 10:22
  • Isn't that what you have now with if ($_POST['regno'] == '') ? Commented Jan 26, 2014 at 13:50
  • i have validated it for non zero entry sir i want to now check if the entered register number is in table or not. if not present i wanna be redirected to a error page else to a success page Commented Jan 26, 2014 at 14:38

1 Answer 1

1

If you base yourself on the following, it will check if a registration number exists in your database.

This is a basic example. Modify to suit your needs.

HTML form

<form method="post" action="if_exists.php">
<b>Enter Register Number <font color='red'>*</font> </b> <input type="text" name="regno"><p>
<ul>
<center><li class="buttons">
<input id="saveForm" class="button_text" type="submit" name="submit" value="Check if Exists" /></center>
</li>
</ul>
</form>

PHP (if_exists.php)

<?php
$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_PASS = "xxx";
$DB_USER = "xxx";

$db = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($db->connect_errno > 0) {
  die('Connection failed [' . $db->connect_error . ']');
}

$regno=mysqli_real_escape_string($db,$_POST['regno']);

// $query = $db->query("SELECT regno FROM student WHERE regno='$regno'");
$query = $db->query("SELECT * FROM student WHERE regno='$regno'");

if(mysqli_num_rows($query) > 0){
    echo "Registration number already exists.";
}else{
// echo "Sorry";
header("Location: redirection_page.php");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.