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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