k... so, here's the thing...

I'm creating a User's Interface to insert some data into a database, and there are some fields that have foreign keys to other tables, which i have listed in a <SELECT> form, so the user would just select the vlue from a list instead of inserting a new one, which might cause some trouble with the db's integrity... when i want to insert the value to the table, i must do a subquery to obtain the value that goes into the table... that's where i have the problem... mysql just returns an error telling me i have an error in my MySQL syntax...
here's the code for 'insertar.php':

<?php
if($_POST['submit_region']){
   if(mysql_query("
      INSERT INTO Region (Nombre, Cod_Pais)
      VALUES ('$_POST[nombre_region]', (SELECT Cod_Pais FROM Pais WHERE Nombre=$_POST[opcion_pais]))", $cos_s)){
      echo "Fila insertada en la tabla 'Region' existosamente<br />";
   }else{
      die("Error al insertar registro en tabla 'Region'<br />" . mysql_error());
   }
}
?>

and this is the code for 'insertar_datos.html':

<form action="insertar.php" method="post">
   Nombre de la Region: <input type="text" name="nombre_region" /><br />
   Pais de la Region: <select name="opcion_pais">
      <option>--Seleccionar Pais--</option>
      <?php 
         $resultado_pais = mysql_query(" SELECT * FROM Pais ORDER BY Nombre");
         while($row = mysql_fetch_array($resultado_pais, MYSQL_ASSOC)){
            echo "<option value=\"" . $row[Cod_Pais] . "\">" . $row[Nombre] . "</option>";
         }
      ?>
   </select><br />
   <input type="submit" name="submit_region" value="Insertar Region">
</form>

this is the output: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/www/lapuglia.awardspace.com/insertar.php on line 21

Try to debug the code. Echo your query on your page and then copy paste that code to your mysql client's console there u can have a brief idea about that. I dont know what is the problem in that but I used this trick to solve problem quickly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.