Jump to content

[SOLVED] Dropdown--Query problem


davich64

Recommended Posts

Hello i'm new to the forum and to php and im building a database-php app, my problem is that im inserting data to the database(mysql) from a form with several textfield and a dynamic dropdown menu, the textfields post the data correctly but the dropdown doesnt seem to be sending any value. the database executes the query but the field from the select is blank. here's my code:

 

<select name="delegacion">

<?php

$con = mysql_connect("localhost","database_usernam","database_password");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("fre", $con);

 

$sql = "SELECT id_del, delegacion FROM delegaciones ORDER BY delegacion";

 

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

 

$rs = mysql_query($sql);

 

while($row = mysql_fetch_array($rs))

{

  echo "<option value=\"".$row['id']."\">".$row['delegacion']."\n  ";

 

}

?>

 

</select>

 

and the querry:

 

<?php

$con = mysql_connect("localhost","database_username","database_password");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("fre", $con);

 

$sql="INSERT INTO propiedades (id,cliente,tipo,terreno,construccion,estacionamientos,habitaciones,baños,jardin,c_serv,niveles,telefono,calle,numero,colonia,delegacion,precio,asesor,comentarios) VALUES(' ','$_POST[cliente]','$_POST[tipo]','$_POST[terreno]','$_POST[construccion]','$_POST[estacionamientos]','$_POST[habitaciones]','$_POST[baños]','$_POST[jardin]','$_POST[c_serv]','$_POST[niveles]','$_POST[telefono]','$_POST[calle]','$_POST[numero]','$_POST[colonia]', '$_POST[delegacion]','$_POST[precio]','$_POST[asesor]','$_POST[comentario]')";

 

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

 

 

$str = "<b>OK</b>";

echo ($str);

 

mysql_close($con)

?>

 

any ideas what im doing wrong? i thought this should work just fine, ive used similar code in jsp but now it doesnt work and i cant figure out why.

 

 

 

Link to comment
Share on other sites

Where are your values?

 

