Jump to content

isset help?


wabash

Recommended Posts

Hi there,

 

I'm inserting data into a mysql database.  My submissions works fine, however when a part of the form is not filled out the php scripit crashes.  How can I submit all data that is set and skip pieces that aren't filled out?

 

For example I have a plant database which has an option to insert different types of common names for the plant.  It's ok if they aren't all filled out.

 

My code is as follows:

<?php  

//insert general plant data into db
$queryplant="INSERT INTO actualplanttable (id, scientific, common1, common2, common3, family, genus, description, planting, growing, care, duration, reference) 
VALUES ('NULL', '$_POST[scientific]', '$_POST[common1]', '$_POST[common2]', '$_POST[common3]', '$_POST[family]', '$_POST[genus]', '$_POST[description]', '$_POST[planting]', '$_POST[growing]', '$_POST[care]','$_POST[duration]','$_POST[reference]')";
mysql_query($queryplant) 
or die ('Error updating actualplanttable');

//insert maintenance data into db
$querymaintenance="INSERT INTO plantmaintenancelinktable (plant_id, maintenance_id)
VALUES ('$inserted_id', '$_POST[maintenance]')";
mysql_query($querymaintenance) 
or die ('Error updating plantdurationtable');

?>

 

 

Should I be trying to work an isset function into this code?  Any help would be greatly appreciated.

 

Thanks!

 

Bill...

 

 

 

Link to comment
Share on other sites

thanks odang!

 

That worked for the text box submissions, though when I get a bit further down in the code to the check box submission area of my code--which is using a foreach statement,  I still get errors for not inserting data:

 

I tried using the error code you provided:

<?php
//insert exposure requirements
foreach ($_POST['exposure'] as $value)
{
$queryexposure="INSERT INTO plantexposurelinktable (plant_id, exposure_id) 
VALUES ('$inserted_id','$value')";
mysql_query($queryexposure) 
or die (mysql_error().' in updating plantexposurelinktable');
}
?>

 

The error is.......

 

Warning: Invalid argument supplied for foreach() in ..... on line 51.  Any ideas about this?

 

Continued thanks for your help.

 

Bill...

Link to comment
Share on other sites

Thanks Nightslyr,

 

Yes, I'm leaving this section blank and not getting the array.  I'm hoping to leave sections blank but still be able to submit other sections of the form.

 

Can you tell me how to check if it's null?  Or point me in the right direction? I've never done that before.

 

Continued thanks.

 

Bill....

Link to comment
Share on other sites

Try something along the lines of:

 

if (!empty($_POST['exposure']) && is_array($_POST['exposure']))
{
   // run foreach
}
else
{
   // error
}

 

The empty part of the conditional checks to see if $_POST['exposure'] actually contains a value.  The is_array portion checks to see if the value is an array you can loop over.

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.