problemas al ingresar datos con php

Iniciado por ::: Devil :::, 17 Julio 2014, 20:21 PM

0 Miembros y 1 Visitante están viendo este tema.

::: Devil :::

hola buenas tardes estoy tratando de ingresar unos datos a mi base de datos la cual lleva como nombre carrito

tiene carrito producto y usuarios

con los campos

id nombre codigo nota valor estado

entonces tengo esto

conexion.php

Código (php) [Seleccionar]
<?php
$conexion 
mysql_connect("localhost""root""") or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_select_db("carrito"$conexion);
?>


insert1.php

Código (php) [Seleccionar]
<?php
require("conexion.php");
// insertarmos el registro
mysql_query("INSERT INTO carrito (codigo, nombre, nota, valor, estado) VALUES ('Apple', '1 Infinite Loop, Cupertino', 899610)");
// obtenemos el ID del registro
echo mysql_insert_id();
?>


insert2.php

Código (php) [Seleccionar]
<?php
require("conexion.php");
require(
"funciones.php");
$status "";

if (isset(
$_POST["codigo"])) {
$nombre sqlValue($_POST["codigo"], "text");
$direccion sqlValue($_POST["nombre"], "text");
$telefono sqlValue($_POST["nota"], "text");
$direccion sqlValue($_POST["valor"], "text");
$direccion sqlValue($_POST["estado"], "text");

$sql "INSERT INTO producto (codigo, nombre, nota, valor, estado) ";
    
$sql.= "VALUES ('".$codigo."', '".$nombre."', '".$nota.", '".$valor.", '".$estado."')";

echo $sql;

mysql_query($sql$conexion);
$status "ok";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHP con MySQL: Insertar datos en MySQL</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h3>Nueva Empresa</h3>
<?php if ($status == "ok") { ?>
<p class="confirm">Registro guardado correctamente</p>
<?php ?>
<form method="post" id="frEmpresa" action="insert-demo2.php">
<label for="nombre">codigo</label>
    <input type="text" id="nombre" name="nombre" />
    <br />
    <label for="direccion">nombre</label>
    <input type="text" id="direccion" name="direccion" />
    <br />
    <label for="telefono">nota</label>
    <input type="text" id="telefono" name="telefono" />
    <br />
    <label for="telefono">valor</label>
    <input type="text" id="telefono" name="telefono" />
    <br />
    <label for="telefono">estado</label>
    <input type="text" id="telefono" name="telefono" />
    <br />
    <label for="bts">&nbsp;</label>
    <button type="submit">Guardar</button>
    <button type="reset">Limpiar</button>
</form>
</body>
</html>


funciones.php

Código (php) [Seleccionar]
<?php
function getParam($param$default) {
$result $default;
if (isset($param)) {
  
$result = (get_magic_quotes_gpc()) ? $param addslashes($param);
}
return $result;
}
function 
sqlValue($value$type) {
  
$value get_magic_quotes_gpc() ? stripslashes($value) : $value;
  
$value function_exists("mysql_real_escape_string") ? mysql_real_escape_string($value) : mysql_escape_string($value);
  switch (
$type) {
    case 
"text":
      
$value = ($value != "") ? "'" $value "'" "NULL";
      break;
    case 
"int":
      
$value = ($value != "") ? intval($value) : "NULL";
      break;
    case 
"double":
      
$value = ($value != "") ? "'" doubleval($value) . "'" "NULL";
      break;
    case 
"date":
      
$value = ($value != "") ? "'" $value "'" "NULL";
      break;
  }
  return 
$value;
}
?>


no me da error en ningun lado pero cuando le doy agregar pareciera que los agrega y limpia el formulario pero nada que ver no ingresa los datos
NI TU NI NADIEN PORDRAN CONMIGO SOY MAS FUERTE QUE LA ENVIDIA.

ESTO ESTA DEDICADO A LOS QUE ME JODEN LA VIDA

MinusFour

¿Tienes un formulario de datos de personas y estas metiendo los datos a la tabla producto? ¿Eso está bien?

Tienes variables que ni siquiera están ahí, $nota, $valor, $estado, $codigo. Nunca se declararon ni nada y tienes $dirección asignando valores multiples veces (borrando lo que haya tenido antes de su nueva asignación).

::: Devil :::

sas hay estoy en 0, no tendras un ejemplo por hay no tan complicado que me pases por fa
NI TU NI NADIEN PORDRAN CONMIGO SOY MAS FUERTE QUE LA ENVIDIA.

ESTO ESTA DEDICADO A LOS QUE ME JODEN LA VIDA

MinusFour

Código (php) [Seleccionar]

if (isset($_POST["codigo"])) {
$codigo = sqlValue($_POST["codigo"], "text");
$nombre = sqlValue($_POST["nombre"], "text");
$nota = sqlValue($_POST["nota"], "text");
$valor = sqlValue($_POST["valor"], "text");
$estado = sqlValue($_POST["estado"], "text");

$sql = "INSERT INTO tabla (campo1, campo2, campo3, campo4, campo5) ";
       $sql.= "VALUES ('".$codigo."', '".$nombre."', '".$nota.", '".$valor.", '".$estado."')";

echo $sql;

mysql_query($sql, $conexion);
$status = "ok";
}
?>


Presta mucha atención a:

Código (sql) [Seleccionar]

INSERT INTO tabla (campo1, campo2, campo3, campo4, campo5)


Tiene que hacer match con lo que tienes en tu base de datos (la tabla a la que quieres guardar los datos, y los campos respectivos de esa tabla)

::: Devil :::

bueno ve acabo de modificar todo mira como lo tengo

conexion.php

Código (php) [Seleccionar]
<?php
$conexion 
mysql_connect("localhost""root""") or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_select_db("carrito"$conexion);
?>



funciones.php

Código (php) [Seleccionar]
<?php
function getParam($param$default) {
$result $default;
if (isset($param)) {
  
$result = (get_magic_quotes_gpc()) ? $param addslashes($param);
}
return $result;
}
function 
sqlValue($value$type) {
  
$value get_magic_quotes_gpc() ? stripslashes($value) : $value;
  
$value function_exists("mysql_real_escape_string") ? mysql_real_escape_string($value) : mysql_escape_string($value);
  switch (
$type) {
    case 
"text":
      
$value = ($value != "") ? "'" $value "'" "NULL";
      break;
    case 
"int":
      
$value = ($value != "") ? intval($value) : "NULL";
      break;
    case 
"double":
      
$value = ($value != "") ? "'" doubleval($value) . "'" "NULL";
      break;
    case 
"date":
      
$value = ($value != "") ? "'" $value "'" "NULL";
      break;
  }
  return 
$value;
}
?>


insert-demo1.php

Código (php) [Seleccionar]
<?php
require("conexion.php");
// insertarmos el registro
mysql_query("INSERT INTO producto (codigo, nombre, nota) VALUES ('Apple', '1 Infinite Loop, Cupertino', 899610)");
// obtenemos el ID del registro
echo mysql_insert_id();
?>



insert-demo2.php

Código (php) [Seleccionar]
<?php
require("conexion.php");
$status "";
if (isset(
$_POST["codigo"])) {
$codigo $_POST["codigo"];
$nombre $_POST["nombre"];
$nota $_POST["nota"];

$sql "INSERT INTO producto (codigo, nombre, nota) ";
    
$sql.= "VALUES ('".$codigo."', '".$nombre."', '".$nota."')";

mysql_query($sql$conexion);
$status "ok";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHP con MySQL: Insertar datos en MySQL</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h3>Nueva Empresa</h3>
<?php if ($status == "ok") { ?>
<p class="confirm">Registro guardado correctamente</p>
<?php ?>
<form method="post" id="frEmpresa" action="insert-demo2.php">
<label for="nombre">Nombre</label>
    <input type="text" id="nombre" name="nombre" />
    <br />
    <label for="direccion">Direcci&oacute;n</label>
    <input type="text" id="direccion" name="direccion" />
    <br />
    <label for="telefono">Telefono</label>
    <input type="text" id="telefono" name="telefono" />
    <br />
    <label for="bts">&nbsp;</label>
    <button type="submit">Guardar</button>
    <button type="reset">Limpiar</button>
</form>
</body>
</html>



insert-demo3.php

Código (php) [Seleccionar]
<?php
require("conexion.php");
require(
"funciones.php");
$status "";

if (isset(
$_POST["codigo"])) {
$nombre sqlValue($_POST["codigo"], "text");
$direccion sqlValue($_POST["nombre"], "text");
$telefono sqlValue($_POST["nota"], "text");

$sql "INSERT INTO producto (codigo, nombre, nota) ";
    
$sql.= "VALUES ('".$codigo."', '".$nombre."', '".$nota."')";

echo $sql;

mysql_query($sql$conexion);
$status "ok";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHP con MySQL: Insertar datos en MySQL</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h3>Nueva Empresa</h3>
<?php if ($status == "ok") { ?>
<p class="confirm">Registro guardado correctamente</p>
<?php ?>
<form method="post" id="frEmpresa" action="insert-demo3.php">
<label for="nombre">Nombre</label>
    <input type="text" id="nombre" name="nombre" />
    <br />
    <label for="direccion">Direcci&oacute;n</label>
    <input type="text" id="direccion" name="direccion" />
    <br />
    <label for="telefono">Telefono</label>
    <input type="text" id="telefono" name="telefono" />
    <br />
    <label for="bts">&nbsp;</label>
    <button type="submit">Guardar</button>
    <button type="reset">Limpiar</button>
</form>
</body>
</html>



lo acomode asi no le ingrese mas campos solo esos 3 para probar pero nada todavia sigue sin ingresar campos
NI TU NI NADIEN PORDRAN CONMIGO SOY MAS FUERTE QUE LA ENVIDIA.

ESTO ESTA DEDICADO A LOS QUE ME JODEN LA VIDA

MinusFour

Cambia:

Código (php) [Seleccionar]

mysql_query($sql, $conexion);


por

Código (php) [Seleccionar]

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


Y vuelve a intentar y a ver si te sale un error.

::: Devil :::

no me da error ni tampoco me agrega nada a la base de datos
NI TU NI NADIEN PORDRAN CONMIGO SOY MAS FUERTE QUE LA ENVIDIA.

ESTO ESTA DEDICADO A LOS QUE ME JODEN LA VIDA

MinusFour

No creo que entiendas como funcionan las variables POST.

Cuando tu tienes un formulario que tiene un metodo post, el servidor recoge los inputs y los pone en la variable superglobal $_POST.

Por ejemplo:

Código (html4strict) [Seleccionar]
<input type="text" id="direccion" name="direccion" />

El servidor recoge el input de nombre (name en Inglés) dirección y lo pone en $_POST para que lo accedas así: $_POST["direccion"]

Tus dos códigos no funcionan por esto:

Código (php) [Seleccionar]

if (isset($_POST["codigo"]))


Este código basicamente dice: Si se envio algún input de nombre codigo...

ejecuta:

Código (php) [Seleccionar]

$codigo = $_POST["codigo"];
$nombre = $_POST["nombre"];
$nota = $_POST["nota"];

$sql = "INSERT INTO producto (codigo, nombre, nota) ";
   $sql.= "VALUES ('".$codigo."', '".$nombre."', '".$nota."')";

mysql_query($sql, $conexion);
$status = "ok";


Ahora, la pregunta del millón de dolares... ¿Tu crees que se ejecute este código?

::: Devil :::

ya mi pana no te arreches con decirme cambiale el nombre a las cajas de texto valia jajajajaja  ;D
NI TU NI NADIEN PORDRAN CONMIGO SOY MAS FUERTE QUE LA ENVIDIA.

ESTO ESTA DEDICADO A LOS QUE ME JODEN LA VIDA

MinusFour

Cita de: ::: Devil ::: en 17 Julio 2014, 22:13 PM
ya mi pana no te arreches con decirme cambiale el nombre a las cajas de texto valia jajajajaja  ;D

Solo recuerda que si le cambias el nombre a las cajas de texto, los datos se almacenan en lugares diferentes:

Por ejemplo si tienes:

Código (html4strict) [Seleccionar]
<input type="text" id="direccion" name="direccion" />

Y lo cambias a :

Código (html4strict) [Seleccionar]
<input type="text" id="direccion" name="codigo" />

Código (php) [Seleccionar]
$_POST["direccion"]; //deja de existir
$_POST["codigo"]; //Ahora existe este