• Hi everybody,

    I was looking for solution of avoiding data duplication while using wpdb insert but still I have a problem with that.

    I created custom DB and custom page templates. One page is called “page-insert-proces.php” (code listed below) and it’s included (using php include) to another custom template page called “page-insert”. Page-insert.php is static page (with header, text etc.). Page-insert-proces is process page with form and insert db function. Everything works fine except the data insert duplication. If I used wpdb->insert etc. wordpress everytime duplicate entry. But I need solution that can say “entry with this compName is already in DB” or something like that.

    My goal is that every entery compName is unique.

    I’m new in php and still learning so php functions in combination with wordpress functions is very broad and hard for solve it by myself.

    Thank you very much for your time.

    “page-insert-proces.php code”

    <?php
    
    If($_POST['Submit']) {
    
    global $wpdb;
    
    $compName=$_POST['compName'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $address=$_POST['address'];
    
    if($wpdb->insert(
    		'customers',
    		array(
    				'compName' => $compName,
    				'email' => $email,
    				'phone' => $phone,
    				'address' => $address
    			)
    ) == false) wp_die('Database Insertion failed');
    else echo 'Entry inserted<p />';
    
    ?>
    <a href="https://127.0.0.1/fs_system/">Index</a>
    <meta http-equiv="refresh" content="3;url=https://127.0.0.1/fs_system/insert-stranka/">
    
    <?php
    }
    else
    {
    ?><form action="" method="post" id="addentry">
    
    <label id="compName">Name:<input type="text" name="compName" size="30" /></label>
    <label id="email">Email:<input type="text" name="email" size="10"  /></label>
    <label id="phone">Phone:<input type="text" name="phone" size="30" /></label>
    <label id="address">Address:<select name="address" size="1">
            <option selected>London, England</option>
            <option>Paris, France</option>
            <option>Madrid, Spain</option>
            <option>Prague, Czech Republic</option>
        </select></label>
    
    <p> </p>
    <p> </p>
    </div>
    <input type="submit" name="Submit" id="addentrysubmit" value="Submit" />
    </form>
    
    <?php
    } 
    
    ?>

  • The topic ‘How To – wpdb insert duplication in custom DB’ is closed to new replies.