error to appear on the same page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kalp1200
Forum Newbie
Posts: 19
Joined: Tue Aug 26, 2008 8:57 pm

error to appear on the same page

Post by kalp1200 »

Hi

I have found the codes to detect duplicate primary key, but is there a way for me to make the error appear in the same page, using javascript or the echo value. right now, the form gets an error but it appears on the next page.

Code: Select all

 
<?php 
include('connect.php');
 
//$Admin_ID=$_POST['Admin_IID']; 
$adminname=$_POST['admin_name']; 
$username=$_POST['username']; 
$password= md5($_POST['password']);
$email_address=$_POST['email_address'];
$department=$_POST['dept_name']; 
$URL="/testing/regsuccess-admin.php";
error_reporting(0);
 
if (isset($_POST['submit'])) 
{ 
    if (!get_magic_quotes_gpc()) 
    {
        $_POST['Admin_ID'] = addslashes($_POST['Admin_ID']);
    }
 
    $Admin_ID = $_POST['Admin_ID'];
    $check = mysql_query("SELECT Admin_ID FROM administrator WHERE Admin_ID = '$Admin_ID'") or die(mysql_error());
    $check2 = mysql_num_rows($check);
        if ($check2 != 0) 
        {
            echo 'Username already exists'; 
        }
 
$insert = "INSERT INTO administrator(Admin_ID, adminname, username, password, email_address, department) VALUES ('$Admin_ID','$adminname','$username','$password','$email_address','$department')"; 
}
 
if (!mysql_query($insert))
  {
    die('Error: ' . mysql_error());
  }
 
header ("Location: $URL");
 
 
?>
 


and I get the following error

Username already existsError: Duplicate entry 'ss' for key 'PRIMARY'

I want the username already exist to appear on the same page, and do not want the Error: Duplicate entry 'ss' for key 'PRIMARY' to appear at all.

Is there a way.
Last edited by Benjamin on Mon Apr 27, 2009 8:23 pm, edited 1 time in total.
Reason: Added code tags.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: error to appear on the same page

Post by Christopher »

You should use die() at all. And you should only redirect if the form is successfully submitted. Try changing the code.
(#10850)
Post Reply