Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php mysql if not exists insert

$mysqli = new mysqli(SERVER, DBUSER, DBPASS, DATABASE);
$result = $mysqli->query("SELECT id FROM mytable WHERE city = 'c7'");
if($result->num_rows == 0) {
     // row not found, do stuff...
} else {
    // do other stuff...
}
$mysqli->close();
Comment

php sql insert into if not exists

INSERT IGNORE INTO `transcripts`
SET `ensembl_transcript_id` = 'ENSORGT00000000001',
`transcript_chrom_start` = 12345,
`transcript_chrom_end` = 12678;
Comment

PHP MySQL Insert record if not exists in table

<?php  
 $connect = mysqli_connect("localhost", "root", "", "zzz");  
 $messsage = '';  
 if(isset($_POST["add"]))  
 {  
      if(!empty($_POST["brand"]))  
      {  
           $sql = "  
                INSERT INTO brand (brand_name)  
                SELECT '".$_POST["brand"]."' FROM brand  
                WHERE NOT EXIST(  
                 SELECT brand_name FROM brand WHERE brand_name = '".$_POST["brand"]."'  
                ) LIMIT 1  
           ";  
           if(mysqli_query($connect, $sql))  
           {  
                $insert_id = mysqli_insert_id($connect);  
                if($insert_id != '')  
                {  
                     header("location:data_already_inserted.php?inserted=1");  
                }  
                else  
                {  
                     header("location:data_already_inserted.php?already=1");  
                }  
           }  
      }  
      else  
      {  
           header("location:data_already_inserted.php?required=1");  
      }  
 }  
 if(isset($_GET["inserted"]))  
 {  
      $message = "Brand inserted";  
 }  
 if(isset($_GET["already"]))  
 {  
      $message = "Brand Already inserted";  
 }  
 if(isset($_GET["required"]))  
 {  
      $message = "Brand Name Required";  
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial | MySQL Insert record if not exists in table</title>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
      </head>  
      <body>  
           <br />  
           <div class="container" style="width:500px;">  
                <label class="text-danger">  
                <?php  
                if($message!= '')  
                {  
                     echo $message;  
                }  
                ?>  
                </label>  
                <h3 align="">Insert Data</h3><br />                 
                <form method="post">  
                     <label>Enter Brand Name</label>  
                     <input type="text" name="brand" class="form-control" />  
                     <br />  
                     <input type="submit" name="add" class="btn btn-info" value="Add" />  
                </form>  
           </div>  
           <br />  
      </body>  
 </html>
Comment

PREVIOUS NEXT
Code Example
Php :: remove first element in array php 
Php :: mysql get the last id php 
Php :: laravel auth namespace 
Php :: get random data laravel 
Php :: current URL without url site laravel 
Php :: how to validate use unique in laravel 8 controller 
Php :: php if string contains 
Php :: laravel update and insert transaction 
Php :: General error: 1709 Index column size too large. The maximum column size is 767 bytes. 
Php :: sanitize user input php 
Php :: PHP Max Input Vars 
Php :: Skip WooCommerce Cart page and redirect to Checkout page 
Php :: laravel password validation 
Php :: Laravel validating birthdate by 13 years old 
Php :: read-json-data-response-using-php 
Php :: how validate data if is exist must not be empty in laravel 
Php :: how to update radio button value in database using php 
Php :: php replace every occurrence of character in string 
Php :: string to int php 
Php :: is_page () 
Php :: mysqli real escape string php 
Php :: upload_max_filesize 
Php :: invalid datetime format 1292 
Php :: php read csv file into array 
Php :: get taxonomies for custom post type 
Php :: str_replace php 
Php :: php imap install 
Php :: laravel validate enum field 
Php :: laravel required_with 
Php :: make model inside module laravel 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =