jber Posted November 23, 2007 Share Posted November 23, 2007 Hello I´m trying to make an application to insert new data into a table. So i extract the fields of the table and let the user introduce new values. The problem is when i´m trying to put them into a table. Firstly i get the new data sent via form with a while loop, because the number anda name of the fields can be different in each case. I do it like this $sql = " SHOW COLUMNS FROM " .$_SESSION["nomtab"]. " "; $res= mysql_query($sql); while($row = mysql_fetch_array($res)) { $$row[0] = $_POST["$row[0]"]; } Now i introduce correctly the first´s field value and i create two auxiliar variables $primercampo and $valorprimercampo which contains the name of the first field and its value, so i can use them later to update the values, it goes like that $primercampo = $row[0]; $valorprimercampo = $$row[0]; $sql = " INSERT INTO " .$_SESSION["nomtab"]. " (".$row[0].") VALUES ('".$$row[0]."')"; $res= mysql_query($sql); and then i use a whole while loop to do it all together while($row = mysql_fetch_array($res)) { $$row[0] = $_POST["$row[0]"]; if ($primercampo == '') { echo ' Primer campo estaba vacío'; $primercampo = $row[0]; $valorprimercampo = $$row[0]; $sql = " INSERT INTO " .$_SESSION["nomtab"]. " (".$row[0].") VALUES ('".$$row[0]."')"; $res= mysql_query($sql); } else { $sql = " UPDATE " .$_SESSION["nomtab"]. " SET " .$row[0]. " = '" .$$row[0]. "' WHERE " .$primercampo. " = '" .$valorprimercampo. "'"; echo $sql; $res= mysql_query($sql); } } It does not work. Indeed, it only writes correctly the first field, but it does not enter into the else part ¿Can anybody help me? Thanks in advance Quote Link to comment Share on other sites More sharing options...
june_c21 Posted November 23, 2007 Share Posted November 23, 2007 try to put $row[0] instead of $$row[0] Quote Link to comment Share on other sites More sharing options...
jber Posted November 23, 2007 Author Share Posted November 23, 2007 where? Quote Link to comment Share on other sites More sharing options...
jber Posted November 23, 2007 Author Share Posted November 23, 2007 I`ve solved it by myself, changing a little bit The final code is as it shows $sql = " SHOW COLUMNS FROM " .$_SESSION["nomtab"]. " "; $res= mysql_query($sql); //echo mysql_errno($db) . ": " . mysql_error($db) . "\n"; $campos = ''; // create two variables which will contain the names of all the fields and its values $valores = ''; $i = 0; while($row = mysql_fetch_array($res)) { if($i == 0) { $$row[0] = $_POST["$row[0]"]; // Fill the two variables, special case the first one when a comma does not to be added $campos .= "".$row[0]. ""; $valores .= "'".$$row[0]. "'"; $i++; } else { $$row[0] = $_POST["$row[0]"]; $campos .= ",".$row[0]. ""; $valores .= ",'".$$row[0]. "'"; } } echo ' Los campos son : ' .$campos. ''; echo ' Sus valores son : ' .$valores. ''; $sql = " INSERT INTO " .$_SESSION["nomtab"]. " (".$campos.") VALUES (".$valores.")"; // Insert all as a whole echo $sql; $res= mysql_query($sql); echo mysql_errno($db) . ": " . mysql_error($db) . "\n"; Now it works Bye 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.