PHP - Check If Rows Exist
Is it possible to check if multiple rows exist at the same time. For example, if a row with the id "1" and another row with id "2" exists, and return something like true then false if only the first exists?
I have a file that provides tens of thousands to check and it takes forever. I know this sounds bad off the start, but it's not something that is run normally. Only to update my database with new additions.
Is there any good way to check a lot at once? I know you can insert many at a time and save a lot of time.
Similar TutorialsOk guys, I am working on my code as I am checking the row in a database that if i have got a same row that I insert the functions in the URL then throw the error that says "you can't store". When I am checking as the rows have already exist, i keep getting "you can now store". Do you know how i can check in a database to see if the rows is exist? here's the code: Code: [Select] <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbuser'); define('DB_PASSWORD', 'mydbpass'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $image = clean($_GET['image']); $strings = clean($_GET['strings']); $username = clean($_GET['user']); $password = clean($_GET['pass']); if (isset($_GET['user']) && (isset($_GET['pass']))) { if($username == '' || $password == '') { $errmsg_arr[] = 'username or password are missing'; $errflag = true; } } if (isset($_GET['image']) || (isset($_GET['strings'])) || (isset($_GET['user']))) { if($image == '' || $strings == '' || $username == '') { $errmsg_arr[] = 'image, strings or username are missing'; $errflag = true; } } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $insert = array(); if(isset($_GET['image'])) { $insert[] = 'image = \'' . clean($_GET['image']) . '\''; } if(isset($_GET['user'])) { $insert[] = 'user = \'' . clean($_GET['user']) .'\''; } if(isset($_GET['pass'])) { $insert[] = 'pass = \'' . clean($_GET['pass']) . '\''; if (count($insert)>0) { $names = implode(',',$insert); if($image && $strings && $from && $username) { $qry="SELECT * FROM members WHERE username='$username'"; $result=mysql_query($qry) or die('Error:<br />' . $qry . '<br />' . mysql_error()); $row=mysql_fetch_assoc($result); if ($row >= 1) { echo 'you cant store.'; } else { echo 'you can now store'; } } elseif($username && $pass) { echo "working 2"; } } } ?> Gud pm!! So i made this script to check if a username and password exist but it kinda fails me. It gives me a database connection error from my confirm_query() which means it returned false? so here's my code: Code: [Select] function check_if_exist($username, $password) { global $connection; $query = "SELECT username, hashed_password"; $query .= "FROM users "; $query .= "WHERE username = '$username' "; $query .= "AND hashed_password = '$password' "; $query .= " LIMIT 1"; $result_set = mysql_query($query, $connection); confirm_query($result_set); if(mysql_num_rows($result_set) == 1) { return true; } else { return false; } } my confirm_query(): Code: [Select] function confirm_query($result) { if(!$result) { die("Database connection failure ". mysql_error()); } } I have a another script to check if the email exist and it works perfectly, i dunno why this won't work properly.. maybe another simple mistake.. Any ideas or Violent reaction? Thanks! Hi.. So what i want to do is basicly to check if id XX exist in my database. How can this be done? Im thinking of something like: if(xx exist){do this} else{do this} Iv'e tried google etc before i posted here, but couldent really find anything where i found a soutable answer.. Hope someone can help! (: Hello, i want to check whether image exist or not, but i can't get any data about it. I have tried everything: getimagesize, file_exist, filesize, etc. <? $info = @getimagesize('http://jargames.eu/ic.php?lf=files/All phone/240x320/007-quantum_of_solace_240x320.jar'); if(isset($info['mime'])){echo 'Image exist';} else{echo 'Image doesnt exist';} ?> Ty, for your help. Hi everyone, I'm still learning, but getting intermediate in PHP now, but it is still challenge to learn. I'm trying to have php check to see if one file inside folder in server, seem I could not get it right, but I tested it on other site, it works, but not this script, I don't understand why it won't work...maybe logical is wrong? here my code: if ($_POST['video']) { $path1 = "UPLOADS/Home/"; $path2 = "UPLOADS/Breakfast/"; $path3 = "UPLOADS/Spider/"; $scan1 = scandir($path1); $scan2 = scandir($path2); $scan3 = scandir($path3); $count1 = count($scan1) - 3; $count2 = count($scan2) - 3; $count3 = count($scan3) - 3; if($count1 > 0) { header('location:exist.html'); }elseif ($count2 > 0) { header('location:exist.html'); }elseif ($count3 > 0) { header('location:exist.html'); } But other site, it works: $scan = scandir($path); $count = count($scan) - 3; echo $count; if($count > 1){ echo "Hello yourself!<br />"; } Anyone will help will be appreciate! Thank you! Gary Edited April 3, 2019 by sigmahokiesHi! I wrote a small function that check if person_id already exist in the table called persons_web_tbl. If the person_id exist I get a message otherwise I add twitter/facebook profile of that person. I'm just trying to prevent duplicated entries this way. I will get person_id from table person_tbl, where are all people saved. The function actually works, I'm just curious if I'm on the right path. Thanks! $person_id = 11; class test extends dbh{ public function personExist($person_id) { $sql = "SELECT person_id FROM persons_web_tbl WHERE person_id = ?"; $stmt = $this->conn->prepare($sql); $stmt->execute([$person_id]); $row = $stmt->fetchColumn(); if ($row > 0){ echo "found"; }else { $person_web_twitter = "add something"; $sql = "INSERT INTO persons_web_tbl (person_web_twitter, person_id) VALUES (?, ?)"; $stmt = $this->conn->prepare($sql); $stmt->execute([$person_web_twitter, $person_id]); } } } $person = new test (); $person = $person->personExist($person_id); Edited February 15, 2020 by mactron //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 by mahendaCode: [Select] $arr = array('../', 'j/', 'AcidBB/', 'fileBOMB/', 'OurHome', 'up'); foreach ($arr as $value) { // echo "Value: $value<br /><br />"; if (is_dir($value)) { echo "<tr> <td>{$value}</td> <td><font color='#66FF33'>Exists</font></td></tr><br />"; } else { echo "<tr> <td>{$value}</td> <td><font color='#FF0000'>Does Not Exist</font></td></tr><br />"; } } ~~~ Solved after posting. Sorry. ~~~ Ok i have an Array of directories i need to check before the script installs itself, I know how to check for directories but i dont know how to scan each array in the list without calling each one seperately in Code: [Select] If() { } Else If { } Else If { } And so on and so on... My Question is, If a For Statement is neaded, How would i go about checking each string in the Array List? I have a customer registration form up and running, the form allows customers to complete the forms and the data is then inserted into mysql db table via php form. I want to prevent duplicates records from already existing customers from creating a new records instead I want the customers to update their existing records in the backend. That is allow them to complete the form as normal, once they hit the submit button, the code should check if the customer first name and last name is already in the database if yes then it should update their records and if not it should insert new record to the table. I do not want to alert the customer that their records already exist. I do not know where to start. Please help Thank you so much in advance for your help. As the title says, how can i check if multiple arrays exist inside an array or not? Any help is greatly appreciated!
Can I use php to delete multiple rows that have a checkbox selected. I'm not sure how to go about doing this. Any suggestions? I really need some huge help on deleting rows with check boxes. Im completly lost on how to do this. Read some examples online and even with the code being small to do this i cant figure it out on intergrading it with the code I wrote. sorry that the code is long on this. I have decent comments though. At the very bottom i have started writing the delete code. My problem is figuring out how to make the script know what check box is checked. I have a select all check box java script on this page too Code: [Select] <form name="frm1" id="frm1" action="" method="post" 15.onsubmit="javascript:return submitIt('frm1')"> <p> <center> <table width="60%" border="1" cellspacing="1" cellpadding="0"> <tr align="center"> </tr> <tr align="center"> <td><input type="checkbox" name="checkAll" value="x" id="checkall" onclick="checkUncheckAll('association[]', this.checked)" /></td> <td>Recipient</td> <td>Subject</td> <td>Sent</td> <td>read/unread</td> </tr> <?php // find out how many rows are in the table $count3 = "SELECT COUNT(*) FROM sent WHERE id = '".($_SESSION['user_id'])."'";; $count2 = mysql_query($count3) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($count2); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 10; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET['sent']) && is_numeric($_GET['sent'])) { // cast var as int $currentpage = (int) $_GET['sent']; } else { // default page num $currentpage = 1; }// end if // if current page is greater than total pages... if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages; }// end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; // get the info from the db $count3 = "SELECT * FROM sent WHERE id = '".($_SESSION['user_id'])."' ORDER BY time DESC LIMIT $offset, $rowsperpage "; $count2 = mysql_query($count3) or trigger_error("SQL", E_USER_ERROR); // while there are rows to be fetched... while ($list = mysql_fetch_assoc($count2)) { // these are variables to place in the echo $sent_id = $list['sent_id']; $recipient = $list['sendto']; $subject = $list['subject']; $read = $list['read']; // grabs the time offset $take3 = "SELECT time_offset FROM users WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'"; $take2 = mysql_query($take3) or die(mysql_error()); $take1 = mysql_fetch_array($take2); $recieved = $list['time']; // adds the users time offset to the inputed time $time = ($recieved + $take1['time_offset']); // sets default time to UTC then formats the inputed time that was offset with the users offset date_default_timezone_set('UTC'); $user_offset = date('h:i:s a M/d', $time); ?> <tr> <td><center><input type="checkbox" name="association[]" id="<?php echo $sent_id; ?>" value="1" /></center></td> </label></td> <td><center><a href="search_goaulds.php?goauld=<?php echo $recipient; ?>"><?php echo $recipient ?></a></center></td> <td><center><a href="sent_view.php?sent_id=<?php echo $sent_id; ?>"><?php echo $subject ?></a></center></td> <td><center><?php echo $read ?></center></td> <td><center><?php echo $user_offset ?></center></td> </tr> <?php } // while loop ?> </table> </center> <div class="deletecontainer"> <div class="delete"> <br/> <input type="submit" name="delete" id="delete" value="Delete"></p> </form> </div> </div> <?php if(isset($_POST['delete'])) { // if checkbox is checked with the matching sent_id if(isset($_POST['sent_id'])) { // then delete sent_id matching the checkbox id mysql_query("DELETE FROM `sent` WHERE `sent_id`= '".($_POST['sent_id'])."'"); } } ?> i have error where my code should update existing data where id exist, it get updated ..but the others data is disappeared .only the data updated is remained $sql1 ="select*from semakan_dokumen where email='$stdEmail'"; $sqlsearch1 = mysqli_query($dbconfig,$sql1); $resultcount1 = mysqli_num_rows($sqlsearch1); if($resultcount1 > 0){ $query1=("UPDATE semakan_dokumen set student_id='$noMatrik', email= '$stdEmail', surat_tawaran='$fileName', ic='$fileName1',sijil_lahir='$fileName2',sijil_spm= '$fileName3',sijil_sekolah= '$fileName4', sijil_dip= '$fileName5',sej_julai='$fileName6',bm_julai='$fileName7',muet='$fileName8', mid1='$fileName9',yuran= '$fileName10',umpa_bend1= '$fileName11',umpa_bend2='$fileName12',bpkp='$fileName13', penaja='$fileName14',kesihatan= '$fileName15', jhepa='$fileName16' where email= '$stdEmail' "); }else{ //filezip $query1 = "INSERT INTO semakan_dokumen (email,surat_tawaran,ic,sijil_lahir,sijil_spm,sijil_sekolah,sijil_dip,sej_julai,bm_julai,muet,mid1,yuran,umpa_bend1,umpa_bend2,bpkp,penaja,kesihatan,jhepa) VALUES ('$stdEmail','$fileName','$fileName1','$fileName2','$fileName3','$fileName4','$fileName5','$fileName6','$fileName7','$fileName8','$fileName9','$fileName10','$fileName11','$fileName12','$fileName13','$fileName14','$fileName15','$fileName16')"; }
Hi, I need help to check multiple rows for many columns, so that the script won't re-insert a duplicate entry. For eg: $isRowExist=mysql_query("SELECT (field1,field2) from table1 WHERE field1='$f1' AND field2='$f2' "); if(mysql_num_rows($isRowExist)==1) print "already in table1!"; else //proceed to insert into table table1 *BUT I tried this and it says mysql_num_rows expects resource, and this warning only shows when I have multipel columns I want to compare (it works just fine with one column in the WHERE clause above) Please help! i wanting users to be able to update there email address and check to see if the new email already exists. if the email is the same as current email ignore the check. i have no errors showing up but if I enter a email already in the db it still accepts the new email instead of bringing the back the error message. Code: [Select] // email enterd from form // $email=$_POST['email']; $queryuser=mysql_query("SELECT * FROM members WHERE inv='$ivn' ") or die (mysql_error()); while($info = mysql_fetch_array( $queryuser )) { $check=$info['email']; // gets current email // } if($check!=$email){ // if check not equal to $email check the new email address already exists// $queryuser=mysql_query("SELECT * FROM members WHERE email='$email' "); //$result=mysql_query($sql); $checkuser=mysql_num_rows($queryuser); if($checkuser != 0) { $error= "0"; header('LOCATION:../pages/myprofile.php?id='.$error.''); } } cheers what Im basically trying to do is just like a phpmyadmin function... you select rows you want to update with a checkbox and then it takes you to a page where the rows that are clicked are shown in forms so that you can view and edit info in them... and then have 1 submit button to update them all at once. I have 2 queries that I want to join together to make one row
if so how would they be used in a mysql query? Basically this gets a number from a table on my DB, and displays the content relevent to that number...the second part checks for that number and gets other information according to that number... what i want to do is make sure that the number from the first query exists in the table from the query, and if it doesn't print out and error saying "there is no information" or whatever.. anyone have an idea? hopefully this makes sense Code: [Select] <?php require_once('Connections/myConnect.php'); $detnum = $_GET['Details']; $detinfo = mysql_query("SELECT* FROM `Agency Stores - Table 1` WHERE F11=".$detnum); $det = mysql_fetch_array($detinfo); $salesnum = $det['F3']; $salesinfo = mysql_query("SELECT* FROM `By Agency Store _` WHERE store_store=".$salesnum); $sales = mysql_fetch_array($salesinfo); if(mysql_query("SELECT* FROM `By Agency Store _` WHERE store_store=".$salesnum)){ } ?> Hi all, I want to make a script that identifies if an image JPG file exists. Would somthing like this work? <?php $file = "images/image.jpg"; if ($file > 0) { echo "file exists"; } else { echo "no file"; } ?> |