I am trying to write a simple script to add data to a table but have not done PHP in a while so had to use the manuals. Anyways I thought I had this looking good but for some reason the script keep running thru all the code when the movie variable is empty.

<?php
if ($_POST['movie']){
 include '../mconect.php';
    if(!mysql_SELECT_DB('cafrow_movies')){
	die("Could not Select Database Movies");
    }
 if (!MYSQL_Query("INSERT INTO movies SET movie='$_POST['movie']'")){
	die("Could not Update Records");
    }Else{
	$updatemessage = $_POST['movie']. " has been added to the database";
	}  	
mysql_close($mconnect);
}

echo $updatemessage;
?>
<form action="addmovie.php" method="post">
Movie Name: <input type="text" name="movie"><br>
<input type="Submit" name="submit" value="Add Movie">
</form>

The first If statment should be stoping the script from running the MySQL crap if it is empty, but I keep getting error about the MySQL trying to add data to the table but is not getting any info, well makes sence since the _Post[movie] is empty, I thought it would return false but it does not, I tried writting the if statement

if (!$_POST['movie'] == ""){

but I get the same error, I thought that if the IF condition was false it would skip the code that was within that condition.

thanks for any help.

    fixed problem

    <?php
    $movie = $_REQUEST['movie'];
    
    if ($movie){
     include '../mconect.php';
        if(!mysql_SELECT_DB('cafrow_movies')){
    	die("Could not connect to Database Movies");
        }
     if (!MYSQL_Query("INSERT INTO movies SET movie='$movie'")){
    	die("Could not Update Records");
        }Else{
    	$updatemessage = $movie. " has been added to the database";
    	}  	
    mysql_close($mconnect);
    }
    
    echo $updatemessage;
    ?>
    <form action="addmovie.php" method="post">
    Movie Name: <input type="text" name="movie"><br>
    <input type="Submit" name="submit" value="Add Movie">
    </form>
    

      Had to rewrite your code, you had a bunch of errors. Compare the 2.

      <?php
      if (isset($_POST["movie"])) {
      	$movie = $_POST["movie"];
      	if (!empty($movie)) {
      		include(../mconect.php);
      		$db = mysql_select_db("cafrow_movies",$conn) or die ("Could Not Connect");
      		$sql = mysql_query("INSERT INTO movies (movie) VALUES ('$movie')") or die (mysql_error());
      		header("Location: addmovie.php?back=1&m=$movie");
      	} else {
      		header("Location: addmovie.php?back=2");
      	}
      } else {
      	if (isset($_GET["back"])) {
      		if ($_GET["back"] == 1) { echo $_GET["m"]." has been added to the database"; }
      		if ($_GET["back"] == 2) { echo "Please enter a movie title"; }
      	}
      ?>
      <form action="addmovie.php" method="post"> 
      Movie Name: <input type="text" name="movie"><br> 
      <input type="Submit" name="submit" value="Add Movie"> 
      </form>
      <?php
      }
      ?>

        this is what I ended up with, it works but was wondering if the coding is really that bad. I have tested it and have found no problems.

        <?php
        $id = $_REQUEST['id'];
        $movie = $_REQUEST['movie'];
        $action="Add New Listing";
        if ($id){
        include 'mconnect.php';
         mysql_SELECT_DB('cafrow_movies') or die ('Could Select Databse Movies');
        if($movie){
         mysql_QUERY("UPDATE movies SET movie = '$movie' WHERE id = '$id'") or die ("Could not Update Database");
        $movie="";
        }else{
         $movie = mysql_QUERY("SELECT * FROM movies WHERE id ='$id'");
         $row=mysql_fetch_array($movie);
        $action ="Edit Listing";
        }
        mysql_close($mconnect);
        }else{
        if ($movie){
         include 'mconnect.php';
            if(!mysql_SELECT_DB('cafrow_movies')){
        	die("Could not connect to Database Movies");
            }
         if (!MYSQL_Query("INSERT INTO movies SET movie='$movie'")){
        	die("Could not Update Records");
            }Else{
        	$updatemessage = $movie. " has been added to the database";
        	}  	
        mysql_close($mconnect);
        }
        }
        echo $updatemessage;
        ?>
        <form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
        <input type="hidden" name="id" value="<? echo $id ?>"><br>
        Movie Name: <input type="text" name="movie" value="<? echo $row['movie'] ?>"><br>
        <input type="Submit" name="submit" value="<? echo $action ?>">
        </form>
        <br><br>
        <a HREF="movies.php" target="_self">Back to Movie Listing</a>
        

        Basicly this site is used to either Update or add a new entry.

          Write a Reply...