Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you <a href="https://dev.mysql.com/doc/refman/5.0/en/create-index.html" rel="nofollow">create a <code>unique</code> index</a> for the column Username an insert with the same value for Username as an other record already in the table will cause a specfic error code. Your script can simply try to insert the new record and handle the specific error code, something like</p> <pre><code>define('ER_DUP_KEY', 1022); // pre-condition: $user, $passwd, $boss have been prepared for usage within an sql statement $query = sprintf(" INSERT INTO tbluser (`Username`, `Password`, `BossID`) VALUES ('%s', '%s', '%s') ", $user, $passwd, $boss); if ( !mysql_query($query) ) { switch(mysql_errno()) { case ER_DUP_KEY: // handle duplicate user name break; default: // handle other errors } } </code></pre> <p>If you would use two queries (SELECT+INSERT) you had to handle <a href="https://en.wikipedia.org/wiki/Race_condition#Computing" rel="nofollow">race conditions</a>, e.g. by locking the table.</p> <hr> <p>edit: I missed the implicit join <code>FROM tbluser, tblfile</code>.<br> In that case you might be interested in <a href="https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html" rel="nofollow">INSERT ... ON DUPLICATE KEY UPDATE</a>, esp. the <blockquote>If a table contains an AUTO_INCREMENT column and INSERT ... UPDATE inserts a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value. If the statement updates a row instead, LAST_INSERT_ID() is not meaningful. However, you can work around this by using LAST_INSERT_ID(expr). Suppose that id is the AUTO_INCREMENT column. To make LAST_INSERT_ID() meaningful for updates, insert rows as follows: </blockquote> part. I.e.:</p> <ul> <li>create a unique index for Username</li> <li>do the <code>INSERT INTO tbluser</code> query with an ON DUPLICATE KEY UPDATE clause using LAST_INSERT_ID(expr)</li> <li>fetch the insert id, see <a href="http://docs.php.net/function.mysql-insert-id" rel="nofollow">http://docs.php.net/function.mysql-insert-id</a></li> <li>do the second insert using the insert_id from the previsous query</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload