Php web表单不从mysql表中添加或删除

Php web表单不从mysql表中添加或删除,php,mysql,webforms,smarty,Php,Mysql,Webforms,Smarty,我正在使用PHP和smarty模板开发一个基本的web表单,允许用户从mysql数据库表中添加和删除数据。数据和表单显示良好,但当用户尝试从表中添加或删除记录时,不会发生任何情况,甚至不会显示“无法删除/添加记录”错误消息。下面是代码的样子: <?php //smartytest.php $path =$_SERVER['DOCUMENT_ROOT']; require "$path/Smarty/Smarty.class.php"; $smarty = new Smarty(); $

我正在使用PHP和smarty模板开发一个基本的web表单,允许用户从mysql数据库表中添加和删除数据。数据和表单显示良好,但当用户尝试从表中添加或删除记录时,不会发生任何情况,甚至不会显示“无法删除/添加记录”错误消息。下面是代码的样子:

<?php //smartytest.php

$path =$_SERVER['DOCUMENT_ROOT'];
require "$path/Smarty/Smarty.class.php";

$smarty = new Smarty();
$smarty->template_dir = "$path/temp/smarty/templates";
$smarty->compile_dir= "$path/temp/smarty/templates_c";
$smarty->cache_dir="$path/temp/smarty/cache";
$smarty->config_dir="$path/temp/smarty/configs";

require_once ("$path/phptest/login.php");

$db_server=mysql_connect($db_hostname, $db_username, $db_password);

if(!$db_server) die('unable to connect to MySQL: '. mysql_error());

mysql_select_db($db_database) or die("unable to select database: " . mysql_error());

if(isset($_POST['author'])&&
    isset($_POST['title'])&&
    isset($_POST['category'])&&
    isset($_POST['year'])&&
    isset($_POST['isbn']))
    {
        $author = get_post('author');
        $title = get_post('title');
        $category=get_post('category');
        $year = get_post('year');
        $isbn =get_post('isbn');

        if (isset($_POST['delete']) && $isbn !='')
        {
            $query= "DELETE FROM classics WHERE isbn='$isbn'";

            if (!mysql_query($query))
            {
                echo "DELETE failed: $query<br>". mysql_error() . "<p>";
            }
        }
        else 
        {
            $query = "INSERT INTO classics VALUES" . "('$author','$title', '$category', '$year', '$isbn')";
        if (!mysql_query($query))
        {
            echo "INSERT failed: $query<br>" . mysql_error() . "<p>";
        }   
    }
}
$query = "SELECT * FROM classics";
$result = mysql_query($query);

if (!$result) die ("Database access failed: ". mysql_error());

$rows = mysql_num_rows($result);

for ($j=0; $j < $rows; ++$j)
{
    $results[] = mysql_fetch_array($result);
}

mysql_close($db_server);

$smarty->assign('results', $results);
$smarty->display("smartytest.tpl");

function get_post($var)
{
    return mysql_escape_string($_POST[$var]);
}
?>
此外,以下是Smarty模板文件:

<html><head>
<title>Smarty Test</title>
</head><body>

<form action="/phptest/smartytest.php" method="post"><pre>
    Author <input type="text" name="author">
    Title<input type="text" name="title">
    Category<input type="text" name="category">
    Year<input type="text" name="year">
    ISBN<input type="text" name="isbn">
        <input type="submit" value="ADD RECORD">
</pre></form>

{section name=row loop=$results}
    <form action="/phptest/smartytest.php" method="post">
    <input type="hidden" name="delete" value="yes">
    <input type="hidden" name="isbn" value="{$results[row].isbn}">
    <pre>
Author    {$results[row].author}
Title     {$results[row].title}
Category  {$results[row].category}
Year      {$results[row].year}
ISBN      {$results[row].isbn}
        <input type="submit" value="DELETE RECORD"></form>
    </pre>
{/section}

 </body></html>

我最好的猜测是嵌套的if语句有问题,这可能就是为什么即使错误消息也没有显示的原因,但我已经仔细检查了代码,至少对我来说它看起来不错。有人注意到这有什么问题吗?如果有帮助的话,我可以发布页面外观的屏幕。

您正在检查是否存在作者、标题、类别、年份、isbn和isset。。检查所采取的行动类型之前。没有设置像$\u POST['author']这样的变量。原因是页面上有多个表单。只有提交的表单中的输入才能在$\u POST中使用

在这种情况下,您可以简单地执行以下操作:

if (isset($_POST['delete'])
    && isset($_POST['isbn'])
    && strlen($_POST['isbn'])) {
    // Remove record code here
}
else {
    // Add record code here
}