2017-04-18 15 views
-2

私は大丈夫でしたが、結果は得られませんでした。 phpとmysqliコマンドを使ってデータを挿入したい。実際にCMSシステムを練習として作っています。それをどうやって助けてください!データがテーブルに挿入されていませんPHPコードとmysqli

ここにいくつかのスクリーンショットが

post page

enter image description here

this is error i got

enter image description here

コードここ

<!DOCTYPE html> 
 
<html lang="en"> 
 
    <head> 
 
    <meta charset="utf-8"> 
 
\t <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
 
\t <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <title>Personal Home Page</title> 
 
    <!-- Bootstrap --> 
 
\t <link href="css/bootstrap.css" rel="stylesheet"> 
 
    <link href="css/custom.css" rel="stylesheet"> 
 
    <link rel="stylesheet" href="css/footer-distributed-with-address-and-phones.css"> 
 

 
\t <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> 
 
\t <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 
 
\t <!--[if lt IE 9]> 
 
\t \t <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 
 
\t \t <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 
 
\t \t <![endif]--> 
 
    </head> 
 
    <body> 
 

 
    <div class="jumbotron-2"> 
 
    <h1 style="text-align:left">Write your New Post</h1> 
 
    <form class="form-horizontal" method="post" action="new_post.php" enctype="multipart/form-data"> 
 
    <div class="form-group"> 
 
     <label for="inputEmail3" class="col-sm-2 control-label">Post Title*:</label> 
 
     <div class="col-sm-10"> 
 
      <input type="name" class="form-control" name="title" placeholder="Title of Post"> 
 
     </div> 
 
    </div> 
 
    <div class="form-group"> 
 
     <label for="inputPassword3" class="col-sm-2 control-label">Post Author*</label> 
 
     <div class="col-sm-10"> 
 
      <input type="name" class="form-control" name="author" placeholder="Published By"> 
 
     </div> 
 
    </div> 
 

 
    <div class="form-group"> 
 
     <label for="inputPassword3" class="col-sm-2 control-label">File(img/vid:)*</label> 
 
     <div class="col-sm-10"> 
 
      <input type="file" name="image" class="form-control" placeholder="image or video file"> 
 
     </div> 
 
    </div> 
 
    <div class="form-group"> 
 
     <label for="inputPassword3" class="col-sm-2 control-label">Post Content:*</label> 
 
     <div class="col-sm-10"> 
 
      <textarea rows="15" cols="100" name="content" class="form-control" placeholder="content goes here"></textarea> 
 
     </div> 
 
    </div> 
 
    
 
    <div class="form-group"> 
 
     <div class="col-sm-offset-2 col-sm-10"> 
 
      <button type="submit" name="submit" class="btn btn-success btn-lg" style="align-items:center">Publish</button> 
 
      <button type="reset" class="btn btn-danger btn-lg" style="align-items:center">Cancle</button> 
 
     </div> 
 
    </div> 
 
</form> 
 
    
 
</div> 
 

 

 
<div style="background-color:#000000"> 
 
    <div style="color:#FFFFFF" class="btn btn-danger btn-lg"><a href="backend.php">Backend</a></div> 
 
    </div> 
 
    
 
\t <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
 
\t <script src="js/jquery-1.11.2.min.js"></script> 
 

 
\t <!-- Include all compiled plugins (below), or include individual files as needed --> 
 
\t <script src="js/bootstrap.js"></script> 
 
    </body> 
 
</html> 
 

 
<?php 
 
include("includes/connect.php"); 
 

 
if (isset($_POST['submit'])) 
 
{ 
 
\t \t $title=$_POST['title']; 
 
\t \t 
 
\t \t $author=$_POST['author']; 
 
\t \t $content=$_POST['content']; 
 
\t \t $image_name = $_FILES['image']['name']; 
 
\t \t $image_type = $_FILES['image']['type']; 
 
\t  $image_size = $_FILES['image']['size']; 
 
\t  $image_tmp = $_FILES['image']['tmp_name']; 
 
\t \t 
 
\t \t 
 
\t \t if ($title=='' or $author=='' or $content==''){ 
 
\t \t \t echo "<script>alert('Do not let any field empty')</script>"; 
 
\t \t \t exit(); 
 
\t \t } 
 
\t \t if($image_type=="image/jpeg" or $image_type=="image/png" or $image_type=="image/gif"){ 
 
\t \t \t 
 
\t \t \t if($image_size<=5000000){ 
 
\t \t \t \t move_uploaded_file($image_tmp, "img/$image_name"); 
 
\t \t \t } 
 
\t \t else{ 
 
\t \t \t echo "<script>alert('image is greater')</script>"; 
 
\t \t } 
 
\t \t } 
 
\t \t else{ 
 
\t \t \t echo "<script>alert('image type is invalid')</script>"; 
 
\t \t } 
 
\t \t 
 
\t \t $sql="INSERT INTO posts(post_title, post_author, post_img, post_content) VALUES($title,$author,$image_name,$content)"; 
 
\t \t $link = mysqli_connect("localhost", "root", " ", "firstwebsitedb"); 
 
\t \t if (mysqli_query($link,$sql)){ 
 
\t \t \t echo "<script>alert('Post is Published')</script>"; 
 
\t \t } 
 
\t \t else{ 
 
\t \t \t echo "<script>alert('Post is not Published')</script>"; 
 
\t \t } 
 
} 
 
?>

+0

あなたのDB接続があるショット? –

+0

接続していることを確認してください – Akintunde007

+0

http://php.net/manual/en/mysqli.select-db.php –

答えて

0

以下、クエリで$linkを追加挿入文です:

$link = new mysqli_connect("localhost", "root", " ", "firstwebsitedb"); 

$sql="INSERT INTO posts(post_title, post_author, post_img, post_content) VALUES($title,$author,$image_name,$content)"; 

if ($link->query($sql)){ 
    echo "<script>alert('Post is Published')</script>"; 
} 
else{ 
    echo "<script>alert('Post is not Published')</script>"; 
} 

希望、これは役立ちます!

+0

彼はすでにconnect.phpファイルを含んでいます。 – LalaByte

+0

はい、この質問があり、 '$ sql'の後に接続文字列が追加されています。なぜ私はこの行を追加したのですか? – mageDev0688

+0

クエリ関数を呼び出さない限り変数の上または下を接続するかどうかは関係ありません。 – LalaByte

-1
$link = mysqli_connect("localhost", "root", " ", "firstwebsitedb"); 
if (!$link) { 
    echo "Error: Unable to connect to MySQL." . PHP_EOL; 
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; 
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; 
    exit; 
} 

if (mysqli_query($link,$sql)){ 
    echo "<script>alert('Post is Published')</script>"; 
} 
else{ 
    echo "<script>alert('Post is not Published')</script>"; 
} 

データベース接続に問題があると思います。データベースのパスワードとして「」を使用します。そこにはスペースがあります。

+0

その点....私はおかげです。高槻 –

関連する問題