Jump to content

Recommended Posts

//database
create table mydata (
	id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
	fname varchar(20),	
    phoneno int(12) NOT NULL
    /*......*/
);
//class my data php

<?php

include('connect.php');
class InsertMydata {
    public function insertnow($fname, $phoneno) {
        $connect = new Connect;
        $insrt = $db -> prepare('INSERT INTO mydata (fname, phoneno) VALUES (?,?)');
		$insrt -> execute(array($fname, $phoneno));		
	}
}
?>


//insernow validate form

<?php
include('../classs/mydata.php');
//Declare data and error arrays
$errors = [];
$mydara = [];
if(!preg_match('/^[a-zA-Z]{4,15}$/', $_POST['fname'])) {
	$errors['fname'] = 'Enter full name!';
}
//this block not working even the phone exist
$connect = new Connect;
$phoneno = $_POST['phoneno'];
$checkiexist = $connect -> prepare('SELECT * FROM mydata WHERE phoneno = ?');
$checkiexist -> execute([$phoneno]);
if($checkiexist->rowCount() > 0) {
      $errors['phonenoexist'] = 'Try another phone number!';
 }
if(!empty($errors)){
      $data['success'] = false;
      $data['errors'] = $errors;
}else{
      $data['success'] = true;
      $data['message'] = 'success message!';  
      $mydata = new InsertMydata;
      $mydata -> insertnow($fname, $phoneno);
}
echo json_encode($data);

?>

//my ajax

$("#insertbtn").click( function(e) {
     var fname = $('#fname').val(),
         phoneno = $('#phoneno').val(),
           
       $.ajax({
           url: 'insertnow.php',
           type: 'POST',
           data: {fname:fname, phoneno:phoneno},
           dataType: "JSON",
           encode: true,
        }).done( function (data) {
         
         if (data.success == false) {
           if (data.errors.fname) {
                 $('#fname').append('<p class="text-danger">' + data.errors.fname + '</p>');
            } 
            if (data.errors.phonenoexist) {
              $('.card-header').append('<div class="alert alert-info alert-dismissible" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'+data.errors.phonenoexist+'</div>');
            }
            } else {
             $('.card-header').append('<div class="alert alert-success alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'+data.message+'</div>');
        }
    });
       e.preventDefault();
    });


//the problem is, the code insert data even if the phone exist why?

the problem is, the code insert data even if the phone exist why?

Edited by mahenda

a phone number, despite being called a number, isn't an integer. it is a formatted string consisting of 3 or 4 fields, depending on  which country you live in and if you are including the country code with international numbers. the signed integer you are using (int(12) isn't even valid) can only hold a value up to 2147483647 (214 748 3647) which can only store some US phone numbers up to area-code 214.

use a string data type and format the value into a common format before using it.

once you define the column with a usable data type and as a unique index, just attempt to insert the data and detect if a duplicate index error number occurred to determine if the phone number already exists.

Edited by mac_gyver
  • Like 1
Just now, mahenda said:
47 minutes ago, mac_gyver said:

a phone number, despite being called a number, isn't an integer. it is a formatted string consisting of 3 or 4 fields, depending on  which country you live in and if you are including the country code with international numbers. the signed integer you are using (int(12) isn't even valid) can only hold a value up to 2147483647 (214 748 3647) which can only store some US phone numbers up to area-code 214.

use a string data type and format the value into a common format before using it.

once you define the column with a usable data type and as a unique index, just attempt to insert the data and detect if a duplicate index error number occurred to determine if the phone number already exists.

Thanks it works


// i changed the columns in a table mydata 
create table mydata (
	id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
	fname varchar(20) NOT NULL,	
    phoneno varchar(12) NOT NULL UNIQUE
    /*......*/
);
//it works, but i validated the phoneno so the user can't enter invalid number

 

is iy possible to auto dismiss the appended alert? 

this doesn't work

 $("#slide-up").fadeTo(2000, 500).slideUp(500, function(){
        $("#slide-up").slideUp(500);
  });

 

after adding  #slide-up id

$('.card-header').append('<div class="alert alert-info alert-dismissible" role="alert" id="slide-up"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'+data.errors.phonenoexist+'</div>');

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.