davich64 Posted December 19, 2007 Share Posted December 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
davich64 Posted December 19, 2007 Author Share Posted December 19, 2007 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> Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 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> Quote Link to comment Share on other sites More sharing options...
xiao Posted December 19, 2007 Share Posted December 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
xiao Posted December 19, 2007 Share Posted December 19, 2007 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]')"; Quote Link to comment Share on other sites More sharing options...
davich64 Posted December 19, 2007 Author Share Posted December 19, 2007 Hmm.. no i still dont get the value from the select field, when i started the code i had it without the id in the querry but i got some weird sql error, anyway i dont think the error is in there. Quote Link to comment Share on other sites More sharing options...
davich64 Posted December 19, 2007 Author Share Posted December 19, 2007 lol i just tried without the id, and i dont get the error anymore but i still dont get the value from the select Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 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'] Quote Link to comment Share on other sites More sharing options...
xiao Posted December 19, 2007 Share Posted December 19, 2007 run the script (without submitting) and look at your sourcecode. Look if your options look like <option value="1">Delegacion1 EDIT: Oh, would've taken a little longer this way to come to the conclusion phpSensei just made Quote Link to comment Share on other sites More sharing options...
davich64 Posted December 19, 2007 Author Share Posted December 19, 2007 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> Quote Link to comment Share on other sites More sharing options...
davich64 Posted December 19, 2007 Author Share Posted December 19, 2007 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 Quote Link to comment Share on other sites More sharing options...
xiao Posted December 19, 2007 Share Posted December 19, 2007 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 "; } Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 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 "; } Quote Link to comment Share on other sites More sharing options...
davich64 Posted December 19, 2007 Author Share Posted December 19, 2007 worked, well almost, im getting the value option number instead the text Quote Link to comment Share on other sites More sharing options...
davich64 Posted December 19, 2007 Author Share Posted December 19, 2007 thanks for all the help, guess all i do now is use the id instead the actual text i got there to make the search form. cya Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.