php - Compare Multiple coloumns in MySql -
i need compare 2 different columns in table: tbl_try (country, name). if there 2 coloumns same value nothing. if there no same values of these 2 columns same insert.
this connection database:
<?php require_once("menu.php"); require_once("function.php"); ?>
here main code (read comments try understand)
<?php $conn = connecttosql(); $query= "select * tbl_countries"; $result = mysqli_query($conn, $query) or die("error in query: ". mysqli_error($conn)); $choose = ''; while ($row = mysqli_fetch_assoc($result)) { $choose .= '<option value = "'.$row['name'].'">'.$row['name'].'</option>'; } ?> <div class="form-group"> <label class="control-label col-sm-2" for="country">choose country:</label> <div class="col-sm-5"> <select class="form-control" name ="reg_country" > <option></option> <?php echo $choose;?></select> </div> </div> <br> <div class="form-group"> <div class="col-sm-offset-4 col-sm-10"> <button type="submit" name="submit" class="btn btn-default">submit</button> </div> </div> <?php if(isset($_post['submit'])) { $country = $_post['reg_country']; $_session['country'] = $country; $query2 = "select name,id tbl_flowertypes "; $result2 = mysqli_query($conn, $query2) or die("error in query: ". mysqli_error($conn)); $result2_rows = mysqli_num_rows($result2); //this code not working. need count 2 columns (name , country) // if in tbl_try there country:italy name:12 red roses. nothing if there no data same // insert data $query3 = "select count(*) tbl_try name='$_session[flower_type_name]' , country ='$_session[country]' "; $result3 = mysqli_query($conn, $query3) or die("error in query: ". mysqli_error($conn)); $result3_rows = mysqli_num_rows($result3); // loop counting how many record in table flowertype , loop insert data in tbl_try (this code working fine) for($i = 1; $i <= $result2_rows;$i++) { while($row = mysqli_fetch_assoc($result2)) { $_session['flower_type_name'] = $row['name']; // insertion of data $inserting = "insert tbl_try(name,country) values ('$row[name]','$_session[country]')"; $result3 = mysqli_query($conn, $inserting) or die("error in query: ". mysqli_error($conn)); } } }
the result need be:
the database need in insertion once . if there no same columns in table. if there same data nothing.
i need complete code showing table records know how that.
any suggestions?
the best way add unique definition 2 columns db this:
alter table tbl_try add unique (name, country)
then can check result code db , on basis of result code now, if row stored db or not due case, same name , country in db.
Comments
Post a Comment