簡體   English   中英

將PHP表單提交到數據庫時出現錯誤500

[英]Error 500 when submitting PHP form to database

我已經使用ProgreSQL和Heroku為我的Facebook Web應用程序創建了一個SQL數據庫,以下代碼是收集數據的形式

</style>
  <div class="container">
  <form action="insert.php" method="post" onSubmit="window.location.reload()">
  Cover URL: <input type="text" name="Cover URL" id="coverURL"><br><br>
  Title:   <input type="text" name="Title" id="title"><br><br>
  Author:      <input type="text" name="Author" id="author"><br><br>
  Genre:<br> <select name="genre" id="genre">
  <option>Adventure & Action</option>
  <option>Anthologies</option>
  <option>Classics</option>
  <option>Sport</option>
  <option>War</option>
//More options in actual code, just deleted some to save space.          
    </select><br><br>
  Total Pages: <input type="number" name="TotalPages" id="totalpages"><br><br>
  Curent Page: <input type="number" name="CurrentPage" id="currentpage"><br><br>
  <input type="submit"> </form><br><br></div>

  </center>
</section>

然后調用insert.php

<?php
$dbconn = pg_connect("host=ec2-54-243-190-226.compute-1.amazonaws.com port=5432    dbname=d6fh4g6l0l6gvb user=[REMOVED] password=[REMOVED] sslmode=require options='--client_encoding=UTF8'") 
or     die('Could not connect: ' . pg_last_error());


pg_query("INSERT INTO books(coverURL, title, author, genre, currentPg, totalPg)     VALUES('"$_POST["coverURL"]"','"$_POST["title"]"','"$_POST["author"]"','"$_POST["genre"]"',    '"$_POST["currentpages"]"','"$_POST["totalpages"]"')");


pg_close($dbconn);
?>

問題是我在在線查看后發現提交時出現錯誤500,大多數解決方案都說PHP中一定有一個錯誤,但是由於我的經驗不足(請邊學習邊學習),我不知道我做錯了什么。

如果需要,我可以提供更多信息。 提前致謝!

嘗試這個:

請確保html輸入的names $_POST匹配

mysqli_query($con,"INSERT INTO books(coverURL, title, author, genre, currentPg, totalPg) VALUES('".$_POST["coverURL"]."','".$_POST["title"]."','".$_POST["author"]."','".$_POST["genre"]."','".$_POST["currentpages"]."','".$_POST["totalpages"]."')");

編輯:使用此語句------ ^

而不是:

'$_POST[author]'

最好這樣做:

'".$_POST["author"]."'

而且您還知道$sql實際上不是插入到數據庫中嗎?

錯誤500是“內部服務器錯誤” 這基本上意味着服務器端發生了某些故障,這只是一條非常普通的錯誤消息。 基本上它可以是所有內容,不僅限於您的腳本。 可能是Apache模塊錯誤,也可能是其他任何拒絕您的WebServer軟件處理請求而沒有致命錯誤的錯誤。

如果僅在相同的操作(例如提交表單或調用特殊頁面)上發生,則很可能是腳本錯誤,導致錯誤500。

您應該查詢日志文件,尤其是。 apache錯誤日志。 它應該包含有關出了什么問題的更多信息。

就您而言,也許您的服務器不允許連接到外部數據庫服務器,但這只是一個猜測。

問題在於您實際上是在將事物“混合”在一起。 您的代碼與pg_connect連接,並與mysqli查詢。 僅使用Postgree函數連接到數據庫請閱讀http://www.php.net/manual/zh/ref.pgsql.php上的手冊。

我會做類似的事情:(雖然我沒有測試過)

 <?php
$dbconn = pg_connect("host=ec2-54-243-190-226.compute-1.amazonaws.com port=5432    dbname=d6fh4g6l0l6gvb user=[REMOVED] password=[REMOVED] sslmode=require options='--client_encoding=UTF8'") or die('Could not connect: ' . pg_last_error());;

$coverUrl = $_POST["coverURL"];
$title = $_POST["title"];
$author = $_POST["author"];
$genre = $_POST["genre"];
$currentPages = $_POST["currentpages"];
$totalPages = $_POST["totalpages"];
pg_query_params($dbconn, "INSERT INTO books(coverURL, title, author, genre, currentPg, totalPg) VALUES($1,$2,$3,$4,$5,$6)", array($coverUrl, $title, $author, $genre, $currentPages, $totalPages));

pg_close($dbconn);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM