0

i have this code, which helps me to retrieve all table fields and associate a check button to them, but this code is generating the same name, i mean it shows me all the fields, but named the same... id

I need their particulatr name..

can you please see what's wrong?

Thanks..

        <form action='report.php' method='post'>

<?php // Script 12.7 - sopping.php

$db = mysql_connect('localhost', 'root', '');
mysql_select_db('db_up', $db);

echo "<table border='1' class='tabtext'>";

$result = mysql_query("SELECT * FROM hostess");
$numrows = mysql_num_rows($result);
$numfields = mysql_num_fields($result);

// show headers
echo '<thead><tr>';
for ($field = 0; $field < $numfields; $field++) {
    $field_name = mysql_field_name($result, $field); // instead of $i
    echo '<th><label><input type="checkbox" name="checkbox[' . $field_name . ']" value="1"/> ' . $field_name . '</label></th>';
}

echo '</tr></thead>';

echo '<tbody>';
for ($row = 0; $row < $numrows; $row++) {
    $data = mysql_fetch_assoc($result);
    echo '<tr>';
    for ($field = 0; $field < $numfields; $field++) {
        $field_name = mysql_field_name($result, $field);
        if (isset($_POST['checkbox'][$field_name])) {
            echo '<td>' . $data[$field_name] . '</td>';
        }
    }
    echo '</tr>';
}
echo '</tbody>';
echo '</table>';


?>
<input type='submit' value='Submit' />
</form>
0

2 Answers 2

1

The second argument of the mysql_field_name function isn't defined so I am pretty sure PHP is assuming you mean 0 and is returning the first fieldname only. Use the variable you defined ($field) as the index.

Should be:

for ($field = 0; $field < $numfields; $field++) {
    $field_name = mysql_field_name($result, $field); // instead of $i
    echo '<th><label><input type="checkbox" name="checkbox[' . $field_name . ']" value="1"/> ' . $field_name . '</label></th>';
}
Sign up to request clarification or add additional context in comments.

9 Comments

Hey, thanks,, this was obvious, thanks :) Can i please ask you help about one thing, once i select the values, how can display only the selected values? i mean i need to print the fields which i selected, the information isnide them..
You already do that but you have the same issue in that block of code as what I answered. If you fixed that, and granted you have the form submitting to this script correctly, it should display only the checked ones. Should be: for ($field = 0; $field < $numfields; $field++) { $field_name = mysql_field_name($result, $field); // not $i again if (isset($_POST['checkbox'][$field_name])) { echo '<td>' . $data[$field_name] . '</td>'; } }
so i should put inside the report.php this code you just told me?
Please,s orry for the questions, but is this first part ok? i mean the checking and all stuff? should i proceed with the submission of the form?
I am not entirely sure which file your code is in but you should just correct the remaining place that you have this: $field_name = mysql_field_name($result, $i); and replace that $i with $field.
|
1

You are using mysql_field_name and it always return the same name for you(for column 0), because $i is not defined.

1 Comment

Can you please see my updated code? can you help me now with printing all tables , based on the fields i've selected, something like Select $checkbox, $checkbox2 etc ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.