Jump to content

[SOLVED] Problems with while loop


jber

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.