PHP将$ _POST数据插入数据库?

时间:2014-03-22 04:14:40

标签: php mysql

我有一个表单,我想用它来发送数据("review""rating")并将其保存到数据库中。出于某种原因,它无法正常工作。我得到的是〜/ share / review,这是一个空白屏幕(它应该重定向到另一个页面)和未更新的数据库。

<form style="text-align: center" method="post" action="/share/review">
  <div class="5grid">
    <div class="row">
      <div class="12u">
        <textarea name="review" id="review" placeholder="Review"></textarea>
      </div>
    </div>
    <div class="row">
      <p> Rating: </p>
      <select name="rating">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
      </select>
    </div>
    <div class="row">
      <div class="12u">
        <input type="submit" class="button" value="Submit">
        <input type="reset" class="button button-alt" value="Clear Form">
      </div>
    </div>
  </div>
</form>

我使用的是Slim框架,所以这里是我用来发送数据的代码:

$app->post('/review', function() use ($app) {
   // put code to remember a review here...
   $res = \Portfolio\Database::saveReview($_POST['review'], $_POST['rating']);
   krumo($res)

   $app->redirect('#');
   //header("Location: /testing/");
});

以下是我用来插入数据库的数据库代码:

public function saveReview() {
    //mysqli_query($this->conn, "INSERT INTO reviews (review_text, rating) VALUES ('$_POST[review]', '$_POST[rating]')");
    $sql_add = "INSERT INTO reviews (review_text, rating) 
                 VALUES ('".$_POST['review']."', '".$_POST['rating']."')";
}

如果这会产生影响,这是数据库连接代码:

function connect() {
    // Connecting to mysql database
    $this->conn = 
      mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME) 
        or die("Error " . mysqli_error($this->conn));

    // returing connection resource
    return $this->conn;
}

0 个答案:

没有答案