Jump to content

Recommended Posts

I have a form that submits the data to mysql. Then on the page where it displays all the rows in the table (items) I want it to only show the form fields (or columns) that have been filled out. So I'm trying to check if it is empty. I'm using a code that does that now -- but it only works for the first entry in the database, and then applies that to each and every entry after it (even though it isn't true)**. Should I be using a for loop so it checks every database table entry, and not just the first entry?

 

** When I say it applies to each and every entry I mean if the first entries "Category" is empty, it thinks the rest of the table entries "Category" is empty, when in fact it isn't.

 

Hope that makes sense...

Any help is appreciated!  ;D

 

$result = mysql_query("SELECT * FROM `Equipment Listings` WHERE id = $id");

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

echo '<table>';

//CATEGORY
if (isset($_POST['category'])) 
{
echo '<tr><td><span class="equipmentheader">Category:</span> ';
echo $row['category'];
echo '</td></tr>';
}
else
{
echo '';
}


//YEAR
if (isset($_POST['year'])) 
{
echo '<tr><td><span class="equipmentheader">Year:</span> ';
echo $row['year'];
echo '</td></tr>';
}
else
{
echo '';
}


//SERIAL NUMBER
if (isset($_POST['serialnum'])) 
{
echo '<tr><td><span class="equipmentheader">Serial Number:</span> ';
echo $row['serialnum'];
echo '</td></tr>';
}
else
{
echo '';
}

Link to comment
https://forums.phpfreaks.com/topic/155184-how-to-check-if-a-field-is-empty/
Share on other sites

Hi

 

Do you mean to be checking the $_POST fields. Quick guess and I think what you are trying to get is:-

 

$result = mysql_query("SELECT * FROM `Equipment Listings` WHERE id = $id");

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

echo '<table>';

//CATEGORY
if (isset($row['category'])) 
{
	echo '<tr><td><span class="equipmentheader">Category:</span> ';
	echo $row['category'];
	echo '</td></tr>';
}
else
{
	echo '';
}


//YEAR
if (isset($row['year'])) 
{
	echo '<tr><td><span class="equipmentheader">Year:</span> ';
	echo $row['year'];
	echo '</td></tr>';
}
else
{
	echo '';
}


//SERIAL NUMBER
if (isset($row['serialnum'])) 
{
	echo '<tr><td><span class="equipmentheader">Serial Number:</span> ';
	echo $row['serialnum'];
	echo '</td></tr>';
}
else
{
	echo '';
}
}

 

All the best

 

Keith

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.