0

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? :)

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Clarkeez
0

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']) );
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.