tags:

views:

191

answers:

2

I have the following prepared query I am using with mysql and php. It is to allow users to be added to a particular category, passed in via $subcat.

$alterQuery = "UPDATE AUCTIONS SET subcat= ? WHERE article_no= ?";
   if ($altRecord = $con->prepare($alterQuery)) {
       $altRecord->bind_param("ss", $subcat, $pk);
       $altRecord->execute();
    $altRecord->close();
    echo "true";
    }

What I would like to do, in the same query, is insert a new record in the USERS table only inserting the username, after checking that the username does not already exist. Is it possible just to change $alterQuery to:

What is the best what to do this?

Check for the username, and if it == null, do a subsequent insert? can I have two different queries executed in the same if block?

edit: attaching database scheme in response to comment

AUCTIONS    

    ARTICLE_NO   varchar(20) primary key
    ARTICLE_NAME  varchar(100)
    SUBTITLE  varchar(20)
    CURRENT_BID  decimal(5,2)
    START_PRICE  decimal(5,2)
    BID_COUNT  varchar(20)
    QUANT_TOTAL  varchar(20)
    QUANT_SOLD  varchar(20)
    ACCESSSTARTS  varchar(30)
    ACCESSENDS  varchar(20)
    ACCESSORIGIN_END  varchar(20)
    USERNAME  varchar(20)  
    BEST_BIDDER_ID  varchar(20)  
    FINISHED  tinyint(4)  
    WATCH  tinyint(4)   
    BUYITNOW_PRICE  decimal(5,2)  
    PIC_URL  varchar(120)  
    PRIVATE_AUCTION  tinyint(4)
    AUCTION_TYPE  varchar(20)  
    ACCESSINSERT_DATE  varchar(20)
    ACCESSUPDATE_DATE  varchar(20)
    CAT_DESC  varchar(20)  
    CAT_PATH  varchar(20)  
    ARTICLE_DESC  text  
    COUNTRYCODE  varchar(20)
    LOCATION  varchar(20)
    CONDITIONS  varchar(20)
    REVISED  tinyint(4) 
    PAYPAL_ACCEPT  tinyint(4) 
    PRE_TERMINATED  tinyint(4) 
    SHIPPING_TO  varchar(20)
    FEE_INSERTION  decimal(5,2)
    FEE_FINAL  decimal(5,2)
    FEE_LISTING  decimal(5,2)
    PIC_XXL  tinyint(4) 
    PIC_DIASHOW  tinyint(4) 
    PIC_COUNT  varchar(20)
    ITEM_SITE_ID  varchar(20)
    STARTS  datetime  
    ENDS  datetime  
    ORIGIN_END  datetime
    SUBCAT  varchar(20)

USERS
    username   varchar(30) primary key
    firstname  varchar(30)
    lastname  varchar(30)
    flaggedauctions  varchar(30)
    lastauction  varchar(30)  
    street1  varchar(30)  
    city1  varchar(30)  
    postcode1  varchar(30)
    street2  varchar(30)
    city2  varchar(30)  
    postcode2  varchar(30)
    phone  varchar(30)  
    mobilephone  varchar(30) 
    fax  varchar(30)  
    email  varchar(30)  
    website  varchar(30) 
    bank  varchar(30)  
    banknumber  varchar(30)
    accountnumber  varchar(30)
    comments  varchar(30)
+1  A: 

Why would you want to do that? Enclosing your queries in a transaction will either be a complete commit or rollback.

http://en.wikipedia.org/wiki/ACID#Atomicity

cherouvim
How is the operation I want to perform not atomic. It is always added to a category, and the username either is or is not inserted.
Joshxtothe4
Up 1, because transactions are the way to go. Of course you can conditionally insert, but within the transaction. That way, if an error occurs, the whole thing will be rolled back, and in the good case you will get what you want. Nice and clean.
Daniel Schneller
I thought my queries would be a transaction?
Joshxtothe4
You need to enclose your queries with a begin/commmit. http://dev.mysql.com/doc/refman/5.1/en/innodb-transaction-model.html
cherouvim
+1  A: 

It will be best to create a stored procedure:

DELIMITER $$
CREATE PROCEDURE prc_add_user_to_category(_subcat VARCHAR(20), _article_no VARCHAR(20), _username VARCHAR(30))
BEGIN
        UPDATE  AUCTIONS
        SET     subcat= _subcat
        WHERE   article_no= _article_no;
        INSERT
        INTO    users (username)
        VALUES  (_username)
        ON DUPLICATE KEY
        UPDATE
        username = username;
END
$$
DELIMITER ;

and call it from your code:

$alterQuery = "CALL prc_add_user_to_category(?, ?, ?)";
   if ($altRecord = $con->prepare($alterQuery)) {
       $altRecord->bind_param("ss", $subcat, $pk, $userid);
       $altRecord->execute();
    $altRecord->close();
    echo "true";
    }

Checked and proved to compile and work against your schema on 5.2.0-falcon-alpha-community-nt-log

Quassnoi
This failed compeltely with mysql 5.0. I tried the following: CREATE PROCEDURE addToCategory(_subcat INT, _article_no INT, _username INT)BEGIN DECLARE UPDATE AUCTIONS SET subcat= _subcat WHERE article_no= _article_no; INSERT INTO users (username) VALUES (_username) ON DUPLICATE KEY UPDATE SET username = username;ENDIt said to check my syntax near UPDATE AUCTIONS
Joshxtothe4
It now gives:#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')BEGIN UPDATE AUCTIONS SET subcat= _subcat WHE' at line 1
Joshxtothe4
Add length to a VARCHAR definition
Quassnoi
you mean for my fields? They all have a length
Joshxtothe4
Have you seen the post update?
Quassnoi
Yep. I am using that, and it gives an error and states to check the syntax near 'UPDATE AUCTIONS
Joshxtothe4
Sorry then, I can't help you without seeing your schema.
Quassnoi
I will amend it to my question.
Joshxtothe4
In the future, please post your schema as CREATE TABLE statements so that it can be easily pasted into the query tool.
Quassnoi
I am sorry about the scehem, I will post it as a create table staement from now on. I still get a syntax error, this time near DELIMITER $$. I am putting it into the sql tool through phpmyadmin
Joshxtothe4
You use DELIMITER $$ when using MySQL command line tool. For phpmyadmin, just paste text from CREATE PROCEDURE to END
Quassnoi
It still gives a error near 'UPDATE AUCTIONS, when using from create to end. I am using Mysql 5.0.32 on Linux.
Joshxtothe4
See here: http://forums.mysql.com/read.php?98,126320,183863#msg-183863
Quassnoi
Hmm, I am trying with DELIMITER ;; CREATE PROCEDURE prc_add_to_category(_subcat VARCHAR(20), _article_no VARCHAR(20), _username VARCHAR(30))BEGIN UPDATE AUCTIONS SET SUBCAT= _subcat WHERE ARTICLE_NO= _article_no; INSERT INTO USERS (username) VALUES (_username) ON DUPLICATE KEY UPDATE username = USERNAME;END;;and it gives an error near DELIMITER..
Joshxtothe4
Is there any way to do this other than as a stored procedure?
Joshxtothe4
No, if you want to have it done in one round-trip to the server, you'll have to create a stored procedure.
Quassnoi