【问题标题】:php form submit isnt inserting into the databasephp表单提交未插入数据库
【发布时间】:2015-07-04 08:39:54
【问题描述】:

对,这应该是非常基本的 php,但是我一生都无法弄清楚为什么这不起作用。

我有一个表单,它在提交时应该抓取字段然后将数据插入数据库。我在我的 index.php 中有一个部分,它实际上从数据库中提取信息并且工作正常,所以我看不出这是一个连接问题。

db_connection.php 我已将此处的信息散列为我的数据库,但一切都是正确的

<?php
define("DB_SERVER","**********");
define("DB_USER","*************");
define("DB_PASS","*********");
define("DB_NAME","reviews");

$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);

if(mysqli_connect_errno()){
    die("database connection failed");
}


?>

functions.php

<?php
function redirect_to($new_location) {
      header("Location: " . $new_location);
      exit;
    }    
function confirm_query($result){
    if(!$result){
        die("database query failed");
    }
}
function find_reviews($connection){
    $query  = "SELECT * ";
    $query .= "FROM reviews";
    $result = mysqli_query($connection,$query);
    confirm_query($result);
    while($reviews = mysqli_fetch_assoc($result)){
        $output = "<li class=\"first\">";
        $output .= $reviews["name"];
        $output .= "</li>";
        $output .= "<li class=\"second\">";
        $output .= $reviews["company"];
        $output .= "</li>";
        $output .= "<li class=\"third\">";
        $output .= $reviews["comment"];
        $output .= "</li>";
        $output .= "<li class=\"line\"></li>";     
        echo $output;
    }
    mysqli_free_result($result);

    return $reviews;
}
?>

index.php 需要注意的是,函数 find_reviews() 有效并且确实从数据库中获取信息。 index.php中还需要上述“db_connection.php”和“functions.php”

<?php
require("includes/header.php");
$page_title = "All County Road Markings";
$description = "Specialising in Road Marking & Car Park Lining. We are a professional established road marking service with over 20 years experience";?>
<?php require("includes/db_connection.php"); ?>
<?php require("includes/functions.php"); ?>

<?php if(isset($_POST["submit"])){
    $name = $_POST["name"];
    $company = $_POST["company"];
    $comment = $_POST["comment"];
    $query = "INSERT INTO `reviews` (name,company,comment) VALUES ('$name','$company','$comment')";

    $result=mysqli_query($connection,$query);
    confirm_query($result);
    redirect_to("index.php");
}
?>
<div id="banner"></div>
<div id="paragraph">
    <h2>All County Road Markings are a professional established road marking<br>service with over 20 years experience within the industry</h2>
    <hr style="width: 1050px;">
</div>
<div id="content">
    <div id="left">
        <div class="slot">
            <div class="top carpark"> </div>
            <div class="linkbar">
                <h1>Car Parks</h1>
            </div><div class="linktext">
                <ul class="comments display">
                    <li>- Car Parking Bays</li>
                    <li>- Disabled Parking Bays</li>
                    <li>- Parent and Child Bays</li>
                    <li>- Lettering</li>
                    <li>- Hatchings</li>
                    <li>- Arrows</li>
                    <li>- Customised Lettering</li>
                </ul>
            </div>
        </div>
        <div class="slot">
            <div class="top roadmark"> </div>
            <div class="linkbar">
                <h1>Road Markings</h1>
            </div>
            <div class="linktext">
                <ul class="comments display">
                    <li>- Hatchings</li>
                    <li>- Centre Lines</li>
                    <li>- Double Yellow lines</li>
                    <li>- Give way junctions</li>
                    <li>- Reinstatement of existing markings</li>
                </ul>
            </div>
        </div>
        <div class="slot">
            <div class="top sportcourt">
            </div>
            <div class="linkbar">
                <h1>Sports/Playground Court</h1>
            </div>
            <div class="linktext">
                <ul class="comments display">
                    <li>- Tennis Court</li>
                    <li>- Basketball Court</li>
                    <li>- 5 A side Court</li>
                    <li>- Netball Court </li>
                    <li>- Reinstatement of existing markings</li>
                    <li>- Custom designs available</li>
                </ul>
            </div>
        </div>
    </div>
    <div id="right">
        <div id="rightbar">
            <h2 style="color: white;">Testimonials</h2>
        </div>
        <div id="comment">
            <ul class="comments">
                <?php echo find_reviews($connection); ?>
            </ul>
        </div>
        <div id="write">
            <p style="margin: 0px; padding-top: 5px;color: grey; font-size: 1.25em;">Click to write a review...<p>
        </div>
    </div>
    <a href="contact.php"><div id="quote"><div class="link-text">contact us</div></div></a>
</div>
<div id="add">
    <div id="close"></div>
    <form action="index.php" method="POST">
        <div id="name"> Name:<br/><span>Please Enter Full Name</span>
            <input type="text" name="name" id="textbox">
        </div>
        <div id="company"> Company<br/><span>Please Enter Company Name</span>
            <input type="text" name="company" id="textbox1">
        </div>
        <div id="review"> Review<br/><span>Please Enter Review</span>
            <textarea name="comment" id="reviewComment"></textarea>
        </div>
        <div id="save">
            <input type="submit" name="submit">
        </div>
    </form>
</div>
</body>
</html>

好的,任何人都可以看到我做错了什么吗?就像提交表单一样,它只是没有插入到数据库中。

编辑::

当我尝试回显mysqli->错误时;我收到以下错误:

 Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /hermes/bosoraweb140/b484/ipg.allcountyroadmarking/index.php on line 17 

所以我把它改成了

if(!results){
     echo "hello";
}

我没有看到“你好”,所以这表明我什至没有进入它处理帖子字段的部分

自行解决:::::

我已经修复了,我将 action="index.php" 更改为 action=""

我不明白,它应该可以工作,但由于某种原因让动作空白有效。两者都应该可以接受!

【问题讨论】:

  • 您需要使用错误消息来集中您的故障排除。查询后可以if (!$result) echo mysqli-&gt;error; 并将错误添加到您的帖子中吗?
  • 我的错误,应该是 (!$result) echo $connection-&gt;error; 来匹配您的代码。我认为这不是问题所在,但仍然是将来调试的有用工具。

标签: php mysql forms


【解决方案1】:

$_POST['submint'] 的 if 语句可能在每次提交时都是错误的。也许试试 !empty($_POST)

【讨论】:

    【解决方案2】:

    设置为

    的表单动作
     <form action="index.php" method="POST">
    

    没有工作,但是我不明白为什么不工作,因为它引用了当前页面。

    不过改成这样就解决了问题。

     <form action="" method="POST">
    

    【讨论】:

      【解决方案3】:

      好吧,你在同一个文件中得到了 php,所以如果它默认为同一页面,那么也许为什么 action="" 正在工作.. action="index.php" 应该在当前网址之前。也许尝试将完整的网址放在 action="the url for index.php" 中,看看是否有效。如果确实如此,那么 action="index.php" 一定不会转到正确的文件。

      【讨论】:

      • 那会更奇怪,除非 op 使用反直觉的 htaccess 规则会弄乱请求引用者
      猜你喜欢
      • 2020-05-15
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      • 2011-03-21
      • 2014-04-04
      • 2015-05-15
      • 2016-07-23
      • 2017-06-07
      相关资源
      最近更新 更多