1

i wanna ask about this code.. im starting with PHP and MYSQL and i cant figured out why it wont to INSERT INTO db even if im connected with..

Basically the php code doesnt work after "SUBMIT" i didnt recieve message, nothing.. Thanks for every help

add_post.php:

<form>
  <div class="form-group">
    <label for="post_title">title</label>
    <input type="text" class="form-control" id="post_title" name="title"/>
  </div>
  <div class="form-group">
    <label for="post_video">video</label>
    <input type="text" class="form-control" id="post_video" name="video"/>
  </div>
  <div class="form-group">
    <label for="post_content">content</label>
    <textarea rows="10" id="post_content" class="form-control" name="content"></textarea>
  </div>
  <div class="form-group">
    <label for="file_input">file input</label>
    <input type="file" id="file_input" name="image">
    <p class="help-block">Max upload file is 5MB.</p>
  </div>
  <button class="btn btn-default" name="submit">Submit</button>
</form>

<?php  
    include('connect.php');

    if(isset($_POST['submit'])){ 

        $post_title = $_POST['title'];
        $post_date = date('d-m-y');
        $post_video = $_POST['video'];
        $post_content = $_POST['content'];
        $post_image = $FILES['image'];

        if(empty($post_title) || empty($post_content)) {
            echo "<script>alert('title or content is empty')</script>";
        }
        else {
            move_uploaded_file($post_image,"image/uploads/$post_image");

            $insert_query = "INSERT INTO post (title, image, content, video) VALUES (
              '$post_title',
              '$post_image',
              '$post_content',
              '$post_video')";

            if(mysqli_query($db, $insert_query)) {
                echo "<center><h1>post published successfuly</h1></center>";
            }
        }

    }
?>

connect.php (for C9.com):

<?php
    // A simple PHP script demonstrating how to connect to MySQL.
    // Press the 'Run' button on the top to start the web server,
    // then click the URL that is emitted to the Output tab of the console.

    $servername = getenv('IP');
    $username = getenv('C9_USER');
    $password = "";
    $database = "portfolio";
    $dbport = 3306;

    // Create connection
    $db = new mysqli($servername, $username, $password, $database, $dbport);

    // Check connection
    if ($db->connect_error) {
        die("Connection failed: " . $db->connect_error);
    }
?>

db screenshots :

enter image description hereenter image description here

liborza
  • 969
  • 1
  • 7
  • 28

2 Answers2

2

I guess you should use the attribute "method" in your form, in this case:

<form method="post">

Because the default method is get, and as I see you are expecting to receive a $_POST data. Hope it helps

Pablo Cardozo
  • 103
  • 1
  • 10
2

the form method should be POST and since you are uploading files, you should specify the enctype.

Here's an exemple :

<form method="post" enctype="multipart/form-data">

also, the variable for the file should be $_FILES not $FILES

reference for the enctype : http://www.w3schools.com/tags/att_form_enctype.asp

wmehanna
  • 394
  • 3
  • 15