Abi sb
Abi sb

Reputation: 189

How check tha value is exist or not in a table if not exist need to update?

                    Hi,
                    In my table, i insert some values like

                    id     admitad_space_id
                    ---    -------
                    1       410

My problem is ,first i need to check the table is empty.If table is empty need to work insert query otherwise need to update the 'admitad_space_id' field.only.

                <div>
                <form action="" method="post">
                <div class='form-group'>
                <span>Enter Admitad Space ID</span>
                <input type='text' name="admitad_space_id" class='form-control' required>
                <button type="submit" name="submit">Submit</button>
                </div>
                <form>
                </div>
                <?php
                global $wpdb;
                 $base_table_name = $wpdb->prefix .'admitad_space_id_list';
                if (isset($_POST['submit'])) {
                 $admitad_space_id = $_POST['admitad_space_id'];
                     $wpdb->insert($base_table_name, array('admitad_space_id' => $admitad_space_id));
                }
            ?>
. Please help

Upvotes: 0

Views: 39

Answers (1)

izem
izem

Reputation: 413

You did not say if their will be only one row in your table and id value is always 1.

If table should have only one row, you can TRUNCATE table and then use insert:

$wpdb->query( "TRUNCATE TABLE $base_table_name" );
$wpdb->insert( $base_table_name, array( 'admitad_space_id' => $admitad_space_id ) );

but it is much better to use $wpdb->replace, so if you know id:

$wpdb->replace( $base_table_name, array( 'id' => 1, 'admitad_space_id' => $admitad_space_id ) )

Reference:

Upvotes: 1

Related Questions