doupo2633 2016-01-09 01:01
浏览 44
已采纳

PHP - 将数据插入数据库[重复]

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

</div>
  • 写回答

2条回答 默认 最新

  • dongzhang2150 2016-01-09 01:06
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?