I have the following php code that seeks to
a) ask a user to enter a school pin b) Look up the database table 'teachers' and then overwrite the new value (entered into the textbox) over the old value of the school pin in the database.
Currently the issue is that it does ADD a new row to the table with the new entered pin, but I want it to overwite the existing value for that particular teacher (basically overwrite the existing value).
The php handler code is below:
if (isset($_POST["addpin1"]) && !empty($_POST['schoolpin1']) && $_SESSION['teacher']) {
$school_pin = mysqli_real_escape_string($con,$_POST['schoolpin1']);
$email = $_SESSION['email'];
$username = $_SESSION['username'];
$pass = $_SESSION['pass'];
$check_duplicate = mysqli_query($con,"SELECT school_pin FROM teachers WHERE school_pin='$school_pin' ");
if(@mysqli_num_rows($check_duplicate) == 0){
$query = mysqli_query($con,"INSERT INTO teachers (school_pin,email,username,pass)
VALUES('$school_pin','$email','$username','$pass')");
if($query){
header("Location: teachers.php");
}
}
}
The HTML form code:
<form action="" method="post">
<input type="text" name="schoolpin1" id="schoolpin1">
<input type="submit" class="btn btn-success" name="addpin1" value="CHANGE PIN">
</form>
Database Structure (tbl: teachers) Fields shown below
id
email
school_pin
username
pass
The page on which this is occuring has a teacher logged in, so their username is displayed on the screen.
UPDATE: As per one of the answers regarding starting session. This has been done
<?php
require_once("scripts/connect_db.php");
session_start();
if(!isset($_SESSION['teacher'])) header('Location: teacher_login.php');;
$result="";
if (isset($_POST["logout"])) {
session_destroy();
header('Location:index.php');
}
$err = "";