dragon_9000 2016-02-19 22:37
浏览 117
已采纳

执行Mysqli查询时出错500

I've been working on a tiny form fetch and insert to MySQL database system with PHP. I've made this type of code work before on a local WAMP server and now I'm trying to do it for real this time on my hosting, but I keep getting the 500 Error Internal Server error with the following code.

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="Main_Stylesheet.css">
    <script   src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <title>Contact</title>
</head>
<body>
    <nav class="navbar navbar-default" id="Body_Nav" style="margin-bottom: none;">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="index.php">Filosofie in Amsterdam</a>
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#myNavbar">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span> 
                </button>
            </div>
            <div class="collapse navbar-collapse" id="myNavbar" >
            <ul class="nav navbar-nav">
                <li><a href="index.php">Home</a></li>
                <li><a href="bestel.php">Bestel</a></li>
                <li><a href="preview.php">Preview</a></li>
                <li class="active"><a href="contact.php">Contact</a></li>
                <li><a href="us.php">Over Ons</a></li>
                <li><a href="donate.php">Doneren</a></li>
            </ul>
            </div>
        </div>
    </nav>
    <div class="container-fluid" id="Contact_Main_Header">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="text-center" id="Contact_Main_Header_H1">Contact</h1>
            </div>
        </div>    
    </div>
    <div class="container">
        <div class="row">
            <div class="col-lg-6">
                <h2 class="text-center">Informatie</h2>
            </div>
            <div class="col-lg-6">
                <h2 class="text-center">Nieuwsbrief</h2>
                <p class="text-justify">Op de hoogte zijn van voortgang of het laatste nieuws? Geef je dan op via de nieuwsbrief hieronder:</p>
                <form method="post" action="formvalid.php">
                    <div class="form-group">
                        <label for="Contact_Form_Email">Email Adres:</label>
                        <input type="email" name="Contact_Form_Email" class="form-control" id="Contact_Form_Email" placeholder="Plaats hier je email adres" required>
                    </div>    
                    <div class="form-group">
                        <label for="Contact_Form_First_Name">Voornaam:</label>
                        <input type="text" name="Contact_Form_First_Name" class="form-control" id="Contact_Form_First_Name" placeholder="Plaats hier je voornaam" required>   
                    </div>
                    <div class="form-group">
                        <label for="Contact_Form_Last_Name">Achternaam:</label>
                        <input type="text" name="Contact_Form_Last_Name" class="form-control" id="Contact_Form_Last_Name" placeholder="Plaats hier je voornaam" required>
                    </div>    
                    <button type="submit" class="btn btn-default">Verzenden</button>
                </form>
            </div>
        </div>
    </div>
</body>
</html>

And the PHP:

 <?php
 $Server = "localhost";
 $Username = "filoso1q_admin";
 $PW = "Foxtrot15";
 $DB = "filoso1q_nieuwsbrief";
 $connection = mysqli_connect($Server, $Username, $PW, $DB);

 if($connection = false) {
     die("Error: " . mysqli_error_connect());
 }  

 $Email = $_POST("Contact_Form_Email");
 $First_Name = $_POST("Contact_Form_First_Name");
 $Last_Name = $_POST("Contact_Form_Last_Name");

 $sql = "INSERT INTO `Users` ('User_ID', 'Email', 'First_Name', 'Last_Name')  VALUES (NULL, '$Email', '$First_Name',  '$Last_Name');";

 mysqli_query($sql);

 mysqli_close($connection);
 ?>

I'm pretty certain that some of this should work, but for some reason I can only run queries in PHPmyadmin itself. The host I have uses the Cpanel webadmin for the webserver.

  • 写回答

1条回答 默认 最新

  • duanpi7107 2016-02-19 23:11
    关注

    I changed my mind and decided to post an answer after all.

    You're assigning if($connection = false) with one equal sign, rather than comparing with two if($connection == false).

    Or, consult the manual on mysqli_connect() for more options:

    You're using incorrect identifier qualifiers for the columns in your query. They should be ticks and not regular quotes.

    Sidenote about the ticks: You don't even need those, since you are not using any of MySQL's reserved words, nor do they contain spaces or hyphens, or any other character that MySQL would complain about, so you can safely remove those for the column names.

    Then you used the incorrect type of brackets for your POST arrays. You used (...) rather then square ones [...].

    You also did not pass the connection to your query, which needs to be the first parameter.

    Rewrite with error checking on the query:

    <?php
     $Server = "localhost";
     $Username = "filoso1q_admin";
     $PW = "Foxtrot15";
     $DB = "filoso1q_nieuwsbrief";
     $connection = mysqli_connect($Server, $Username, $PW, $DB);
    
     if($connection == false) {
         die("Error: " . mysqli_error_connect());
     }  
    
     $Email = $_POST["Contact_Form_Email"];
     $First_Name = $_POST["Contact_Form_First_Name"];
     $Last_Name = $_POST["Contact_Form_Last_Name"];
    
     $sql = "INSERT INTO `Users` (`User_ID`, `Email`, `First_Name`, `Last_Name`)  VALUES (NULL, '$Email', '$First_Name',  '$Last_Name');";
    
     mysqli_query($connection, $sql) or die(mysqli_error($connection));
    
     mysqli_close($connection);
     ?>
    

    Add error reporting to the top of your file(s) which will help find errors.

    <?php 
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    // Then the rest of your code
    

    Sidenote: Displaying errors should only be done in staging, and never production.

    and or die(mysqli_error($con)) to mysqli_query() for debugging.

    Also make sure that you have the correct types chosen for your columns and that their lengths are long enough to accommodate the data.

    Plus, make sure that your User_ID column does accept NULL values and isn't an AUTO_INCREMENT. Otherwise, using NULL will fail.

    You would need to change your query to VALUES ('', '$Email', ...


    Your present code is open to SQL injection. Use mysqli_* with prepared statements, or PDO with prepared statements.


    An 500 error is a server error and can be viewed in your logs. Either check those, or use error reporting as outlined above in order to be viewed on screen.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大