Jump to content

[SOLVED] Won´t insert any field in a table


ferpadro

Recommended Posts

Ok, guys, you are my last hope now. I´ve been wandering most of the night trying to find what i did wrong but couldn´t find and answer. Here is what happens: im using a function called "cargar_horas" that should insert 6 values in a table, but for some reason the query is not working. I echoed most of my code to check the values of my variables and they are fine. I think it must be something beyond my knowledge, so i hope someone can figure out what is this.

 

(First of all, sorry but the scripts are in spanish. I can translate them to english if anyone wants to)

 

-------------------------------------------------------------------------------------------

usuario.php:

 

<?php

 

include('config.inc.php');

include('funciones.usuario.php');

 

require('session.php');

 

$val = check_in($_SESSION['usuario'], $_SESSION['contrasena'], $tab);

 

if (strcmp($val,"usuario") != 0) {

 

cambiar("index.php");

 

return;

 

}

 

else

{

?>

<html>

<head>

<form method="post" action="">

<div style="text-align: center;"><big style="font-style: italic;"><big><big><big>-.USUARIO.-</big></big></big></big>

   <br><br>

 

1) Cargar horas trabajadas </br></br>

Fecha del servicio:

Dia:<select name="diai" class="input" id="diai">

 

              <?

 

  for ($i = 1; $i < 32; $i++)

 

{

 

if ($i < 10)

 

echo '<option value="0'.$i.'">0'.$i.'</option>';

 

else

 

echo '<option value="'.$i.'">'.$i.'</option>';

 

}

 

  ?> 

</select>

Mes:<select name="mesi" class="input" id="mesi">

 

              <?

 

  for ($i = 1; $i < 13; $i++)

 

{

 

if ($i < 10)

 

echo '<option value="0'.$i.'">0'.$i.'</option>';

 

else

 

echo '<option value="'.$i.'">'.$i.'</option>';

 

}

 

  ?>

</select>

Ano:<select name="anoi" class="input" id="anoi">

 

              <?

 

  for ($i = 2007; $i < 3000; $i++)

 

{

 

if ($i < 10)

 

echo '<option value="0'.$i.'">0'.$i.'</option>';

 

else

 

echo '<option value="'.$i.'">'.$i.'</option>';

 

}

  ?>

</select><br></br>

<?

$mes = $_POST['mesi'];

$dia = $_POST['diai'];

$ano = $_POST['anoi'];

$fechafinal = $ano."-".$mes."-".$dia;

?>

Hora de comienzo del servicio:

<select name="horaservicio" class="input" id="horaservicio">

 

              <?

 

  for ($i = 0; $i < 24; $i++)

 

{

 

if ($i < 10)

 

echo '<option value="0'.$i.'">0'.$i.'</option>';

 

else

 

echo '<option value="'.$i.'">'.$i.'</option>';

 

}

 

  ?> 

</select><big><big>:</big></big>

<select name="minutoservicio" class="input" id="minutoservicio">

 

              <?

 

  for ($i = 0; $i < 60; $i++)

 

{

 

if ($i < 10)

 

echo '<option value="0'.$i.'">0'.$i.'</option>';

 

else

 

echo '<option value="'.$i.'">'.$i.'</option>';

 

}

 

  ?> 

</select></br></br>

 

<?

 

$hora = $_POST['horaservicio'];

$minuto = $_POST['minutoservicio'];

$horafinal = $hora.":".$minuto;

?>

 

Cliente: <select name="cliente">

<?

  $sql= "SELECT * FROM cliente";

 

  $consulta=mysql_query($sql);

 

  while($array = mysql_fetch_array($consulta))

 

  {

 

  echo "<option value=$array[0]>$array[1]</option>";

 

  }

 

?>

</select></br></br>

<?

$cliente = $_POST['cliente'];

?>

Tipo de servicio: <select name="tiposervicio">

<?

  $sql= "SELECT * FROM tiposervicio";

 

  $consulta=mysql_query($sql);

 

  while($array = mysql_fetch_array($consulta))

 

  {

 

  echo "<option value=$array[0]>$array[1]</option>";

 

  }

 

?>

