doubi4435 2014-12-22 12:41
浏览 12

PHP通过SQL命令在访问表上编辑一行

I have a php file called edit_atividade.php, which runs when I click a Edit command on a table containing the Atividades table. Here is the code that it is not working:

<?php
    // creates the edit record form
    // since this form is used multiple times in this file, I have made it a function that is easily reusable
    function renderForm($cdatividade, $atividade, $error) {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>Edit Atividade</title>
    </head>
    <body>
    <?php 
        // if there are any errors, display them
        if ($error != '') {
            echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
        }
    ?> 

    <form action="" method="post">
        <input type="hidden" name="cdatividade" value="<?php echo $cdatividade; ?>"/>
        <div>
            <p><strong>CdAtividade:</strong> <?php echo $cdatividade; ?></p>
            <strong>Atividade: *</strong> <input type="text" name="atividade" value="<?php echo $atividade; ?>"/><br/>
            <p>* Required</p>
            <input type="submit" name="submit" value="Submit">
        </div>
    </form> 
    </body>
</html> 
<?php
}

// connect to the database
include "session.php";
include "connectSQL.php";

// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit'])) { 
    // confirm that the 'id' value is a valid integer before getting the form data
    if (is_numeric($_POST['cdatividade'])) {
        // get form data, making sure it is valid
        $cdatividade = $_POST['cdatividade'];
        $atividade = $_POST['atividade'];

        // check that firstname/lastname fields are both filled in
        if ($atividade == '') {
            // generate error message
            $error = 'ERROR: Please fill in all required fields!';

            //error, display form
            renderForm($cdatividade, $atividade, $error);
        } else {
            // save the data to the database
            $db->query("UPDATE Atividades SET Atividade='$atividade' WHERE CdAtividade='$cdatividade'");

            // once saved, redirect back to the view page
            header("Location: atividades.php"); 
        }
    } else {
        // if the 'id' isn't valid, display an error
        echo 'Error!';
    }
} else {
    // if the form hasn't been submitted, get the data from the db and display the form
    // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
    if (isset($_GET['cdatividade']) && is_numeric($_GET['cdatividade']) && $_GET['cdatividade'] > 0) {
        // query db
        $cdatividade = $_GET['cdatividade'];
        $result = $db->query("SELECT * FROM Atividades WHERE cdatividade=$cdatividade")
        //$row = mysql_fetch_array($result);
        $row = $result->fetch();

        // check that the 'id' matches up with a row in the databse
        if($row) {     
            // get data from db
            $atividade = $row['atividade'];

            // show form
            renderForm($cdatividade, $atividade, '');
        } else {
            // if no match, display result
            echo "No results!";
        }
    } else {
        // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
        echo 'Error!';
    }
}
?>

I took codes from this page as base to my code: http://www.killersites.com/community/index.php?/topic/1969-basic-php-system-vieweditdeleteadd-records/

Is there something missing in the code to not be working? I appreciate any help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)