My code looks like:
$sql_ = $this->db->prepare("SELECT * FROM user");
$sql_->execute();
$user = $sql_->fetchAll();
$sql_insert_blog = $this->db->prepare("INSERT INTO
blog
(blog_title,blog_author,blog_content,blog_tags,blog_create,blog_update)
VALUES
(:1,:2,:3,:4,:5,:6)");
$sql_insert_blog->execute(array(
":1"=>$title,
":2"=>$author,
":3"=>$content,
":4"=>$tags,
":5"=>$date_create,
":6"=>$date_update
));
So the first sql statement is to proof if the database coneccion is active. It works when i use
print_r($user)
Th second one is the statement thats not been executed.
I have a controller that calls this function on a model, and gives all the info from the form to this function like
function newBlogEntry()
{
$title = $_POST["input_blog_title"];
$author = $_POST["input_blog_author"];
$content = $_POST["input_blog_content"];
$tags = $_POST["input_blog_tags"];
$date_create = date('m/d/Y h:i:s a', time());
$date_update = $date_create;
$this->model->newBlogEntry($title,$author,$content,$tags,$date_create,$date_update);
}
and the function on this model is actually called. i can select all users from db.
i allready checked all the parameters, and also every parameter can be null so, even if the form is emtpy it should be a insert in the database(This is just for testing).
But there is no entry in the database...
So my question is: what am i doing wrong here?
It would appear that MySQL doesn't like your date values. The default format for dates-as-strings in MySQL is YYYY-MM-DD HH:MM:SS, according to the docs; a string that looks like this will automatically be parsed as a date.
use (?,?,?,?,?,?) instead of (:1,:2,:3,:4,:5,:6)
and
$sql_insert_blog->execute(array($title, $author, $content, $tags, $date_create, $date_update));
and
dates should be strings with Y-m-d H:i:s php date() format
Related
Ok so i already have a few insert scripts working, however when i try and use this one nothing happens. I dont get an error or anything, it simply doesnt insert into the database. I have a feeling its the date parameter, however im unsure how to make it recognizable?
<?php //SETTING SESSION VARIABLES FOR ROUND 1 TEAMS AND SCORE
session_start();
if (isset($_POST['go1'])) { // MATCHUP 1
include_once 'dbcon.php';
$_SESSION['t_team1'] = $_POST['team-1'];
$_SESSION['t_team2'] = $_POST['team-2'];
$_SESSION['s_score1'] = $_POST['score-1'];
$_SESSION['s_score2'] = $_POST['score-2'];
$team1winner = mysqli_real_escape_string($conn, $_POST['team-1']);
$team2winner = mysqli_real_escape_string($conn, $_POST['team-2']);
$date1 = mysqli_real_escape_string($conn, $_POST['date-1']);
$sql = "INSERT INTO knockout (knockout_team1, knockout_team2, knockout_date)
VALUES ('$team1winner', '$team2winner', '$date1');";
header("Location: ../tables.php?tables=winner");
}
//date html
<input type="date" name"date-1" value="date" class="date">
First of all, you are putting the SQL command in a string but you are not actually executing the command.
Second of all, if you are executing the command but you are not showing it here and your dbcon.php file is working properly, then it is more likely a date format issue.
Finally, you need to execute all of your commands especially INSERT commands in prepared statements to prevent SQL injections winch is VERY important.
Here how your code should look like :
<?php //SETTING SESSION VARIABLES FOR ROUND 1 TEAMS AND SCORE
session_start();
if (isset($_POST['go1'])) { // MATCHUP 1
include_once 'dbcon.php';
$_SESSION['t_team1'] = $_POST['team-1'];
$_SESSION['t_team2'] = $_POST['team-2'];
$_SESSION['s_score1'] = $_POST['score-1'];
$_SESSION['s_score2'] = $_POST['score-2'];
$team1winner = mysqli_real_escape_string($conn, $_POST['team-1']);
$team2winner = mysqli_real_escape_string($conn, $_POST['team-2']);
$date1 = mysqli_real_escape_string($conn, $_POST['date-1']);
$TeamsStat = $conn->prepare("INSERT INTO knockout (knockout_team1, knockout_team2, knockout_date) VALUES (?, ?, ?)");
$TeamsStat->bind_param("sss", $team1winner, $team2winner, $date1);
$TeamsStat->execute();
$TeamsStat->close();
header("Location: ../tables.php?tables=winner");
}
Where $conn is the object of your database connection.
Since prepared statements doesn't support date type and since the date is not a free input value, the knockout_date column should be a string and the variable $date1 should also be a string.
Hope that helped you.
The date format in HTML is of type dd-mm-yyyy but while inserting it into database you need to make sure its YYYY-mm-dd type. So make sure you are converting it Before inserting it
Ex :
$originalDate = $yourDateVariable;
$newDate = date("d-m-Y", strtotime($originalDate));
Or Go through manual and find alternatives functions to convert it
I'm new with Symfony2 and PHP and I've a question pertaining to formatting dates.
I have a form where the user can enter a date in this format: dd.mm.yyyy. The SQL Database uses this format: yyyy-mm-dd. Now when make an sql query i get a mismatch of course.
public function getFindingPatientQuery($birthdate){
$em = $this->getEntityManager();
$query = $em->createQuery(
'SELECT p
FROM Data\LiveBundle\Entity\DataAPatient p
WHERE p.pDateOfBirth = :birthdate'
)->setParameter('birthdate', $birthdate| ('Y-m-d'));
return $query;
}
What's the easiest way to transform the input dd.mm.yyyy date to the demanded format yyyy-mm-dd? Does there exist some global solution if I'd need this transformation more than 1 time? I prefer a non-JavaScript solution.
I found some answer here in the forum but nothing worked.
Thanks
Try to indicate parameter type and make sure the $birthdate variable is a \DateTime object:
use Doctrine\DBAL\Types\Type;
...
->setParameter('birthdate', $birthdate, Type::DATE);
May be your birthdate isn't the type date ?
$date = DateTime::createFromFormat('j-M-Y', $birthdate);
echo $date->format('Y-m-d');
i would like to help with my code how to edit HTML input date into MySQL TIMESTEMP format cause my SQL code is invalid and data are not added to my database, Thanks for help
also i asking to check this code on bottom cause 2nd SQL deppends on 1st SQL, thanks
if (isset($_POST['pridaj_anime_submit'])) {
$sql_vloz_anime = "INSERT INTO anime (a_name, a_year, a_translated_min, a_translated_max, a_rate_min, a_rate_max, a_edit, a_condition)
VALUES ('$_POST[nazov]', '$_POST[rok]', '0', '$_POST[pocet]', '8', '10', '$_POST[preklad]', '$_POST[stav]')";
mysqli_query($connect_to_db , $sql_vloz_anime);
$sql_ziskaj_a_id_pridaneho_anime = "SELECT * FROM anime WHERE a_name = '$_POST[nazov]'";
$run_sql_ziskaj_a_id_pridaneho_anime = mysqli_query($sql_ziskaj_a_id_pridaneho_anime);
$a_id_ziskane = "";
while ($db_data = mysqli_fetch_assoc($run_sql_ziskaj_a_id_pridaneho_anime)) {
$a_id_ziskane = $db_data['a_id'];
}
$sql_vloz_anime_info = "INSERT INTO anime_info (a_id, a_img, a_start, a_stop, a_time_ep, a_akihabara)
VALUES ('$a_id_ziskane' , '$_POST[obrazok]', '$_POST[zaciatok]', '$_POST[koniec]', '$_POST[cas]', '$_POST[akihabara]')";
mysqli_query($connect_to_db , $sql_vloz_anime_info);
}
$input_date=$_POST['date'];
$date=date("Y-m-d H:i:s",strtotime($input_date));
This will convert input date into MySQL Timestamp compliant format.
Please DO NOT put POST values directly in your queries. Your website or web application will be hacked in no time through SQL Injections.
MySql datetime considered yyyy-mm-dd h:i:s format.
Your input date is like dd-mm-yyyy h:i:s and so on.
Then use convert date to yyyy-mm-dd h:i:s. Read strtotime manual.
For e.g.
$inputDate = '06-06-2017 05:21:34';
$mysqlDate = date("Y-m-d H:i:s",strtotime($inputDate));
// MySql Query.
INSERT INTO table_name SET `your_field`='$mysqlDate'
I have a crazy phenomenon in my php script. I have defined a column as a timestamp in a mysql table. I fill it with the function:
date("Y-m-d H:i:s");
The data in the table then look like this: 2017-04-19 17:08:45
When I query this column with mysqli as a unix timestamp again:
SELECT UNIX_TIMESTAMP (timestamp) as timestampUnix FROM posts
Then binding the result using bind_result to the variable $timestampUnix.
I can echo the variable with
echo $timestampUnix;
and it outputs a correct timestamp like this: 1492614559
however if i do the following:
$timestampUnix2 = $timestampUnix;
echo $timestampUnix2;
there is simply no echo output... What is the reason?
I tried this because I actually want echo only the date in an other format with:
date('d.m.Y', $timestampUnix)
and it gave me 01.01.1970 and i wondered why the timestamp must be 0 but it isnt since when i directly echo it it gives me a correct one.
however when i do
Date('d.m.Y', 1492614559)
it gives me the correct date.. no clue what is going on there!
i know there are many other questions about mysql php Date output, but no one have this issue as i think i cannot do something with the variable i got from the query.
thanks in advance!
edit: i attach the complete code in question:
---the query that inputs the data in the db----
$timestamp = date("Y-m-d H:i:s");
mysqli_query($mysqli,"INSERT INTO posts (timestamp)
VALUES ('$timestamp')");
---the query that fetches the data----
$results = $mysqli->prepare("SELECT UNIX_TIMESTAMP(timestamp) as timestampUnix FROM posts");
$results->execute(); //Execute prepared Query
$results->bind_result($timestampUnix); //bind variables to prepared statement
$postdate = date('d.m.Y',$timestampUnix)
echo $postdate;
I'm trying to filter out repeated values entering into a MySQL table, by comparing the input PHP variable with the timestamp of an entry already present in the table and only if they don't match, the input PHP variable is entered into the table.
$user1_date = mysql_real_escape_string($user1_date); // the date variable
$user1_temp1 = mysql_real_escape_string($user1_temp1);
$user1_temp2 = mysql_real_escape_string($user1_temp2);
$user1_temp3 = mysql_real_escape_string($user1_temp3);
$user1_date = date("Y-m-d H:i:s", strtotime($user1_date)); //Typecasting PHP variable into timestamp
$sql_check = "SELECT * FROM user_details WHERE temp_date ='$user1_date'";
$result_check = mysql_query($sql_check);
$num_rows_check = mysql_num_rows($result_check);
if ($num_rows_check == 0) // To check if there is no entry in the table with the same date and time as input PHP variable
{
$sql_insert = "INSERT INTO data_hour (user_id, temp1, temp_date, temp2, temp3)
VALUES (1,'$user1_temp1', '$user1_date', '$user1_temp2', '$user1_temp3')";
$result_insert = mysql_query($sql_insert);
}
temp_date is a column in the table of type timestamp. Even when the $user1_date is the same as the temp_date(timestamp) column for one of the entries in the table, it considers it as not equal and is inserting it into the table and hence I'm getting repeated values. I'm guessing the WHERE temp_date = '$user1_date'is not working properly. Some troubleshooting that I have done included
Changing '$user1_date' to just $user1_date in the WHERE
statement
Changing the WHERE clause as follows WHERE temp_date = (date)'$user1_date'
It will be great if somebody can help me out with this!
A nice easy solution would be giving temp_date a UNIQUE INDEX in your Mysql Table, as that would not allow the same value to be inserted twice. This would also make your operations more efficient, as you wouldn't have to do SELECT * every time you want to insert something.
However, for what you're doing, I think I see your problem; there are some quirks in your logic so I'll try to dispel them here. (Hopefully?) this will make your program cleaner and you'll be able to pinpoint the error, if not eliminate it altogether.
Examining this piece of code:
// $user1_date doesn't have a value here! //
$user1_date = mysql_real_escape_string($user1_date);
...
$user1_date = date("Y-m-d H:i:s", strtotime($user1_date));
Error 1 - You escape the string before ever setting a value.
What you are doing is that you are using mysql_real_escape_string() before $user1_date is ever defined.
Correction:
// Getting better, but not done. //
$user1_date = date("Y-m-d H:i:s", strtotime($user1_date));
...
$user1_date = mysql_real_escape_string($user1_date);
Error 2 - You do not give the date() function appropriate parameters
The date() function in PHP expects a timestamp, which is just an int. You can easily get the time with time(), so that should rectify your problem
Correction:
// You use strtotime($user1_date), but you should use time() //
$user1_date = date("Y-m-d H:i:s", time());
...
$user1_date = mysql_real_escape_string($user1_date);
These are small mistakes, but they can be deadly. Like I said, you should assign temp_date to a UNIQUE INDEX in your MySQL table, but make sure to correct these errors listed as well.
Let me know how it goes!