更新表单中的问题

时间:2011-01-18 20:16:21

标签: php

当我试图处理更新表单时,我发现其中存在错误。表单效果很好,但新数据不会插入数据库,而是显示在URL

代码:

include"connection.php";

$gid = intval($_GET['id']);
$id = $_POST['product_id'];
$content = $_POST['content'];
$link = $_POST['link'];

if(isset($_POST['saveedit']) && $_POST['saveedit']=='products'){
    $update = mysql_query(" UPDATE products SET
                            content='$content',
                            link='$link'
                            WHERE product_id='$id' ") or die (mysql_error());

    if(isset($update)){
        echo "<b>update is done</b>";
    }
}

$query = mysql_query(" SELECT * FROM products WHERE product_id='".$gid."' ") or die (mysql_error());
$re = mysql_fetch_assoc($query);

if($_REQUEST['do'] == 'edit'){

echo"
<form action='".$_SERVER['PHP_SELF']."' metho='post'>
    <table>
    <tr>
    <td>Link : </td>
    <td><input type='text' name='link' value='".$re['link']."'/></td
    </tr>
    <tr>
    <td>Content :</td>
    <td><textarea name='content'>".$re['content']."</textarea></td>
    </tr>
    <tr>
    <td colspan='2'>
    <input type='submit' name='saveedit' value='save'/>
    <input type='hidden' name='id' value='".$gid."'/>
    <input type='hidden' name='productedit' value='products'/>
    </td>
    </tr>
    </table>
</form>
";
}

$data = mysql_query("SELECT * FROM products ORDER BY product_id");

echo"
    <table width='500' border='1'>
    <tr>
        <td>Link</td>
        <td>Content</td>
        <td>Option</td>
    </tr>
";

while($row = mysql_fetch_assoc($data)){
    echo"
        <tr>
        <td>".$row['link']."</td>
        <td>".$row['content']."</td>
        <td>
            <a href='edit.php?do=edit&id=".$row['product_id']."'> Edit </a>
        </td>
        </tr>
    ";
}
echo"</table>";

&GT;

3 个答案:

答案 0 :(得分:1)

我所看到的是你所拥有的形式, 使用name='saveedit'输入value='save'

并将其与其他内容进行比较

isset($_POST['saveedit']) && $_POST['saveedit']=='products'

它应该是

isset($_POST['saveedit']) && $_POST['productedit']=='products'

答案 1 :(得分:0)

PS。不是答案,但我不知道如何在主帖下面创建一个小评论。

在更新数据库之前,您应该真正清理代码,即使它位于管理部分。

答案 2 :(得分:0)

@Hannan:试试 -

<?php
include "connection.php";

$gid = intval($_GET['id']);

if(isset($_POST['saveedit']) && $_POST['productedit'] == 'products')
{
    $content = $_POST['content'];
    $link    = $_POST['link'];

    $update = mysql_query("UPDATE products SET
                           content='$content',
                           link='$link'
                           WHERE product_id='$gid'") or die (mysql_error());

    if(mysql_affected_rows() > 0)
    {
        echo "<b>update is done</b>";
    }
}

if($_GET['do'] == 'edit')
{
    $query = mysql_query("SELECT * FROM products WHERE product_id='".$gid."' ") or die (mysql_error());
    $re = mysql_fetch_assoc($query);

    echo "
    <form action='".$_SERVER['PHP_SELF']."?id=" . $gid . "' method='post'>
        <table>
        <tr>
        <td>Link : </td>
        <td><input type='text' name='link' value='".$re['link']."'/></td
        </tr>
        <tr>
        <td>Content :</td>
        <td><textarea name='content'>".$re['content']."</textarea></td>
        </tr>
        <tr>
        <td colspan='2'>
        <input type='submit' name='saveedit' value='save'/>
        <input type='hidden' name='productedit' value='products'/>
        </td>
        </tr>
        </table>
    </form>
    ";
}

$data = mysql_query("SELECT * FROM products ORDER BY product_id");

echo "
    <table width='500' border='1'>
    <tr>
        <td>Link</td>
        <td>Content</td>
        <td>Option</td>
    </tr>
";

while($row = mysql_fetch_assoc($data))
{
    echo "
        <tr>
        <td>".$row['link']."</td>
        <td>".$row['content']."</td>
        <td>
            <a href='edit.php?do=edit&id=".$row['product_id']."'> Edit </a>
        </td>
        </tr>
    ";
}
echo "</table>";
?>