user3163063 user3163063 - 2 months ago 7
MySQL Question

MySQL: Check if row exists does not really work

I have encountered a genuine problem which cause is uknown to me.
I am inserting datetime into database (and some other data). Script works, it is adding new records and it's just swell. The genuine problem is about duplicating data. Even though I have this if-condition, it does not work properly :x

$data = $year."-".$month."-".$day." ".$godzina.":".$minuta;

$result = mysql_query("SELECT * from kolejka WHERE data LIKE '$data%'");
$exists = (mysql_num_rows($result))?TRUE:FALSE;
header('Location: location');
$_SESSION['msg'] = 'text';
$sql = "INSERT INTO kolejka (pID, mID, data, odbyta) VALUES ('$id', '$mid', '$data', '0')";
if (!mysql_query($sql)){
header('Location: 'location');
$_SESSION['msg'] = 'error';

here's db:


It's got to be done by mysql_query :>
Thanks in advance for any comments


Your problem is that by concatenating the values like that, you're not padding the numbers, so they don't match. ( 9:00 != 09:00) You want to create a valid date format from your variables with mktime():

 $data = date('Y-m-d H:i', mktime ($godzina, $minuta, 0, $month, $day, $year));