</select></br></br>

<?

$tipo = $_POST['tiposervicio'];

?>

Cantidad horas: <input name="cantidadhoras" type="text"><br><br>

<?

$horas = $_POST['cantidadhoras'];

?>

Observacion: <input name="observacion" type="text"><br><br>

<?

$observa = $_POST['observacion'];

?>

<input name="opcion" type="submit" value=Cargar><br><br>

<?

/*echo "$tipo";

echo "</br>";

echo "$cliente";

echo "</br>";

echo "$fechafinal";

echo "</br>";

echo "$horafinal";

echo "</br>";

echo "$horas";

echo "</br>";

echo "$observa";

echo "</br>";*/

 

$opcion=$_POST['opcion'];

switch($opcion)

 

{

 

case "Cargar":

 

{

cargar_horas($tipo,$cliente,$fechafinal,$horafinal,$horas,$observa);

break;

 

}

 

}

 

include('pie.php');

 

}

 

 

?>

 

----------------------------------------------------------------------------------------------------------------

 

And this is the file with the function that should do the insert:

 

<?

function cargar_horas($tipo,$cliente,$fechafinal,$horafinal,$horas,$observa)

{

$sql="INSERT INTO servicio VALUES ('','$_SESSION[id]','$tipo','$cliente','$fechafinal','$horafinal','$horas','$observa')";

mysql_query($sql);

echo "Informacion cargada con exito";

echo "</br>";

echo "$tipo";

echo "</br>";

echo "$cliente";

echo "</br>";

echo "$fechafinal";

echo "</br>";

echo "$horafinal";

echo "</br>";

echo "$horas";

echo "</br>";

echo "$observa";

}

?>

 

----------------------------------------------------------------------------------------------------------------

 

the "servicio" table has 8 fields:

 

idservicio

idtecnico

idtiposervicio

idcliente

fecha

hora

horas

observacion

 

Please forgive me about writing such a long post. If anyone finds the error and is able to post it here, it would be a great help.

Thanks in advance

 

a tip. Whenever there is a problem with mysql_query(), check mysql_error() like so:

 

mysql_query($sql) or die(mysql_error());

 

Now: Where are you connecting to the database server and selecting the database? I don't see it anywhere, and mysql_error() is probably going to complain about an invalid resource.

That would be config.inc.php:

 

<?
$connection = "Local";

$dbhost = "localhost"; 
$dbuser = "root"; //usuarios de la base de datos
$dbpass = "*******"; //contraseña de la base de datos
$db = "tecnicos"; //nombre de la base de datos
$tabla="usuario";
$tab=$tabla;

if (!($conectar=mysql_connect($dbhost,$dbuser,$dbpass)))
                {
                        echo "No puede conectar al Motor";
                        die();
                }

else
          		{
                    if(!(mysql_select_db($db, $conectar)))
                                {
				echo "No puede conectar a la base de datos $db"; 
				die();

			        }
	            }

function check_in($user, $pass, $tab)
{
	$sql= "select nombre,password,rol from $tab where (nombre= '$user' and password= '$pass')";
	$conectar=mysql_query($sql);
	if ($datos=mysql_fetch_array($conectar))
	{
		if (($datos['nombre'] == $user) && ($datos['password'] == $pass))
		{
			$rol=$datos['rol'];
			return $rol;
		}
		else
			echo "No concuerdan los datos pasados con la base de datos";
	}
	else
		return 0;	
}

function cambiar($dire) {

    echo '
    <script LANGUAGE="JavaScript">
var pagina="'.$dire.'"
function redireccionar()
{
location.href=pagina
}
setTimeout ("redireccionar()", 500);
</script>
    ';
}
?>

 

 

 

Sorry again for the spanish. I`ll try with the tip you gave me, thanks a lot

 

Mod. edit: PLEASE post code between [ code] tags. Please remove some of the whitespace in it.

 

Nevermind i solved it. I was inserting rows all with id = 0 (because i forgot to add the auto_increment property) and that generated duplicate entries for my composite key.

BlueSkyIS, you helped me a lot. Without mysql_error function im sure i would never find the error. Thank you a lot! 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.