mahenda Posted April 8, 2021 Share Posted April 8, 2021 (edited) //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 April 8, 2021 by mahenda Quote Link to comment https://forums.phpfreaks.com/topic/312444-check-if-data-exist-before-insert-not-working/ Share on other sites More sharing options...
Barand Posted April 8, 2021 Share Posted April 8, 2021 Because you haven't specified that phoneno is unique. Quote Link to comment https://forums.phpfreaks.com/topic/312444-check-if-data-exist-before-insert-not-working/#findComment-1585666 Share on other sites More sharing options...
mac_gyver Posted April 8, 2021 Share Posted April 8, 2021 (edited) 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 April 8, 2021 by mac_gyver 1 Quote Link to comment https://forums.phpfreaks.com/topic/312444-check-if-data-exist-before-insert-not-working/#findComment-1585667 Share on other sites More sharing options...
mahenda Posted April 8, 2021 Author Share Posted April 8, 2021 1 hour ago, Barand said: Because you haven't specified that phoneno is unique. Thanks it works Quote Link to comment https://forums.phpfreaks.com/topic/312444-check-if-data-exist-before-insert-not-working/#findComment-1585668 Share on other sites More sharing options...
mahenda Posted April 8, 2021 Author Share Posted April 8, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/312444-check-if-data-exist-before-insert-not-working/#findComment-1585669 Share on other sites More sharing options...
mahenda Posted April 9, 2021 Author Share Posted April 9, 2021 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>'); Quote Link to comment https://forums.phpfreaks.com/topic/312444-check-if-data-exist-before-insert-not-working/#findComment-1585674 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.