Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post

    Ignor certains db_table fields during php/mysql insert

    I'm wondering if it is possible to skip certain fields in the mysql table in a sql statement. Here is what I mean:

    I have a table that has the following columns:
    firstname
    lastname
    phone
    dob

    Suppose I write the following statement:

    PHP Code:
    $mysql_query=("insert db_table (firstname, lastname, phone) VALUES ('$firstname' ,'$lastname', '$phone')"); 
    In the above scenario, I want the statement to insert the above values, and IGNORE the field in "dob" in the database.

    Currently, left as is, the dob is check for value and produces the following:
    Code:
    Error: Field 'dob' doesn't have a default value
    This may sound a bit unorthodox, but it addresses a critical need at the moment.

    I appreciate any thoughts on this!
    Mossa

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Set a default on the column value via your SQL cli.

    Code:
    ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT 0;
    Last edited by MattF; 12-07-2010 at 03:42 PM.

  • Users who have thanked MattF for this post:

    mbarandao (12-07-2010)

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    Thanks for the reply MattF! The things is that 'dob' may or may not already have a value. That field is updated with an other UPDATE statement.

    Would adding your suggestion replace that existing value?

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Nope. That's merely a default value which is used if you don't supply one in your INSERT. It doesn't have to be zero, btw. That was just for example. You can set any text, NULL etc as the default.

  • #5
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    Great! that worked! Thanks!

    If the field is a BLOB/TEXT, How can similar concept be achieved?

  • #6
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    All issues resolved!
    Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •