无法发送到数据库

时间:2014-11-16 08:56:33

标签: php html post mysqli get

我一直在尝试制作一个管理面板。对于设置,我想在一个文件中运行所有脚本,这样就不会弄乱文件夹。但是,当我尝试添加/编辑一行时,它不会让我。

注意:我可以从数据库中获取所有行而不会出现问题。它也不会出错,所以我无法看到它。

这是我的代码:

<? session_start();
include '../database.php';
if (!isset($_SESSION['logged'])) {
header("Location: login.php");
}
if ($_GET['action'] == 'add') {
if (!isset($_POST['submit'])) {
 ?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="main.css">
        <link rel="stylesheet" type="text/css" href="../css/menu.css">
        <link rel="stylesheet" type="text/css" href="../css/atasoy.css">
        <title>Yönetim Paneli - Berru Perde</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <h1>Add Slide</h1>
        <br>
        <br>
        <form action="<?=$_SERVER['PHP_SELF']; ?>" method="post" align="center">
        <label for="name">Name</label><br><input type="text" name="name"><br>
        <label for="link">Image Link (<a href="javascript:void(0);" onclick="window.open('upload1.php', 'Image Upload', 'width=400, height=200');">Upload Image</a>)</label><br>
        <input name="link"><br><input type="submit" name="submit" value="Add Image">
        </form>
    </body>
</html>
<?
} else {
    $name = $_POST['name'];
    $link = $_POST['link'];

    $query = $mysqli->query("INSERT INTO `carousel` VALUES ('', '$name', '$link')");

    header("Location: carousel.php");
}
} elseif ($_GET['action'] == 'edit') {
    $id = $_GET['id'];
    $result = $mysqli->query("SELECT * FROM `carousel` WHERE `id` = '$id'");
    $fetch = $result->fetch_array();
    if (!isset($_POST['submit'])) {
 ?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="main.css">
        <link rel="stylesheet" type="text/css" href="../css/menu.css">
        <link rel="stylesheet" type="text/css" href="../css/atasoy.css">
        <title>Admin Panel</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <h1>Edit Slide</h1>
        <br>
        <br>
        <form action="<?=$_SERVER['PHP_SELF']; ?>" method="post" align="center">
        <label for="name">Name</label><br><input type="text" name="name" value="<?=$fetch['name']; ?>"><br>
        <label for="link">Image Link(<a href="javascript:void(0);" onclick="window.open('upload1.php', 'Image Upload', 'width=400, height=200');">Upload Image</a>)</label><br>
        <input name="link" value="<?=$fetch['link']; ?>"><br><input type="submit" name="submit" value="Save">
        </form>
    </body>
</html>
<?
} else {
    $id = $_GET['id'];
    $name = $_POST['name'];
    $link = $_POST['link'];

    $mysqli->query("UPDATE `carousel` SET `name`='$name', `link`='$link' WHERE `id`='$id'");
    header("Location: carousel.php");
} # Delete script not done yet
} else {
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="main.css">
        <link rel="stylesheet" type="text/css" href="../css/menu.css">
        <link rel="stylesheet" type="text/css" href="../css/atasoy.css">
        <title>Admin Panel</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <h1>Slideshow Editor</h1>
        <br>
        <br>
        <ul class="addthings"><a href="carousel.php?action=add"><li><img src="http://www.widgetop.com/images/AddWidgets.png">Yeni Carousel Slaytı Ekle</li></a></ul>
        <table class="listing" align="center">
            <tr>
                <td class="darkblue">ID</td>
                <td class="darkblue">Name</td>
                <td class="darkblue">Preview</td>
                <td class="darkblue">Edit</td>
                <td class="darkblue">Delete</td>
            </tr>
            <?
            $query = $mysqli->query("SELECT * FROM `carousel` ORDER BY `id`");
            while ($fetch = $query->fetch_array()) {
            $i = $fetch['id'];
            if ($i % 2 != 0) {
            echo "<tr class='lightblue'><td>" . $fetch['id'] . "</td>";
            echo "<td>" . $fetch['name'] . "</td>";
            echo "<td><img src='" . $fetch['link'] . "' width='117.5' height='67.5'></td>";
            echo "<td><a href='carousel.php?action=edit&id=" . $fetch['id'] . "'><img src='http://docs.gimp.org/en/images/dialogs/stock-edit-16.png'></a></td>";
            echo "<td><a href='carousel.php?action=delete&id=" . $fetch['id'] . "'><img src='https://cdn2.iconfinder.com/data/icons/aspneticons_v1.0_Nov2006/delete_16x16.gif'></a></td></tr>";
            } else {
            echo "<tr class='white'><td>" . $fetch['id'] . "</td>";
            echo "<td>" . $fetch['name'] . "</td>";
            echo "<td><img src='" . $fetch['link'] . "' width='117.5' height='67.5'></td>";
            echo "<td><a href='carousel.php?action=edit&id=" . $fetch['id'] . "'><img src='http://docs.gimp.org/en/images/dialogs/stock-edit-16.png'></a></td>";
            echo "<td><a href='carousel.php?action=delete&id=" . $fetch['id'] . "'><img src='https://cdn2.iconfinder.com/data/icons/aspneticons_v1.0_Nov2006/delete_16x16.gif'></a></td></tr>";
            }
            }
            ?>
        </table>
    </body>
</html>
<? } ?>

我做错了什么?

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

您的问题肯定来自于您不想将文件分开的事实。你现在把一个乱七八糟的东西聚合成一个文件,它本身就是模型,视图和控制器的一部分。

如果您在数据库中看不到任何更改但已执行INSERT指令,那么您应该会看到错误。 $mysqli->error中的内容是什么?将此错误置于脚本的开头,激活PHP错误:

ini_set('display_errors', 1);
error_reporting(E_ALL);

我看到您的INSERT部分仅在action == add时执行。写$_REQUEST['action']代替$_GET并将<input type="hidden" name="action" value="add">放入<form>,因为您使用的是POST表单,我不确定您的网址是什么样的。删除action="<?=$_SERVER['PHP_SELF']; ?>",会给脚本增加无用的复杂性(如果未定义,操作总是指向当前URL)。

我希望我能为您提供足够的资料来帮助您调试代码;如果没有访问数据库,就很难说出来。你的bug的起源可能是很多东西。