0

Thanks in advance and sorry, but my English is not very good. I was having problems with the update of a combobox. I can make that combobox come loaded with the data from the table and to appear selected the record stored in the table, but at the time of editing the registry, the table is saved with a 0 (zero). I can't find the error.

Here's the code for the combobox for the INSERT

Laboratorio: 
<select name="lab" size="0">
    <?php
        $sql = 'SELECT * FROM laboratorios order by lab_Nombre asc';
        $res = mysql_query($sql);
        while ($array = mysql_fetch_array($res)) { ?>
            <option value="<?php echo $array['lab_Id']?>"><?php echo $array['lab_Nombre']?> </option>
    <?php } ?>
</select><br/><br/>

Here's the code for the combobox for the UPDATE

Laboratorio: 
<select name="lab" size="0">
    <?php
        $sqlLab = 'SELECT * FROM laboratorios order by lab_Nombre asc';
        $resLab = mysql_query($sqlLab);
        while ($arrayLab = mysql_fetch_array($resLab)) 
        { ?>
            <option value=<?php 
                            if ($array['ac_Lab']==$arrayLab['lab_Id'])
                            {
                                echo $arrayLab['lab_Id']?> selected="selected"><?php echo $arrayLab['lab_Nombre']?> </option>
                      <?php } 
                            else
                            { ?>
                                <option value=<?php echo $arrayLab['lab_Id']?> ><?php echo $arrayLab['lab_Nombre']?> </option>
                      <?php } ?>
    <?php } ?>
</select><br/><br/>

The table to modify is:

CREATE TABLE IF NOT EXISTS `laboratorios` (
  `lab_Id` smallint(3) NOT NULL AUTO_INCREMENT,
  `lab_Nombre` varchar(50) NOT NULL,
  `lab_Contacto` varchar(50) DEFAULT NULL,
  `lab_Mail` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`lab_Id`)
) ENGINE=MyISAM AUTO_INCREMENT=42

coming from the following table:

`ac_Id` smallint(6) NOT NULL AUTO_INCREMENT,
`ac_Desc` varchar(50) NOT NULL,
`ac_Cat` varchar(50) NOT NULL,
`ac_Lab` smallint(6) NOT NULL,
`ac_Apli` varchar(200) NOT NULL,
`ac_Prov` smallint(6) NOT NULL,
`ac_Alm` smallint(6) NOT NULL,
`ac_Vol` smallint(6) NOT NULL,
`ac_Dil` varchar(50) DEFAULT NULL,
`ac_Img` varchar(50) DEFAULT NULL,
`ac_Obs` varchar(200) DEFAULT NULL,
PRIMARY KEY (`ac_Id`)

OK, thanks so much!!

Kelly Keller-Heikkila
  • 2,544
  • 6
  • 23
  • 35
MaxHD
  • 31
  • 5
  • mysql_ functions is deprecated. use something else. also, turn on error reporting and you'll see that `$array` is undefined during your "update" loop. You're not showing enough code to give any suggestions as to how to fix it. – I wrestled a bear once. Dec 09 '15 at 20:40
  • Thanks for answering so queckly. There is no error, simply does not make the modification and leaves the field with 0 as a value. I use the mysql_functions because it's all I know for now. I can put the code whole if necessary... Thank you – MaxHD Dec 09 '15 at 20:47
  • there is no error because error reporting isn't turned on. turn it on or look in your logs and you will see the error. – I wrestled a bear once. Dec 09 '15 at 20:49
  • Yes, the error reporting it´s ON but when I turned OFF, the problem is going on... – MaxHD Dec 09 '15 at 20:53
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Dec 09 '15 at 21:11

4 Answers4

0

You will save yourself a lot of headaches by switching over to PDO or mysqli* functions now, rather than using mysql* functions, which are deprecated.

PDO Manual

Mysqli Manual

Why you shouldn't use Mysql_* functions

Is it possible that the error is in not having double or single quotes around the <?php echo $arrayLab['lab_Id']?> & I would suggest to post your SQL, but having error reporting turned on will definitely help, as well as using PDO or mysqli as they are currently not under active development. (could not post as comment cause I need 50 rep..)

Community
  • 1
  • 1
Moonblaze
  • 163
  • 1
  • 11
0

Remove the size="0" because that may modify your final data in your form

Henri Devigne
  • 44
  • 1
  • 6
0

Look at the HTML you're generating:

start an <option>:

<option value=[..snip...]

do a test

if ($array['ac_Lab']==$arrayLab['lab_Id']) {
   [..snip..]
} else {
   <option value=<?php echo $arrayLab['lab_Id']?> ><?php echo $arrayLab['lab_Nombre']?> </option>
   ^^^^^^^^^^^^^---- start ANOTHER option

For every element which ISN'T "selected" (which is usually all of them except for one, you're building this:

<option value=<option value=$someid>

which is outright illegal html.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

In the code:

Laboratorio: 
<select name="lab">
    <?php
        $sqlLab = 'SELECT * FROM laboratorios order by lab_Nombre asc';
        $resLab = mysql_query($sqlLab);
        while ($arrayLab = mysql_fetch_array($resLab)) 
        { ?>
            <option value=<?php 
                            if ($array['ac_Lab']==$arrayLab['lab_Id'])
                            {
                                echo $arrayLab['lab_Id']?> selected="selected"><?php echo $arrayLab['lab_Nombre']?> </option>
                      <?php } 
                            else
                            { ?>
                                <option value=<?php echo $arrayLab['lab_Id']?> ><?php echo $arrayLab['lab_Nombre']?> </option>
                      <?php } ?>
    <?php } ?>
</select><br/><br/>

after the "else" i put a <option for more...

The code now is:

Laboratorio: 
<select name="lab">
    <?php
        $sqlLab = 'SELECT * FROM laboratorios order by lab_Nombre asc';
        $resLab = mysql_query($sqlLab);
        while ($arrayLab = mysql_fetch_array($resLab)) 
        { ?>
            <option value=<?php 
                            if ($array['ac_Lab']==$arrayLab['lab_Id'])
                            {
                                echo $arrayLab['lab_Id']?> selected="selected"><?php echo $arrayLab['lab_Nombre']?> </option>
                      <?php } 
                            else
                            { 
                                echo $arrayLab['lab_Id']?> ><?php echo $arrayLab['lab_Nombre']?> </option>
                      <?php } ?>
    <?php } ?>
</select><br/><br/>

Thanks so much!!

MaxHD
  • 31
  • 5