Jump to content

[SOLVED] pulling field names from sql and printing form var from field names


ryantaylor3

Recommended Posts

I can't get this to work, it prints out $Field1,$Field2,$Field3,$Field4,$Field5 instead of the actualy form variable.

 

if($submit){

require("config.php");

$db_connect = mysql_connect($sql_host, $sql_user, $sql_pass) or die ('Could not connect: ' . mysql_error());
mysql_select_db($sql_db, $db_connect) or die ('Could not select DB');

$field_names = mysql_query("SELECT * FROM ryan_devel_fieldnames", $db_connect);
$fieldnames_row = mysql_fetch_row($field_names);

$fieldnames_var = "";

for ($i = 0; $i < mysql_num_fields($field_names); $i++) {

$fieldnames_var .= "\$$fieldnames_row[$i], ";

}
$fieldnames_var = substr($fieldnames_var, 0, -2);

$fieldnames_var = '$fieldnames_var';

echo $fieldnames_var;
exit;
}
<html>
<body>
<br><br><br>
<form action="<? echo $PHP_SELF; ?>" method="post">
<input type="text" name="Field1"><br>
<input type="text" name="Field2"><br>
<input type="text" name="Field3"><br>
<input type="text" name="Field4"><br>
<input type="text" name="Field5"><br>
<input type="submit" name="submit"></form>
</body>
</html>

Link to comment
Share on other sites

I get this output:

 

Field Names = Field1, Field2, Field3, Field4, Field5

Field Values = $Field1, $Field2, $Field3, $Field4, $Field5

Field URL Format = Field1=$Field1&Field2=$Field2&Field3=$Field3&Field4=$Field4&Field5=$Field5

 

OK, well this is what the code looks like now:

 

<?

if($submit){

require("config.php");

$db_connect = mysql_connect($sql_host, $sql_user, $sql_pass) or die ('Could not connect: ' . mysql_error());
mysql_select_db($sql_db, $db_connect) or die ('Could not select DB');

$field_names = mysql_query("SELECT * FROM ryan_devel_fieldnames", $db_connect);
$fieldnames_row = mysql_fetch_row($field_names);

$fieldnames_data = "";
$fieldnames_var = "";
$fieldnames_url = "";

for ($i = 0; $i < mysql_num_fields($field_names); $i++) {

$fieldnames_data .= "$fieldnames_row[$i], ";
$fieldnames_var .= "\$$fieldnames_row[$i], ";
$fieldnames_url .= "$fieldnames_row[$i]=\$$fieldnames_row[$i]&";

}
$fieldnames_data = substr($fieldnames_data, 0, -2);
$fieldnames_var = substr($fieldnames_var, 0, -2);
$fieldnames_url = substr($fieldnames_url, 0, -1);

echo "Field Names = $fieldnames_data<br>";
echo "Field Values = $fieldnames_var<br>";
echo "Field URL Format = $fieldnames_url";

exit;
}
?>

<html>
<body>
<br><br><br>
<form action="<? echo $PHP_SELF; ?>" method="post">
<input type="text" name="Field1"><br>
<input type="text" name="Field2"><br>
<input type="text" name="Field3"><br>
<input type="text" name="Field4"><br>
<input type="text" name="Field5"><br>
<input type="submit" name="submit"></form>
</body>
</html>

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.