<?php
$con = mysql_connect("localhost","database_username","database_password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("fre", $con);

$sql="INSERT INTO propiedades (id,cliente,tipo,terreno,construccion,estacionamientos,habitaciones,baños,jardin,c_serv,niveles,telefono,calle,numero,colonia,delegacion,precio,asesor,comentarios) VALUES(' ','$_POST[cliente]','$_POST[tipo]','$_POST[terreno]','$_POST[construccion]','$_POST[estacionamientos]','$_POST[habitaciones]','$_POST[baños]','$_POST[jardin]','$_POST[c_serv]','$_POST[niveles]','$_POST[telefono]','$_POST[calle]','$_POST[numero]','$_POST[colonia]', '$_POST[delegacion]','$_POST[precio]','$_POST[asesor]','$_POST[comentario]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }


$str = "OK";
echo ($str);

mysql_close($con)
?>

 

post the full script for the query, and your form.

 

 

Link to comment
Share on other sites

ok sorry. here it is:

<!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=iso-8859-1" />
<title>Nuevas Propiedades</title>
</head>

<table width="800" border="10" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="alta_success.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Nueva Propiedad: </strong></td>
</tr>
<tr>
<td width="78">Cliente</td>
<td width="6">:</td>
<td width="294"><input name="cliente" type="text" id="cliente"></td>
</tr>
<tr>
<td>Tipo</td>
<td>:</td>
<td><input name="tipo" type="text" id="tipo"></td>
</tr>

<tr>

</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>

<tr>
<td width="78">M2 de Terreno:</td>
<td width="6">:</td>
<td width="294"><input name="terreno" type="text" id="terreno"></td>


<td width="78">M2 de Construcción:</td>
<td width="6">:</td>
<td width="294"><input name="construccion" type="text" id="construccion"></td>
</tr>



<tr>
<td width="78">Número de Habitaciones:</td>
<td width="6">:</td>
<td width="294"><input name="habitaciones" type="text" id="habitaciones"></td>


<td width="78">Número de Baños:</td>
<td width="6">:</td>
<td width="294"><input name="baños" type="text" id="baños"></td>
</tr>

<tr>
<td width="78">Jardín:</td>
<td width="6">:</td>
<td width="294"><input name="jardin" type="text" id="jardin"></td>


<td width="78">Cuarto de Servicio:</td>
<td width="6">:</td>
<td width="294"><input name="c_serv" type="text" id="c_serv"></td>
</tr>

<tr>
<td width="78">Número de Niveles:</td>
<td width="6">:</td>
<td width="294"><input name="niveles" type="text" id="niveles"></td>

<td width="78">Número de Estacionamientos:</td>
<td width="6">:</td>
<td width="294"><input name="estacionamientos" type="text" id="estacionamientos"></td>
</tr>

<tr>
<td width="78">Número de Líneas Telefónicas:</td>
<td width="6">:</td>
<td width="294"><input name="telefono" type="text" id="telefono"></td>
</tr>

<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>


<tr>
<td width="78">Calle:</td>
<td width="6">:</td>
<td width="294"><input name="calle" type="text" id="calle"></td>

<td width="78">Número:</td>
<td width="6">:</td>
<td width="294"><input name="numero" type="text" id="numero"></td>
</tr>

<tr>
<td width="78">Colonia:</td>
<td width="6">:</td>
<td width="294"><input name="colonia" type="text" id="colonia"></td>

<td width="78">Delegación:</td>
<td width="6">:</td>
<td width="294"><select name="delegacion" id="delegacion">
<?php
$con = mysql_connect("localhost","database_username","database_password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("fre", $con);

$sql = "SELECT id_del, delegacion FROM delegaciones ORDER BY delegacion";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['id']."\">".$row['delegacion']."\n  ";

}
?>
</select></td>
</tr>

<tr>
<td width="78">Precio:</td>
<td width="6">:</td>
<td width="294"><input name="precio" type="text" id="precio"></td>
</tr>

<tr>
<td width="78">Asesor:</td>
<td width="6">:</td>
<td width="294"><input name="asesor" type="text" id="asesor"></td>
</tr>

<tr>
<td width="78">Comentarios:</td>
<td width="6">:</td>
<td width="294"><input name="comentario" type="text" id="comentario"></td>
</tr>

<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Guardar"></td>

<?php
$con = mysql_connect("localhost","database_username","database_password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("fre", $con);

$sql="INSERT INTO propiedades (id,cliente,tipo,terreno,construccion,estacionamientos,habitaciones,baños,jardin,c_serv,niveles,telefono,calle,numero,colonia,delegacion,precio,asesor,comentarios) VALUES(' ','$_POST[cliente]','$_POST[tipo]','$_POST[terreno]','$_POST[construccion]','$_POST[estacionamientos]','$_POST[habitaciones]','$_POST[baños]','$_POST[jardin]','$_POST[c_serv]','$_POST[niveles]','$_POST[telefono]','$_POST[calle]','$_POST[numero]','$_POST[colonia]', '$_POST[delegacion]','$_POST[precio]','$_POST[asesor]','$_POST[comentario]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
  
  
$str = "<b>Propiedad Agregada Exitosamente</b>";
echo ($str);

mysql_close($con)
?>

</tr>
</table>
</td>
</form>
</tr>
</table>



<body>
</body>
</html>

Link to comment
Share on other sites

Try

 

<!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=iso-8859-1" />
<title>Nuevas Propiedades</title>
</head>

<table width="800" border="10" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="alta_success.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Nueva Propiedad: </strong></td>
</tr>
<tr>
<td width="78">Cliente</td>
<td width="6">:</td>
<td width="294"><input name="cliente" type="text" id="cliente"></td>
</tr>
<tr>
<td>Tipo</td>
<td>:</td>
<td><input name="tipo" type="text" id="tipo"></td>
</tr>

<tr>

</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>

<tr>
<td width="78">M2 de Terreno:</td>
<td width="6">:</td>
<td width="294"><input name="terreno" type="text" id="terreno"></td>


<td width="78">M2 de Construcción:</td>
<td width="6">:</td>
<td width="294"><input name="construccion" type="text" id="construccion"></td>
</tr>



<tr>
<td width="78">Número de Habitaciones:</td>
<td width="6">:</td>
<td width="294"><input name="habitaciones" type="text" id="habitaciones"></td>


<td width="78">Número de Baños:</td>
<td width="6">:</td>
<td width="294"><input name="baños" type="text" id="baños"></td>
</tr>

<tr>
<td width="78">Jardín:</td>
<td width="6">:</td>
<td width="294"><input name="jardin" type="text" id="jardin"></td>


<td width="78">Cuarto de Servicio:</td>
<td width="6">:</td>
<td width="294"><input name="c_serv" type="text" id="c_serv"></td>
</tr>

<tr>
<td width="78">Número de Niveles:</td>
<td width="6">:</td>
<td width="294"><input name="niveles" type="text" id="niveles"></td>

<td width="78">Número de Estacionamientos:</td>
<td width="6">:</td>
<td width="294"><input name="estacionamientos" type="text" id="estacionamientos"></td>
</tr>

<tr>
<td width="78">Número de Líneas Telefónicas:</td>
<td width="6">:</td>
<td width="294"><input name="telefono" type="text" id="telefono"></td>
</tr>

<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>


<tr>
<td width="78">Calle:</td>
<td width="6">:</td>
<td width="294"><input name="calle" type="text" id="calle"></td>

<td width="78">Número:</td>
<td width="6">:</td>
<td width="294"><input name="numero" type="text" id="numero"></td>
</tr>

<tr>
<td width="78">Colonia:</td>
<td width="6">:</td>
<td width="294"><input name="colonia" type="text" id="colonia"></td>

<td width="78">Delegación:</td>
<td width="6">:</td>
<td width="294"><select name="delegacion" id="delegacion">
<?php
$con = mysql_connect("localhost","database_username","database_password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("fre", $con);

$sql = "SELECT id_del, delegacion FROM delegaciones ORDER BY delegacion";

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

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['id']."\">".$row['delegacion']."\n  ";

}
?>
</select></td>
</tr>

<tr>
<td width="78">Precio:</td>
<td width="6">:</td>
<td width="294"><input name="precio" type="text" id="precio"></td>
</tr>

<tr>
<td width="78">Asesor:</td>
<td width="6">:</td>
<td width="294"><input name="asesor" type="text" id="asesor"></td>
</tr>

<tr>
<td width="78">Comentarios:</td>
<td width="6">:</td>
<td width="294"><input name="comentario" type="text" id="comentario"></td>
</tr>

<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Guardar"></td>

<?php
$con = mysql_connect("localhost","database_username","database_password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("fre", $con);

$sql="INSERT INTO propiedades (id,cliente,tipo,terreno,construccion,estacionamientos,habitaciones,baños,jardin,c_serv,niveles,telefono,calle,numero,colonia,delegacion,precio,asesor,comentarios) VALUES(' ','$_POST[cliente]','$_POST[tipo]','$_POST[terreno]','$_POST[construccion]','$_POST[estacionamientos]','$_POST[habitaciones]','$_POST[baños]','$_POST[jardin]','$_POST[c_serv]','$_POST[niveles]','$_POST[telefono]','$_POST[calle]','$_POST[numero]','$_POST[colonia]', '$_POST[delegacion]','$_POST[precio]','$_POST[asesor]','$_POST[comentario]')";


mysql_query($sql,$con) or die(mysql_error());
  
$str = "<b>Propiedad Agregada Exitosamente</b>";
echo ($str);

mysql_close($con)
?>

</tr>
</table>
</td>
</form>
</tr>
</table>



<body>
</body>
</html>

Link to comment
Share on other sites

I know this doesn't answer your question, but here:

 

$sql="INSERT INTO propiedades (id,cliente,tipo,terreno,construccion,estacionamientos,habitaciones,baños,jardin,c_serv,niveles,telefono,calle,numero,colonia,delegacion,precio,asesor,comentarios) VALUES(' ','$_POST[cliente]','$_POST[tipo]','$_POST[terreno]','$_POST[construccion]','$_POST[estacionamientos]','$_POST[habitaciones]','$_POST[baños]','$_POST[jardin]','$_POST[c_serv]','$_POST[niveles]','$_POST[telefono]','$_POST[calle]','$_POST[numero]','$_POST[colonia]', '$_POST[delegacion]','$_POST[precio]','$_POST[asesor]','$_POST[comentario]')";

 

You can leave out id and the ' ' since it's most likely set to auto_increment.

Link to comment
Share on other sites

I know this doesn't answer your question, but here:

 

$sql="INSERT INTO propiedades (id,cliente,tipo,terreno,construccion,estacionamientos,habitaciones,baños,jardin,c_serv,niveles,telefono,calle,numero,colonia,delegacion,precio,asesor,comentarios) VALUES(' ','$_POST[cliente]','$_POST[tipo]','$_POST[terreno]','$_POST[construccion]','$_POST[estacionamientos]','$_POST[habitaciones]','$_POST[baños]','$_POST[jardin]','$_POST[c_serv]','$_POST[niveles]','$_POST[telefono]','$_POST[calle]','$_POST[numero]','$_POST[colonia]', '$_POST[delegacion]','$_POST[precio]','$_POST[asesor]','$_POST[comentario]')";

 

You can leave out id and the ' ' since it's most likely set to auto_increment.

 

 

Removie ID from the VALUES, and the FIELDs.

 

 

 

Link to comment
Share on other sites

Yea, sorry, like this:

 

$sql="INSERT INTO propiedades (cliente,tipo,terreno,construccion,estacionamientos,habitaciones,baños,jardin,c_serv,niveles,telefono,calle,numero,colonia,delegacion,precio,asesor,comentarios) VALUES('$_POST[cliente]','$_POST[tipo]','$_POST[terreno]','$_POST[construccion]','$_POST[estacionamientos]','$_POST[habitaciones]','$_POST[baños]','$_POST[jardin]','$_POST[c_serv]','$_POST[niveles]','$_POST[telefono]','$_POST[calle]','$_POST[numero]','$_POST[colonia]', '$_POST[delegacion]','$_POST[precio]','$_POST[asesor]','$_POST[comentario]')";

Link to comment
Share on other sites

lol i just tried without the id, and i dont get the error anymore but i still dont get the value from the select :(

 

Well no wonder

 

$sql = "SELECT id_del, delegacion FROM delegaciones ORDER BY delegacion";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['id']."\">".$row['delegacion']."\n  ";
   
}

 

 

 

You select only 2 coloumn from your database, and you have no .$row['id']. selected in the query.

 

 

 

 

You sure its not $row['id_del']

Link to comment
Share on other sites

i dont really undertand that last, why would i need a .$row['id'] in the querry shouldnt the querry read only the value i selected from the listbox(the values from the listbox are read correctly from the database)?

 

the source code looks like this without submitting it:

 

<select name="delegacion">
<option value="">Alvaro Obregón
  <option value="">Azcapotzalco
  <option value="">Benito Juárez
  <option value="">Coyoacán
  <option value="">Cuajimalpa
  <option value="">Cuauhtémoc
  <option value="">Gustavo A. Madero
  <option value="">Iztacalco
  <option value="">Iztapalapa
  <option value="">Magdalena Contreras
  <option value="">Miguel Hidalgo
  <option value="">Milpa Alta
  <option value="">Tláhuac
  <option value="">Tlalpan
  <option value="">Venustiano Carranza
  <option value="">Xochimilco
  
</select></td>

Link to comment
Share on other sites

lol i just tried without the id, and i dont get the error anymore but i still dont get the value from the select :(

 

Well no wonder

 

$sql = "SELECT id_del, delegacion FROM delegaciones ORDER BY delegacion";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['id']."\">".$row['delegacion']."\n  ";
   
}

 

 

 

You select only 2 coloumn from your database, and you have no .$row['id']. selected in the query.

 

 

 

 

You sure its not $row['id_del']

 

no,id is correct id_del is from another table to populate the select

Link to comment
Share on other sites

Yea, there's you problem.

You use $row['id'] to set your value, but you don't get id from your database.

 

If your database field is named id_del, you should use $row['id_del'] where you set your value:

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['id_del']."\">".$row['delegacion']."\n  ";
   
}

 

Link to comment
Share on other sites

Then you have to use mysql_fetch_array  the right way.

 

Also, ID has to be withtin the query on $sql.

 

try this

 

$sql = "SELECT * FROM delegaciones ORDER BY delegacion";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['id']."\">".$row['delegacion']."\n  ";
   
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.