Do not put into database if there is no entry.

Discussion in 'PHP' started by baris22, Feb 17, 2009.

  1. #1
    hello,

    I have got a form with 3 fields (20 form on 1 page). Sometimes there is no entry on title field but there is entry on description and fullpage fields. All i want to do is if there is no entry on title i just do not want to put it into database.

    How can i do this?

    
    
        $title=$_POST['title'][$i];
        $description=$_POST['description'][$i];
        $fullpage=$_POST['fullpage'][$i];
    
    		
    
                    $query="INSERT INTO `file` VALUES ('', '".$title."', '".$description."', '".$fullpage."')";
        mysql_query($query);
        echo mysql_error();
        $c++;
       }
      }
      echo '<center><b>'. $c.' entries put into database</b></center><br/>';
      mysql_close($link);
     }
    
    
    PHP:
    Thanks
     
    baris22, Feb 17, 2009 IP
  2. MC_delta_T

    MC_delta_T Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    
    
        $title=$_POST['title'][$i];
        $description=$_POST['description'][$i];
        $fullpage=$_POST['fullpage'][$i];
    
    	if($title != ""){
    
                    $query="INSERT INTO `file` VALUES ('', '".$title."', '".$description."', '".$fullpage."')";
        mysql_query($query);
        echo mysql_error();
    			}
        $c++;
       }
      }
      echo '<center><b>'. $c.' entries put into database</b></center><br/>';
      mysql_close($link);
     }
    
    
    PHP:
    try this. but $c value doesnt affect.
     
    MC_delta_T, Feb 17, 2009 IP
    baris22 likes this.
  3. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Without having all the code, I can't figure out what you're doing with "$i" and "$c", but here's how I would do it for a single form submit.

    <?php
    if(isset($_POST['submitForm']) {
    	// create the error array for display problems
    	$error		=	array();
    
    	// if the title isn't set, set the error to display for the title
    	// you can add more statements, like for length, invalid characters, etc
    	if(!$_POST['title']) {
    		$error['title']		=	"<p>You must enter a title.</p>";
    	}
    
    	// same as for title
    	if(!$_POST['description']) {
    		$error['description']	=	"<p>You must enter a description.</p>";
    	}
    
    	// if there are errors in the form, set a form error to let the user know what's going on, and redisplay the form
    	// else, go forth and submit thine query!
    	if(!empty($error)) {
    		$error['form']		=	"<p>There were errors with your submission. Please correct them and resubmit.</p>";
    		displayForm($error);
    	} else {	
    		$title=$_POST['title'];
        		$description=$_POST['description'];
     	
    		$query="INSERT INTO `file` VALUES ('', '".$title."', '".$description."', '".$fullpage."')";
        
    		mysql_query($query);
    
    		$result	=	mysql_insert_id();
    		
    		// if there is no insert id, something happened. Set a new error, display the mysql_error, and display the form again
    		// else, everything worked great, so tell the user so.and maybe display the form again.
    		if(!$result) {
    			error['form'] 	=	("<p>There was an error with the submission. MySQL error: ".mysql_error())."</p>");
    			displayForm($error);
    		} else {
    			echo("<p>Successfully inserted data!</p>");
    			//dispalyForm();
    		}
    	}
    }
    
    function displayForm($error = "") {
    	?>
    	
    	<?php echo($error['form']); ?>
    	
    	<form method="POST">
    	
    	<?php echo($error['title']; ?>
    	<p><input type="text" name="title" value="<?php echo($_POST['title']); ?>" /></p>
    	
    	<?php echo($error['description']; ?>
    	<p><input type="text" name="description" value="<?php echo($_POST['description']); ?>" /></p>
    	
    	<p><input type="submit" name="submitForm" value="Submit Form" /></p>
    	
    	</form>
    	
    	<?php
    }
    PHP:
     
    Altari, Feb 17, 2009 IP
  4. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4

    It worked perfect.

    Thanks
     
    baris22, Feb 20, 2009 IP