PHP MySQL插入表

时间:2016-04-15 20:43:15

标签: php html mysql

我已经按照教程编写了确切的代码,但我似乎无法使其正常工作

当我提交表格时,没有任何内容被放入我的表格

我似乎无法找到问题,我改变了一堆东西,但似乎无法找到答案。

如果有人可以查看我的代码并帮我解决问题,我将非常感谢

代码

<form class="form" method="post" action="index.php">
            <h2>
              Post new account...
            </h2>
              <div class="form-group">
                <label for="title">Title</label>
                <input required type="text" class="form-control" name="title"  placeholder="Enter a title for the account...">
              </div>
              <div class="form-group">
                <div class="row">
                  <div class="col-md-6">
                    <label for="server">Server</label>
                    <input required type="text" class="form-control" name="server"  placeholder="Enter Server...">
                  </div>
                   <div class="col-md-6">
                      <label for="price">Price</label>
                      <input required type="number" class="form-control" name="price"  placeholder="Enter Price...">
                   </div>  
                </div>
              </div>
              <div class="form-group">  
              <div class="row">
                  <div class="col-md-6">
                    <label for="Energy">Energy</label>
                    <input required type="number" class="form-control" name="energy"  placeholder="Enter Energy...">
                  </div>
                   <div class="col-md-6">
                      <label for="Stamina">Stamina</label>
                      <input required type="number" class="form-control" name="stamina"  placeholder="Enter Stamina...">
                   </div>  
                </div>
              </div>
              <div class="form-group">  
              <div class="row">
                  <div class="col-md-6">
                    <label for="Jewels">Jewels</label>
                    <input required type="number" class="form-control" name="jewels" placeholder="Enter Jewels...">
                  </div>
                   <div class="col-md-6">
                      <label for="honor">Stamina</label>
                      <input required type="number" class="form-control" name="honor" placeholder="Enter Honor...">
                   </div>  
                </div>
              </div>
              <div class="form-group">
                <label for="image">Image URL</label>
                <input required type="text" class="form-control" name="image" placeholder="Enter the image link...">
              </div>
              <div class="form-group">
                <label for="link">Link URL</label>
                <input required type="text" class="form-control" name="link" placeholder="Enter the payment link...">
              </div>
              <div class="form-group">
                <input class="btn btn-primary btn-md" type="submit" name="submit">
              </div>
          </form>

<?php
if (isset($_POST['submit'])) {     

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("We're having problems at the moment, please come back later!");
} 

$query = "INSERT INTO accounts (title,price,energy,stamina,jewels,honor,damage,server,image,link) VALUES ('$_POST[title]','$_POST[price]','$_POST[energy]','$_POST[stamina]','$_POST[jewels]','$_POST[honor]','$_POST[damage]','$_POST[server]','$_POST[image]','$_POST[link]')";

mysql_query($query,$conn);
mysql_close($conn);
};
?>

1 个答案:

答案 0 :(得分:0)

检查您的连接:

if (!$conn) {
   die("Connection failed: " . mysqli_connect_error());
}

然后检查它是否插入数据库。如果有错误,它也会让你知道。

if (mysqli_query($conn, $sql)) {
   echo "New record created successfully";
} else {
   echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

另外,不要将mysql_与mysqli_混合使用。开始使用mysqli_而不是mysql_。不推荐使用mysql_。