0

I've searched on the Internet to get my answer, but I couldn't find a helpful one. I've got a page called 'post.php' with a form where I can add an image and submit it to the database.

The big problem is when I go to mysite.com/post.php a new empty row is created automatically in the database, which I clearly don't want. I want only to update the database after clicking on the submit button my code:

the part of INSERT:

<?php
// POST.PHP POSTING NEW CONTENT
include 'config.php';
                            // values from form
                            $id=$_POST['id'];
                            $title=$_POST['title'];
                            $pic=$_POST['pic'];
                            $youtube=$_POST['youtube'];
                            $cat=$_POST['cat'];


                            // insert data to mysql
                            $sql = "INSERT INTO post(id, title, pic, youtube, cat)VALUES('$id', '$title', '$pic', '$youtube', '$cat')";
                            $result=mysql_query($sql);


                            // succes added
                            if(!$result){

                            echo "Something went wrong!";
                            }

                            else {
                            echo "Yeah, buddy! Your content is added.";
                            }
                            // end of post script ^^
?> 

// end of insert

        //POST IMAGE PAGE
        if(isset($_GET['pic'])) { 
        ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    title: <input name="title" type="text" id="title"><br />

   Add url of image;<br />
    <input type="text" name="pic" id="pic"/><br />
    <?php
    echo '
    Category game: 
     <select name="cat"> ';


     $query2 = mysql_query("SELECT * FROM `category`");
      while($row=mysql_fetch_array($query2)){ 
echo '
       <option value="'.$row["nameID"].'">'.$row["name"].'</option> ';
        } 
    ?>

     </select>
    <input type="submit" onclick="this.disabled = true" name="submit" value="submit">
   </form> 
   <?php
   // end script of posting picture
  }

 ?>
n00dle
  • 5,949
  • 2
  • 35
  • 48
Hadscape
  • 35
  • 2
  • 9
  • 1
    Need to see more of the code from this file - specifically the part where you insert into the database. – larsAnders Mar 25 '14 at 19:20
  • I've tidied your grammar to improve readability and changed your tags, as this question has nothing to do with JS/jQuery, but does potentially include MySQL information. – n00dle Mar 25 '14 at 19:56

3 Answers3

1

You need to add some conditional code around the part that inserts into the database, checking for if any values has been received (if($myvar){ // do stuff }).

Add the rest of your code, specifically the part that adds stuff to the database as that is what's causing you problems, not the code you posted.

rorymorris
  • 223
  • 1
  • 4
0

Check if the post have been set on the file that handles the database input.

    if(isset($_POST['pic'])){
//do something
}

else{ // handle the exeption} 

Also, you should not use mysql_* functions anymore. they are unsafe and deprecated as-of php 5.5

0

You need to wrap the whole block of database insertion code in an if statement. That way, it will not execute until the form has been submitted and $_POST['submit'] has a value:

include 'config.php';
if (isset($_POST['submit'])){
    // values from form
    $id=$_POST['id'];

    // etc... code stays the same down to:
    echo "Yeah, buddy! Your content is added.";
    }
}//end if (don't forget to add this last bracket)

Also, you should switch to mysqli or PDO, and use parameterized queries. Otherwise, your site is open to a variety of gnarly attacks via SQL injection. It's not that hard to switch, and very, very important.

Community
  • 1
  • 1
larsAnders
  • 3,813
  • 1
  • 15
  • 19