I want to merge data into a MySQL table from PHP :
<?php
function convertFromDateHM($str)
{
$tmp=$str;
$Y=substr($tmp, 6,4);
$M=substr($tmp, 3,2);
$D=substr($tmp, 0,2);
$H=substr($tmp, 13,2);
$MN=substr($tmp, 18,2);
$tmp=$Y.'-'.$M.'-'.$D.' '.$H.':'.$MN.':00';
return $tmp;
}
function mergeCompteRendu($compterendu)
{
$resultat=explode("\n",$compterendu);
$connec = mysql_connect("192.168.1.123:3306","root", "mysqlroot");
mysql_set_charset('utf8', $connec);
mysql_select_db("finance",$connec);
for($i = 0; $i < count($resultat) ; $i++)
{
$valeur=explode(";",$resultat[$i]);
if ($valeur[0] != '0') // case of update
{
$requete = mysql_query("UPDATE compte_rendu SET
cr_date = '".convertFromDateHM($valeur[1])."',
cr_lieu = '".$valeur[2]."',
cred_id = '".$valeur[3]."',
cr_resultat = '".$valeur[5]."',
cr_comment = '".$valeur[6]."',
adc_id = '".$valeur[7]."'
WHERE cr_id = '".$valeur[0]."'"); // cr_id is the primary key which is auto_increment
}
else // case of insert : here is the problem raised
{
$requete = mysql_query("INSERT INTO compte_rendu(cr_date,cr_lieu,cred_id,cr_resultat,cr_comment,adc_id) SET
cr_date = '".convertFromDateHM($valeur[1])."',
cr_lieu = '".$valeur[2]."',
cred_id = '".$valeur[3]."',
cr_resultat = '".$valeur[5]."',
cr_comment = '".$valeur[6]."',
adc_id = '".$valeur[7]."' ");
}
}
return $resultat[0];
}
NB : the “cr_date” column is of datatype datetime.
This PHP is a webservice which I call from a J2ME client. When I run the app then the record is not inserted into the database ( I checked it through SQLYog ). It’s the case of insert which causes problem , the update is ok.
So what is wrong in my code ?
Your insert query syntax in the code is wrong. You should be using either of the following form.
or,
But in your code you have mixed up the insert syntax so it is not working. You can use
die(mysql_error())to see the exact error generated by MySQL.