Link to home
Start Free TrialLog in
Avatar of IvetteM
IvetteM

asked on

Trouble inserting data in mysql using php and ajax.

I`ve created a form where i draw checkboxes of days and checkboxes of months. The idea is to asign those days lets say monday, tuesday, and friday for the months january, march, and december. I use a if condition to verify if monday has been checked and then another if to checked the months. But when i verify the database it only inserts the monday, tuesday and friday just for january and not for the other 2 months.

Here is part of my code, i've only attach the part where it receives the posts.  Any help is appreciated.

 
$corte=substr($_POST['idmed'] ,-4, strlen($_POST['idmed'] )-0);
	$fechareg = date("Y-m-d h:i:s");
	
		if($con->conectar()==true){
		
	if ($_POST['chkm']=="true"){
		
			if($_POST['chken']=="true"){//January
				$query_Insert= "INSERT INTO cit_distribucion (codigo,iddistribucionmed,idtipocon,idrangohoras,idempleado,primera,subsecuente,mes,yrs,dia,IdUsuarioReg,FechaHoraReg) VALUES ('0',concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',1,'".$_POST['txtyr']."',2,'0','$fechareg')";
				$query = @mysql_query($query_Insert) or die('La consulta falloó: ' . @mysql_error());
			}
			
			if($_POST['chkfb']=="true"){//February
				$query_Insert= "INSERT INTO cit_distribucion (codigo,iddistribucionmed,idtipocon,idrangohoras,idempleado,primera,subsecuente,mes,yrs,dia,IdUsuarioReg,FechaHoraReg) VALUES ('0',concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',2,'".$_POST['txtyr']."',2,'0','$fechareg')";
				$query = @mysql_query($query_Insert) or die('La consulta falloó: ' . @mysql_error());
			}
			if($_POST['chkmz']=="true"){//March
				$query_Insert= "INSERT INTO cit_distribucion (codigo,iddistribucionmed,idtipocon,idrangohoras,idempleado,primera,subsecuente,mes,yrs,dia,IdUsuarioReg,FechaHoraReg) VALUES ('0',concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',3,'".$_POST['txtyr']."',2,'0','$fechareg')";
				$query = @mysql_query($query_Insert) or die('La consulta falloó: ' . @mysql_error());
			}
	
	}//End of the if for Monday
				
if ($_POST['chkt']=="true"){//Ini for Tuesday
				
		if($_POST['chken']=="true"){//January
				$query_Insert= "INSERT INTO cit_distribucion (codigo,iddistribucionmed,idtipocon,idrangohoras,idempleado,primera,subsecuente,mes,yrs,dia,IdUsuarioReg,FechaHoraReg) VALUES ('0',concat('M','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',1,'".$_POST['txtyr']."',3,'0','$fechareg')";
				$query = @mysql_query($query_Insert) or die('La consulta falloó: ' . @mysql_error());
			}	
			if($_POST['chkfb']=="true"){//February
				$query_Insert= "INSERT INTO cit_distribucion (codigo,iddistribucionmed,idtipocon,idrangohoras,idempleado,primera,subsecuente,mes,yrs,dia,IdUsuarioReg,FechaHoraReg) VALUES ('0',concat('M','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',2,'".$_POST['txtyr']."',3,'0','$fechareg')";
				$query = @mysql_query($query_Insert) or die('La consulta falloó: ' . @mysql_error());
			}
			if($_POST['chkmz']=="true"){//March
				$query_Insert= "INSERT INTO cit_distribucion (codigo,iddistribucionmed,idtipocon,idrangohoras,idempleado,primera,subsecuente,mes,yrs,dia,IdUsuarioReg,FechaHoraReg) VALUES ('0',concat('M','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',3,'".$_POST['txtyr']."',3,'0','$fechareg')";
				$query = @mysql_query($query_Insert) or die('La consulta falloó: ' . @mysql_error());
			}
 
	}

Open in new window

Avatar of HeroGuran
HeroGuran
Flag of Sweden image

Try removing @ at the mysql_query.
If you have a faulty insertion you won't be able to se that with your current settings.

Second, the code doesn't make much sense to me, probably because I don't know the language you have named your variables and database fields in. Makes it real hard to read.

So far I can only reccomend you print checkpoints in all if-statements to see that they are entered, and then check the SQL queries.

Regards,
Andreas
Avatar of jaltd
jaltd

post this into the top of your page to test if you are receiving your values correctly, this helps me when im having troubles like your.
<?
foreach($HTTP_POST_VARS as $key => $value){
 
	echo "$key = $value <BR>";
 
}
?>

Open in new window

Avatar of IvetteM

ASKER

Ok I have made some changes in my code now i have created an array of all the post of month like this:


But stills it just enters the the values for the first month wich its value is checked to true. And i have added an alert to see if my post are send correctly and they have.
$corte=substr($_POST['idmed'] ,-4, strlen($_POST['idmed'] )-0);
$fechareg = date("Y-m-d h:i:s");
	
$months = array($_POST['chkjanuary'],$_POST['chkfebruary'],$_POST['chkmarch'],$_POST['chkapril'],$_POST['chkmay'],$_POST['chkjune'],$_POST['chkjuly'],$_POST['chkagoust'],$_POST['chkseptember'],$_POST['chkoctober'],$_POST['chknovember'],$_POST['chkdecember'],$_POST['chkall']);
	
		
	$tamano=sizeof($months);
 
	if($con->conexion()==true){
	
	for ($i=0;$i<$tamano;$i++){
		if($months[$i]=="true"){
		
			if($_POST['chkmonday']=="true"){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES ('0',concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',2,'0','$fechareg')";
				$query = mysql_query($query_Insert) or die('Query Trouble' . @mysql_error());
			}
			if($_POST['chktuesday']=="true"){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES ('0',concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',3,'0','$fechareg')";
				$query = mysql_query($query_Insert) or die('Query Trouble' . @mysql_error());
			}
			if($_POST['chkwednesday']=="true"){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES ('0',concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',4,'0','$fechareg')";
				$query = mysql_query($query_Insert) or die('Query Trouble' . @mysql_error());
			}
			if($_POST['chkthusday']=="true"){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES ('0',concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',5,'0','$fechareg')";
				$query = mysql_query($query_Insert) or die('Query Trouble' . @mysql_error());
			}
			if($_POST['chkfriday']=="true"){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES ('0',concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',6,'0','$fechareg')";
				$query = mysql_query($query_Insert) or die('Query Trouble' . @mysql_error());
			}
		}
	}
	
	}

Open in new window

Avatar of hielo
Try this:
$corte=substr($_POST['idmed'] ,-4, strlen($_POST['idmed'] )-0);
$fechareg = date("Y-m-d h:i:s");
	
$months = array('chkjanuary','chkfebruary','chkmarch','chkapril','chkmay','chkjune','chkjuly','chkagoust','chkseptember','chkoctober','chknovember','chkdecember','chkall');
	
		
	$tamano=sizeof($months);
 
	if($con->conexion()==true){
	
	for ($i=0;$i<$tamano;$i++){
		if( isset($_POST[$months[$i]]) ){
		
			if($_POST['chkmonday']){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES (NULL,concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',2,'0','$fechareg')";
				$query = mysql_query($query_Insert) or die('Query Trouble' . @mysql_error());
			}
			if($_POST['chktuesday']){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES (NULL,concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',3,'0','$fechareg')";
				$query = mysql_query($query_Insert) or die('Query Trouble' . @mysql_error());
			}
			if($_POST['chkwednesday']){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES (NULL,concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',4,'0','$fechareg')";
				$query = mysql_query($query_Insert) or die('Query Trouble' . @mysql_error());
			}
			if($_POST['chkthusday']){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES (NULL,concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',5,'0','$fechareg')";
				$query = mysql_query($query_Insert) or die('Query Trouble' . @mysql_error());
			}
			if($_POST['chkfriday']){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES (NULL,concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',6,'0','$fechareg')";
				$query = mysql_query($query_Insert) or die('Query Trouble' . @mysql_error());
			}
		}
	}
	
	}
 

Open in new window

Avatar of IvetteM

ASKER

My variable $months wich is the array i create when i received my posts of all the checkboxes, so if i make an isset of the post $months it wont work is generated at the moment i received parameters from the ajax file. Hielo! Thanks anyways
if( isset($_POST[$months[$i]]) )
is equivalent to:

if( isset( $_POST['chkjanuary'] ) )

when $i==0. The same concept for the rest of the month names.

Lo tratastes?
Avatar of IvetteM

ASKER

Yes i did try it, but it doesnt do anything.
Verify that the sql being executed is correct. I modified the code below. IF the INSERT sql is correct, the other question is: What's your primary field?
$corte=substr($_POST['idmed'] ,-4, strlen($_POST['idmed'] )-0);
$fechareg = date("Y-m-d h:i:s");
	
$months = array($_POST['chkjanuary'],$_POST['chkfebruary'],$_POST['chkmarch'],$_POST['chkapril'],$_POST['chkmay'],$_POST['chkjune'],$_POST['chkjuly'],$_POST['chkagoust'],$_POST['chkseptember'],$_POST['chkoctober'],$_POST['chknovember'],$_POST['chkdecember'],$_POST['chkall']);
	
		
	$tamano=sizeof($months);
 
	if($con->conexion()==true){
	
	for ($i=0;$i<$tamano;$i++){
		if($months[$i]=="true"){
		
			if($_POST['chkmonday']=="true"){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES (NULL,concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',2,'0','$fechareg')";
				echo('Mon: '.$query_Insert);
				$query = mysql_query($query_Insert) or die('Query Trouble' . mysql_error());
			}
			if($_POST['chktuesday']=="true"){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES (NULL,concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',3,'0','$fechareg')";
				echo('Tue: '.$query_Insert);
				$query = mysql_query($query_Insert) or die('Query Trouble' . mysql_error());
			}
			if($_POST['chkwednesday']=="true"){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES (NULL,concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',4,'0','$fechareg')";
				echo('Wed: '.$query_Insert);
				$query = mysql_query($query_Insert) or die('Query Trouble' . mysql_error());
			}
			if($_POST['chkthusday']=="true"){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES (NULL,concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',5,'0','$fechareg')";
				echo('Thu: '.$query_Insert);
				$query = mysql_query($query_Insert) or die('Query Trouble' . mysql_error());
			}
			if($_POST['chkfriday']=="true"){
			$query_Insert= "INSERT INTO cit_distribution (Id,Iddistribuciotion,IdType,IdTime,IdEmployee,First,Second,Month,Year,Day,IdUserReg,DateTimeReg) VALUES (NULL,concat('L','$corte'),'EXT','".$_POST['txtrgs']."','".$_POST['idmed']."','".$_POST['cantprim']."','".$_POST['cantsub']."',$i,'".$_POST['txtyr']."',6,'0','$fechareg')";
				echo('Fri: '.$query_Insert);
				$query = mysql_query($query_Insert) or die('Query Trouble' . mysql_error());
			}
		}
	}
	
	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jaltd
jaltd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial