Basically. I have this query that is executed on a form submit.

$add_proc = mysql_query(" INSERT INTO `proc` (active, title, desc, notes, link, tags1, tags2, tags3)
VALUES ( '".$_POST['add_proc_active']."',  '".$_POST['add_proc_title']."', '".$_POST['add_proc_desc']."', '".$_POST['add_proc_notes']."', '".$_POST['add_proc_link']."', '".$_POST['add_proc_tag1']."', '".$_POST['add_proc_tag2']."', '".$_POST['add_proc_tag3']."' )

Now.
The form doesn't require you to enter all 3 tags.
However, if someone doesn't fill in 1 or more of the tags fields, it need to set that value to NULL in the database.

Something to the effect of

VALUES ( if(isset($_POST['add_proc_tag1)) { '".$_POST['add_proc_tag1']."' } else { tags1=null } )

But obviously you can't put PHP in an SQL query.

Any ideas? :)

Recommended Answers

All 2 Replies

You can write a simple function to do this. I use something like this:

function dbNull($value) {
	if(empty($value)) {
		return 'NULL';
	} else {
		return "'".mysql_real_escape_string($value)."'";
	}
}

// and you apply it like this:
      $add_proc = mysql_query(" INSERT INTO `proc` (active, title, desc, notes, link, tags1, tags2, tags3)   
		VALUES ( '".$_POST['add_proc_active']."', '".$_POST['add_proc_title']."', '".$_POST['add_proc_desc']."', '".$_POST['add_proc_notes']."', '".$_POST['add_proc_link']."', ".dbNull($_POST['add_proc_tag1']).", ".dbNull($_POST['add_proc_tag2']).", ".dbNull($_POST['add_proc_tag3']) );

Your an actual ledgend!
Thanks dude!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.