Mysql查询不将数据插入数据库

时间:2016-12-05 23:19:04

标签: php html mysql database insert

我正在尝试创建一个连接到数据库的网站。我的Select语句用于显示数据,但我的insert和delete命令实际上都不会更改数据。我搜索了谷歌和stackoverflow,并试图尝试各种"答案"当我被难倒,但它还没有工作。结果,我的代码的当前状态是可能冗余代码的大杂烩。我对html和php相当新,所以我很感激帮助。如果我的格式错误,我也很抱歉,因为我之前从未使用过像stackoverflow这样的东西。以下是该网页的一个版本,适用于那些未能及时查看问题的人。编辑:修正。每个人的评论和答案都有帮助,但事实证明,当发布的前两个变量包含数字时,我有一个if语句检查以确保输入只是字母。

if(preg_match(" / [A-Z | a-z] + /",$ _POST [' name']))是罪魁祸首

<!DOCTYPE html>
<html>
<head>
<link rel = "stylesheet" type = "text/css" href = "style.css" />
</head>
<body>
    <div>
    <form method="post" action="DBADELETE.php?go" id="deleteform">
       <input name="GMOVIE" type="text" placeholder="GMOVIEID of a movie to    delete" required="">
    <input name="UMOVIE" type="text" placeholder="UMOVIEID of a movie to delete" required="">
        <input  type="submit" name="submit" value="DELETE">            
        </form>
    </div>
<div>
    <form method="post" action="DBAUPDATE.php?go" id="updateform">
       <input name="GMOVIE" type="text" placeholder="GMOVIEID of a movie to update" required="">
    <input name="UMOVIE" type="text" placeholder="UMOVIEID of a movie to update" required="">
<input name="TITLE" type="text" placeholder="TITLE of a movie to update" required="">
<input name="GENRE" type="text" placeholder="GENRE of a movie to update" required="">
<input name="RATING" type="text" placeholder="RATING of a movie to update" required="">
        <input  type="submit" name="submit" value="UPDATE">            
        </form>
    </div>
    <div id = "content1" align = "center">
    <table id = "MOVIES">
    <tr>
        <th>GMOVIEID</th>
        <th>UMOVIEID</th>
        <th>TITLE</th>
        <th>GENRE</th>
        <th>RATING</th>
    </tr>
<?php
include 'bd.php';
$conn = new mysqli( $host, $user, $pw, $bd_name); 

if ( $conn->connect_error ) {
    die( "Connection Failed: " . $conn->connect_error );
}
if(isset($_POST['submit'])){
$gmovie=$_POST['GMOVIE'];
$umovie=$_POST['UMOVIE'];
$title=$_POST['TITLE'];
$genre=$_POST['GENRE'];
$rating=$_POST['RATING'];
$gmov_safe = mysqli_real_escape_string($gmovie,$conn);
$umov_safe = mysqli_real_escape_string($umov,$conn);
$title_safe = mysqli_real_escape_string($title,$conn);
$genre_safe = mysqli_real_escape_string($genre,$conn);
$rating_safe = mysqli_real_escape_string($rating,$conn);  
$q ="INSERT INTO MOVIE (GMOVIEID, UMOVIEID, TITLE,GENRE, RATING)      VALUES($gmovie,$umovie,'$title_safe','$genre_safe','$rating_safe')"
$conn->query($q);

if ($conn->query($q) === TRUE) 
{
echo "New record created successfully";
} 
else 
{
echo "Error: " . $q . "<br>" . $conn->error;
}
}
?>
<?php
include 'bd.php';
$conn = new mysqli( $host, $user, $pw, $bd_name); 

if ( $conn->connect_error ) {
    die( "Connection Failed: " . $conn->connect_error );
}
$q = "SELECT GMOVIEID, UMOVIEID, TITLE, GENRE, RATING FROM MOVIE";
$result = $conn->query( $q );

$row_count = $result->num_rows;

while ( $row = $result->fetch_assoc() ) {
    if ( ( $row_count % 2 ) == 1 ) {
        echo "<tr>";
    } else {
        echo "<tr class=\"alt\">";
    }

    echo "<td>" . $row[GMOVIEID] . "</td>" .
        "<td>" . $row[UMOVIEID] . "</td>" .
        "<td>" . $row[TITLE] . "</td>" .
        "<td>" . $row[GENRE] . "</td>" .
        "<td>" . $row[RATING] . "</td>" .
        "</tr>" . PHP_EOL;

    $row_count--;
  }
?>
 </body>
<?php $conn->close(); ?>
</html>

1 个答案:

答案 0 :(得分:0)

问题似乎是表单在提交时重新路由到另一个页面:

action="DBAUPDATE.php?go"

action="DBADELETE.php?go"

但是insert语句和样板代码在当前页面中:DBAMOVIES.php

您需要在DBAUPDATE.php和DBADELETE.php中添加insert语句和样板代码。

只需创建以上两个页面并添加以下语句:

<html>
 <body>
  <?php include("DBAMOVIES.php"); ?>
 </body>
</html>