Jump to content

Building HTML form from MySQL table


pcw

Recommended Posts

Hi, I have this script which I would like to use to build an html form from a MySQL table.

 

The <input text................ writes successfully to file but none of the other form types are written to file when using the elseif command

 

Also, I would like this script to carry out the file write foreach row in the table

 

It seems complicated and i am not sure if I am going about it in the right way, but here goes

 

//// Create Forms from MySQL

$result = mysql_query("SELECT * FROM forms");

while($row = mysql_fetch_assoc($result)){

$field_label = $row['field_label'];
$column_name = $row['column_name'];
$field_type = $row['field_type'];

if ($row['field_type'] = 'Text') {

$stringData = "$field_label<input type=text name=$column_name>\n";
fwrite($fh, $stringData);

} elseif ($row['field_type'] = 'Text Area') {

$stringData = "$field_label<input type=textarea name=$column_name></textarea>\n";
fwrite($fh, $stringData);

} elseif ($row['field_type'] = 'Select Menu') {

$stringData = "$field_label<select name=$column_name></select>\n";
fwrite($fh, $stringData);

} elseif ($row['field_type'] = 'Checkbox') {

$stringData = "$field_label<input type=checkbox name=$column_name></textarea>\n";
fwrite($fh, $stringData);

}

fclose($fh);

 

As always, any help is much appreciated

Link to comment
Share on other sites

Were you intending to 'assign' values to $row['field_type'] in your if statements? What's basically happening is you're making the first condition true, and the rest false, simply because you're assigning instead of comparing.

 

= bad

== good

 

Unless it was somehow intentional, it's been a long day for me.

 

 

 

Link to comment
Share on other sites

Hi, thanks for your reply.

 

$field_type = $row['field_type'];

 

The above variable is passed from a form to this script.

 

What I want is if the $field_type is text then write this

 

$stringData = "$field_label<input type=text name=$column_name>\n";

 

whereas if $field_type is textarea then write this

 

$stringData = "$field_label<input type=textarea name=$column_name></textarea>\n";

 

also if possible, i would like it to retrieve each row from the table and carry out the appropriate action for each of them.

 

Im really stuck on this, but it is essential that it works. Help! lol

 

Link to comment
Share on other sites

Did you fix your assignments to be comparisons?

 

if ($row['field_type'] = 'Text')

 

Will always be true because you're assigning the value, and not making a comparison.

 

This line should read

if ($row['field_type'] == 'Text')

 

As should all of the elseifs.

 

== is a proper comparison

= is an assignment

 

 

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.