douxing1353
2015-01-18 16:50
浏览 54
已采纳

$ _POST没有插入数据库 - PHP MySQL

Here is my HTML form for adding data into the table:

<div class="TTWForm-container">
<div id="form-title" class="form-title field"><h2>ახალი სტატია</h2></div>
<form enctype="multipart/form-data" action="index.php?link=addart" class="TTWForm ui-sortable-disabled" method="post"> 
<div id="field2-container" class="field f_100 ui-resizable-disabled ui-state-disabled">
<label for="title">სათაური</label>
<input name="title" id="title" required="required" type="text">
</div>
<div id="field10-container" class="field f_100 ui-resizable-disabled ui-state-disabled">
<label for="post">
სტატია
</label>
<textarea rows="15" cols="40" name="post" id="post" required="required"></textarea>
</div>
<div id="form-submit" class="field f_100 clearfix submit">
<input value="დამატება" type="submit" id="submit">
</div>
</form>
</div>

processing

if (isset($_POST['title'])) {$title = $_POST['title'];}
if (isset($_POST['post'])) {$post = $_POST['post'];}

<?php
if (isset($title) && isset($post))
{
/*can add post */
$insert = "INSERT INTO article ('title','post') VALUES ('$title','$post')";
$result = mysqli_query($connect,$insert);

if ($result == 'true') {echo "<p>post is added.</p>";}
else {echo "<p>post is not added.</p>";}
}
else
{
echo "<p>Fill all fields.</p>";
}
?>

After processing it always says that post is not added. Checking database and its true. Cant find the mistake. Please help. Thanks.

图片转代码服务由CSDN问答提供 功能建议

这是我在表格中添加数据的HTML表格:

 <  code>&lt; div class =“TTWForm-container”&gt; 
&lt; div id =“form-title”class =“form-title field”&gt;&lt; h2&gt;ახალისტატია&lt; / h2&gt;&lt; / div&gt;  
&lt; form enctype =“multipart / form-data”action =“index.php?link = addart”class =“TTWForm ui-sortable-disabled”method =“post”&gt;  
&lt; div id =“field2-container”class =“field f_100 ui-resizable-disabled ui-state-disabled”&gt; 
&lt; label for =“title”&gt;სათაური&lt; / label&gt; 
&lt;输入名称 =“title”id =“title”required =“required”type =“text”&gt; 
&lt; / div&gt; 
&lt; div id =“field10-container”class =“field f_100 ui-resizable-disabled ui- 州禁用“&gt; 
&lt; label for =”post“&gt; 
სტატია
&lt; / label&gt; 
&lt; textarea rows =”15“cols =”40“name =”post“id =”post“required  =“required”&gt;&lt; / textarea&gt; 
&lt; / div&gt; 
&lt; div id =“form-submit”class =“field f_100 clearfix submit”&gt; 
&lt; input value =“დამატება”type =“ 提交“id =”提交“&gt; 
&lt; / div&gt; 
&lt; / form&gt; 
&lt; / div&gt; 
   
 
 

处理 \ n

  if(isset($ _ POST ['title'])){$ title = $ _POST ['title'];} 
if(isset($ _ POST ['post'])){  $ post = $ _POST ['post'];} 
 
&lt;?php 
if(isset($ title)&amp;&amp; isset($ post))
 {
 / *可以添加帖子* / \  n $ insert =“INSERT INTO文章('标题','发布')VALUES('$ title','  $ post')“; 
 $ result = mysqli_query($ connect,$ insert); 
 
if($ result =='true'){echo”&lt; p&gt; post is added。&lt; / p&gt;“  ;} 
else {echo“&lt; p&gt; post is not added。&lt; / p&gt;”;} 
} 
else 
 {
echo“&lt; p&gt;填写所有字段。&lt; / p&gt;”;  
} 
?&gt; 
   
 
 

处理后,它总是说没有添加帖子。 检查数据库及其真实性。 无法找到错误。 请帮忙。 谢谢。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duannuan0074 2015-01-18 16:56
    已采纳

    so just change:

    else
    {
    echo "<p>post is not added</p>";
    }
    

    to:

    else
    {
    echo "<p>MySQL error:".mysqli_error($connect)."</p>";
    }
    
    打赏 评论
  • dtjw6660 2015-01-18 16:55

    Boolean Comparison

    First, check your comparison. mysqli_query returns a boolean true or false, not a string 'true' or 'false.' Instead of:

    if ($result == 'true')
    

    use

    if ($result == true)
    

    or even:

    if ($result)
    

    Debug the Query

    Next, check the query itself for errors. As Fred noted, you should mark your column names with back ticks instead of single quotes:

    INSERT INTO article (`title`,`post`)
    

    Consider logging the output of mysqli_error, or try echoing your query and run it manually against the database. You may find that they query has a hidden syntax or data error that is not obvious in the code.

    Posted Values

    Next, check the values that you are fetching from $_POST. Is $_POST['post'] set? Does it have a useful value?

    Database Connection

    Finally, check the database connection itself. Are you truly connected? Do you have debugging logging or output around the connection attempt?

    打赏 评论
  • duanjiangzhi6851 2015-01-18 17:18

    Here's a prepared statement for you.

    $link = new MySQLi('localhost','username','password','database');
    
    if(isset($_POST['title'],$_POST['post'])&& $_POST['title'] !="" && $_POST['post'] !=""){
            $title = $_POST['title'];
            $post = $_POST['post'];
            if($query = $link->prepare('INSERT INTO article (title,post) VALUES(?,?)')){
                $query->bind_param('ss',$title,$post);
                $query->execute();
                $query->close();
                echo 'Success!';
            }else{
                echo 'Failure!';
            }
        }else{
            echo 'You missed a field!';
    }
    
    打赏 评论

相关推荐 更多相似问题