View Full Version : Problem with lookup table...

09-11-2009, 11:36 PM
Hello again...

Maybe someone can help me with this:


categorias: id_cat - cat_name

catalogo: id - id_cat - id_img - name - desc - status

In a form I submit the content for insert in catalogo. In one input, I lookup for categorias (cat_name) but only for displaying ther name, because what i need to insert in catalogo field is the id_cat. I've the following code:

$id = $_POST['id'];
$catg = $_POST['catg']; //it passes the categorias cat_name, but i need the id_cat

$catalogname = $_POST['catalogname'];
$catalogdesc = $_POST['catalogdesc'];
$stat = $_POST['stat'];

if ($catalogname == ""){
echo "<h3>Empty Fields...</h3>";
mysql_query("INSERT INTO `catalogo` VALUES ('','$catg','','$catalogname','$catalogdesc','$stat')");

09-11-2009, 11:58 PM
Are you trying to get the id for the category name so you can insert it into the record? If so you may want to user a select field and set its value to the id for each corresponding field name.

$q = 'SELECT id_cat, cat_name FROM categorias';
$r = mysql_query ($q);
if (mysql_num_rows ($r) > 0) {
echo '<select name="catg">';
while ($row=mysql_fetch_assoc ($r) {
echo "<option value=\"{$row['id_cat']}\">{$row['cat_name']}</option>";
echo '</select>';

Now you will have a drop down with the ids as the value for each name. When you select cat_name from the menu you will receive the id value for the $_POST['catg'] and not the name.

09-12-2009, 12:08 AM
Thats it Coyote6...ībut i receive this error with the code that you post it:

Parse error: parse error, unexpected '{' in C:\Programas\EasyPHP 2.0b1\www\administracao\catalogAdd.php on line 41

line 41 is: while ($row=mysql_fetch_assoc ($r){

09-12-2009, 12:59 AM
Thanks...you give me the light...I turn on the switcher!!....:=)

echo "<select name=\"catg\" id=\"catg\">";
$rSQL = mysql_query("SELECT * FROM categorias");
while($asData = mysql_fetch_array($rSQL))
echo "<option value=\"" . $asData['0'] . "\">" . $asData['1'] . "</option>";
echo "</select>";


09-12-2009, 01:30 AM
Sorry bout that I missed a ) on the end of that while statement.