Hiding an Index value in SQL via PHP
I'm currently trying to get users to submit data via PHP/HTML forms and into a SQL database. I can do this happily but would like to not allow users to input the Index
column as it is automatically incrementing ajd wouldn't want to be fiddled with.
The current code I have is like this:
$result = mysql_query("SELECT * FROM {$table}") or die(mysql_error());
// Start a form to run insert.php
echo "<form action=\"insert.php\" method=\"post\">";
// Loop to create boxes in the form
for ($i = 0; $i < $fields_num; $i++)
{
$field = mysql_f开发者_运维百科etch_field($result);
echo "{$field->name}: <br /><input type=\"text\" name=\"{$field->name}\"onkeypress=\"return noenter()\"/>";
echo "<br />";
};
// End form.
echo "<input type=\"submit\">
</form>
</div>";
From here how could I ignore the Index column. I have total access to the database if that helps.
Thanks guys, in advance!
If you don't want this field to be displayed, just add a precondition in your loop before generating the output, something like this:
for ($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
if ($field->name != "yourindexfieldname")
{
echo "{$field->name}: <br><input type=\"text\" name=\"{$field->name}\"onkeypress=\"return noenter()\"/>";
echo "<br>";
}
};
Like this:
for ($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
if($field->name == 'Index') continue; //This does the trick
echo "{$field->name}: <br><input type=\"text\" name=\"{$field->name}\"onkeypress=\"return noenter()\"/>";
echo "<br>";
};
The continue
statement 'skips' to the next iteration of the loop. So, what we're doing is 'If the field name is "Index", skip to the following field'
Hope this helps. Cheers
you could omit the index field in the query:
SELECT <put the fields without your index field> from {table};
Create a view in db without index.
$result = mysql_query("SELECT * FROM v_{$table}) or die(mysql_error());
精彩评论