I hope someone could give me a quick answer to my question, since i'm not very good in PHP & MySQL.
I want to import a CSV File which includes 2 dates: 1-5-2017 and 31-5-2018.
When i import the csv trough mysql, the dates look like 0000-00-00
My code:
while (($column = fgetcsv($file, 10000, ";")) !== FALSE) {
$sqlInsert = "INSERT into employees (naam,functie,afdeling,contract,DID,DUD,manager,profiel)
values ('" . $column[0] . "','" . $column[1] . "','" . $column[2] . "','" . $column[3] . "','" . $column[4] . "', '" . $column[5] . "','" . $column[6] . "','" . $column[7] . "')";
$result = mysqli_query($conn, $sqlInsert);
Thanks
you didn't point out which column is the date field
in naam,functie,afdeling,contract,DID,DUD,manager,profiel
you can use the function strtotime and date to convert this date format
like below code
$column[2] = date('Y-m-d', strtotime($column[2]));
for example
// this will print 2017-05-01
echo date('Y-m-d', strtotime('1-5-2017'));
Use below code to convert date format
$date="1-5-2017";
$convertedDate=date("Y-m-d", strtotime($date));
Rather than this
STR_TO_DATE('15-06-2017', '%Y/%m/%d')
Try this
STR_TO_DATE('15-06-2017', '%Y-%m-%d')
This should work.Sample Sql query is mentioned below.
INSERT INTO `table1` (`col1`, `col2`, `col3`, `col4`) VALUES ('1', '1', '1',STR_TO_DATE('15-06-2017','%Y-%m-%d'))
Related
I am looking to update one of the table. After I update, all the duplicate data is getting inserted again. Especially, the cloneSQL part of the code. I tried using DISTINCT, NOT EXISTS but no luck.
if(DB_num_rows($checkResult) > 0){
$cloneSQL = "UPDATE DISTINCT pricematrixdiscount SET
discount='" . $vals[3] . "'
WHERE debtorno='" . $_POST['cloneTo'] . "',
product_line='" . $vals[1] . "',
salestype='" . $vals[2] . "' ";
}
else {
$cloneSQL = "INSERT into pricematrixdiscount
(debtorno,
product_line,
salestype,
discount) VALUES
('" . $_POST['cloneTo'] . "',
'" . $vals[1] . "',
'" . $vals[2] . "',
'" . $vals[3] . "')";
How can I insert only distinct values on the pricematricdiscount table without the duplicates being inserted?
Here is my code:
$result = mysql_query("INSERT INTO clients (client_id, name, surname, tel1,tel2,id_num,address)
VALUES ('" .$updating_id . "','" .$updatedName1 . "','" .$updatedName1 . "', '" .$updatedSurname1
. "', '" . $updatedTel1 . "', '" .$updatedTel2 . ", '" .$updatedId_num1. "', '" .$updatedAddress1.
") ON DUPLICATE KEY UPDATE name='" . $updatedName1 . "', surname='" . $updatedSurname1 . "',
tel1='" . $updatedTel1 . "', tel2='" . $updatedTel2 . "', id_num='" . $updatedId_num1 . "',
address='" .$updatedAddress1 . "'");
if(mysql_query($result))
{ echo $updatedName1," ", $updatedSurname1, " updated successfully ";
}
else {
echo mysql_error();}
}
I am noticing that the first quote on the mysql_query("INSERT INTO...
is closing with the first quote of the VALUES ('" .$updating_id . "'... statement and yet the way I quoted is the one in my examples, I have assessed.
Use if($result) instead of if(mysql_query($result)). thx #Vinie
And you miss two simple quotes in your VALUES statement:
$updatedTel2 . "'
$updatedAddress1."'
And you need to have a look at mysql_real_escape_string(); or at least use PDO :)
I have part of the code below:
while($array = $result->fetch_assoc() ){
$second_query = "INSERT INTO".TBL_USERSDONTPAY."VALUES ($array[\"username\"], $array[\"password\"], '0',$array[\"userid|\"], )";
$second_result = $database->query($second_query);
}
The query doesn't seem to work. Any clues? I think it's a problem with the quotes or something. How can actually pass array elements?
here is my whole code i want to move one row to another table
$q = "SELECT * FROM ".TBL_USERS." WHERE username = '$subuser'";
$result = $database->query($q);
if($result && $result->num_rows == 1){
while($array = $result->fetch_assoc() ){
$second_query = "INSERT INTO" . TBL_USERSDONTPAY . "VALUES ('" . $array['username'] . "', '" . $array['password'] . "', '0', '" . $array['userid'] ."')";
$second_result = $database->query($second_query);
if($second_result){
// it worked!
$q = "DELETE FROM ".TBL_USERS." WHERE username = '$subuser'";
$database->query($q);
}
}
}
You need to clean that query up and remove the final comma.
$second_query = "INSERT INTO " . TBL_USERSDONTPAY . " VALUES ('" . $array['username'] . "', '" . $array['password'] . "', '0', '" . $array['userid'] . "')";
I see several issues with your query code
escaping of the array indexes in your string:
you can either end the string and concatenate the parts together:
$second_query = "INSERT INTO " . TBL_USERSDONTPAY .
" VALUES ('" . $array['username'] . "', '" . $array['password'] . "', '0', '" . $array['userid'] . "')";
or use the {$var} syntax:
$second_query = "INSERT INTO " . TBL_USERSDONTPAY .
" VALUES ('{$array['username']}', '{$array['password']}', '0', '{$array['userid']}')";
missing spaces (see example code above .. you were missing the spaces before and after the table name)
missing field names. your query may work without if you specify all fields in the right order, but will fail misteriously when you alter the table later (e.g. add a field to the table)
$second_query = "INSERT INTO " . TBL_USERSDONTPAY .
" (username, password, foo, user_id)".
" VALUES ('{$array['username']}', '{$array['password']}', '0', '{$array['userid']}')";
please note you should actually insert the correct field names in the second line of my example above. You can find more information on this in the MySQL docs for INSERT
Despite reading quite many posts i cannot solve this error-
Unknown column 'alt.atheism1111' in 'field list'
the fields filename,category may have . in the middle of numbers or words,
im using phpmyadmin for database
function insert_rec($cat,$file,$wordid,$synsetid,$seqno)
{
$cat=mysql_real_escape_string($cat);
$file=mysql_real_escape_string($file);
$wordid=mysql_real_escape_string($wordid);
$synsetid=mysql_real_escape_string($synsetid);
$seqno=mysql_real_escape_string($seqno);
echo $cat." ". $file ." ". $wordid." " . $synsetid." " . $seqno;
$sql="INSERT INTO `wordnet50`.`table` (`category`,`filename`,`wordid`,`synsetid`,`seqno`) VALUES (`" . $cat . "`,`" . $file. "`,`" . $wordid. " `,`" . $synsetid . "`,`" .$seqno . "`)";
$result=mysql_query($sql);
if(!$result)
{
die(mysql_error());
}
}
$sql="INSERT INTO `wordnet50`.`table` (`category`,`filename`,`wordid`,`synsetid`,`seqno`) VALUES (`" . $cat . "`,`" . $file. "`,`" . $wordid. " `,`" . $synsetid . "`,`" .$seqno . "`)";
You need to remove "`" from the above query in the values only and replace it with " ' " (single quote)
Use backticks for field names and single quotes for the values.
$sql = "INSERT INTO `wordnet50`.`table` (`category`,`filename`,`wordid`,`synsetid`,`seqno`)
VALUES ('$cat', '$file', '$wordid', '$synsetid', '$seqno')";
It should be wrapped with single quotes not with back tick.
$sql = "INSERT INTO `wordnet50`.`table` (`category`,`filename`,`wordid`,`synsetid`,`seqno`) VALUES ('" . $cat . "','" . $file. "','" . $wordid. "','" . $synsetid . "','" .$seqno . "')";
BackTick escapes MYSQL Reserved WORDS.
if u can post ur db schema than it will be easy to check, as of now it look like u have a field as alt.atheism1111 which can be the show stopper
or use this:
$sql = "INSERT INTO `wordnet50`.`table` (`category`,`filename`,`wordid`,`synsetid`,`seqno`)
VALUES ('$cat', '$file', '$wordid', '$synsetid', '$seqno')";
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP/MySQL - Format date/time
I have input field (name:day1 and varchar) to enter date in my form. It attached with a jquery calendar and picks the date in the format "D, dd M, yy" (I WANT TO PICK LIKE THIS AND CANT CHANGE FORMAT). I want to convert and save into mysql date format into table. How is this possible?
$insert_query = 'insert into '.$this->tablename.'(
venue,
day1,
day2,
day3,
day4,
day5,
day6,
day7,
day8,
day9,
day10,
city,
contactperson,
)
values
(
"' . $this->SanitizeForSQL($formvars['venue']) . '",
"' . $this->SanitizeForSQL($formvars['day1']) . '",
"' . $this->SanitizeForSQL($formvars['day2']) . '",
"' . $this->SanitizeForSQL($formvars['day3']) . '",
"' . $this->SanitizeForSQL($formvars['day4']) . '",
"' . $this->SanitizeForSQL($formvars['day5']) . '",
"' . $this->SanitizeForSQL($formvars['day6']) . '",
"' . $this->SanitizeForSQL($formvars['day7']) . '",
"' . $this->SanitizeForSQL($formvars['day8']) . '",
"' . $this->SanitizeForSQL($formvars['day9']) . '",
"' . $this->SanitizeForSQL($formvars['day10']) . '",
"' . $this->SanitizeForSQL($formvars['city']) . '",
"' . $this->SanitizeForSQL($formvars['contactperson']) . '",
)';
day 1 to day 10 are the dates want to convert...
Try this on your date input:
$date = date('Y-m-d H:i:s', strtotime($_GET['date_input']));
As long as strtotime() picks up the right timestamp from your input, you should be ok. Don't forget to escape it as well.
or in the sql statement you can do that:
$insert_query = 'insert into '.$this->tablename.'(
venue,
day1,
day2,
day3,
(.......) )
values
("' . $this->SanitizeForSQL($formvars['venue']) . '",
STR_TO_DATE("' . $this->SanitizeForSQL($formvars['day1']) . '","%W, %d %M, %y"),
STR_TO_DATE("' . $this->SanitizeForSQL($formvars['day2']) . '","%W, %d %M, %y"),
STR_TO_DATE("' . $this->SanitizeForSQL($formvars['day1']) . '","%W, %d %M, %y"),
(.......) )';
The "%W, %d %M, %y" bit must be in the same format as the date you get from your calendar...
To check all date functions and dates format see these links:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
I hope that helps...