Sunday, March 17, 2013

MySQL - If Exists update else insert Query

INSERT INTO component_psar (tbl_id, row_nr, col_1, col_2, col_3, col_4, col_5, col_6, unit, add_info, fsar_lock)
VALUES('2', '1', '1', '1', '1', '1', '1', '1', '1', '1', 'N')
ON DUPLICATE KEY UPDATE col_1 = VALUES(col_1), col_2 = VALUES(col_2), col_3 = VALUES(col_3), col_4 = VALUES(col_4), col_5 = VALUES(col_5), col_6 = VALUES(col_6), unit = VALUES(unit), add_info = VALUES(add_info), fsar_lock = VALUES(fsar_lock)
The Checking key should be UNIQUE. Only if this query will work.
Refer : http://stackoverflow.com/questions/12639407/sql-if-exists-update-else-insert-syntax-error 

Save array as multiple fields in DB

Set id as an auto_increment primary key column in your table.
create table email (
    id int unsigned primary key auto_increment,
    cus_id int unsigned,
    email varchar(50)
);

insert into email (cus_id, email) values ('$cusid', '$email');
id will then take care of itself, all you have to do is leave it alone when inserting.
PHP code to do this in a loop:
foreach ($test as $cusid => $email) {
    $querystring = "insert into users (cus_id, email) values ('$cusid', '$email')";
    myqsli_query($querystring);
}

Saturday, March 16, 2013

Reset Form on AJAX Call

$("#cform")[0].reset();
or in plain javascript:
document.getElementById("cform").reset();

Javascript Regex for Email Validation

function IsEmail(email) {
  var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}
 
This Function return a true value is Email is valid. Else false will be returned.

Thursday, March 7, 2013

Check whether checkbox is checked using Jquery on page load

 In the following example,
 isAgeSelected is a checkbox and txtAge may be anthing like textbox or div area.
$('#isAgeSelected').is(':checked') ? $("#txtAge").show() : $("#txtAge").hide();