使用外部PHP文件更新SQL表

时间:2016-02-26 23:33:19

标签: php mysql sql forms sql-update

我正在尝试使用表单更新数据库中的表,该表单显示表中的当前值,以便对其进行编辑,如下所示。这需要页面顶部的PHP / SQL“SELECT”脚本。

<form action="courseEdit.php" method="post" id="createCourse">
            <div id="wrapper1">
                <p> <?php echo $_SESSION['editcourse']['coursename'] ?> </p>

                <input id="location" type="text" name="location" value="<?php echo $_SESSION['editcourse']['location'] ?>" class="form-field" placeholder=" Location"/> 

                <input id="time" type="text" name="date" value="<?php echo $_SESSION['editcourse']['date'] ?>" class="form-field" placeholder=" Time"/> 

                <input id="minnumber" type="text" name="minnumber" value="<?php echo $_SESSION['editcourse']['minimumpeople'] ?>" class="form-field" placeholder=" Minimum Number of Students"/> 

                <input id="maxnumber" type="text" name="maxnumber" value="<?php echo $_SESSION['editcourse']['maximumpeople'] ?>" class="form-field" placeholder=" Maximum Number of Students"/> 

            </div>
            <div id="wrapper2">
                <textarea id="coursedesc" cols="50" rows="15" type="text" name="courseDesc" placeholder=" Course Description"><?php echo $_SESSION['editcourse']['description'] ?></textarea>
            </div>
            <input type="text" name="objectiveone" value="<?php echo $_SESSION['editcourse']['objective1'] ?>" class="form-field" placeholder=" Learning Objective One"/> 

             <input type="text" name="objectivetwo" value="<?php echo $_SESSION['editcourse']['objective2'] ?>" class="form-field" placeholder=" Learning Objective Two"/> 

             <input type="text" name="objectivethree" value="<?php echo $_SESSION['editcourse']['objective3'] ?>" class="form-field" placeholder=" Learning Objective Three"/> 

             <input type="text" name="objectivefour" value="<?php echo $_SESSION['editcourse']['objective4'] ?>" class="form-field" placeholder=" Learning Objective Four"/> 

             <input type="text" name="objectivefive" value="<?php echo $_SESSION['editcourse']['objective5'] ?>" class="form-field" placeholder=" Learning Objective Five"/> 

            <input onclick="" type="submit" value="Register" class="button" id="createCourseBtn"/>

 </form>

在提交时,表单引用一个单独的页面,其中刚刚编辑过的表单将更新之前显示的表格值。该代码发布在下面,但据我所知,一旦单击提交按钮,它将移至“courseEdit.php”并忘记所有输入值。

 <?php
require("common.php");
$courseid = $_SESSION['editcourse']['courseID'];

$query = " 
            UPDATE courses
            SET 
                coursename = :coursename,
                location = :location, 
                date = :date,
                minimumpeople = :minnumber, 
                maximumpeople = :maxnumber, 
                description = :courseDesc,
                objective1 = :objectiveone,
                objective2 = :objectivetwo,
                objective3 = :objectivethree,
                objective4 = :objectivefour,
                objective5 = :objectivefive,
            WHERE
                courseID = $courseid
        "; 


        $query_params = array( 
            ':coursename' => $_POST['coursename'],
            ':location' => $_POST['location'], 
            ':date' => $_POST['date'],  
            ':minnumber' =>  $_POST['minnumber'],
            ':maxnumber' =>  $_POST['maxnumber'], 
            ':courseDesc' => $_POST['courseDesc'],
            ':objectiveone' => $_POST['objectiveone'],
            ':objectivetwo' => $_POST['objectivetwo'],
            ':objectivethree' => $_POST['objectivethree'],
            ':objectivefour' => $_POST['objectivefour'],
            ':objectivefive' => $_POST['objectivefive'],
        ); 

        try 
        { 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex) 
        {   
            die("Failed to run query: " . $ex->getMessage()); 
        } 

        header("Location: myprofile.php"); 

        die("Redirecting to registercourse.php");  

?>

有没有办法将它放在初始页面上,让它只在提交时运行?或者,将值传递到下一个php文件,以便它们可以正确更新?提前谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用表单顶部的php函数isset()来检查用户是否提交了表单 码       

   if(isset($_POST['location'])){  //use any valid field name
        $courseid = $_SESSION['editcourse']['courseID'];

        $query = " 
        UPDATE courses
        SET 
            coursename = :coursename,
            location = :location, 
            date = :date,
            minimumpeople = :minnumber, 
            maximumpeople = :maxnumber, 
            description = :courseDesc,
            objective1 = :objectiveone,
            objective2 = :objectivetwo,
            objective3 = :objectivethree,
            objective4 = :objectivefour,
            objective5 = :objectivefive,
        WHERE
            courseID = $courseid
    "; 


    $query_params = array( 
        ':coursename' => $_POST['coursename'],
        ':location' => $_POST['location'], 
        ':date' => $_POST['date'],  
        ':minnumber' =>  $_POST['minnumber'],
        ':maxnumber' =>  $_POST['maxnumber'], 
        ':courseDesc' => $_POST['courseDesc'],
        ':objectiveone' => $_POST['objectiveone'],
        ':objectivetwo' => $_POST['objectivetwo'],
        ':objectivethree' => $_POST['objectivethree'],
        ':objectivefour' => $_POST['objectivefour'],
        ':objectivefive' => $_POST['objectivefive'],
    ); 

    try 
    { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    {   
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    //your can use meta tag to redirect user to another page
    ?>  
        <meta http-equiv="refresh" content="0; url=myprofile.php">
    <?php
   }
   ?>
   <!-- form details -->

请记住删除表单中的操作网址