无法从模态更新mysql记录

时间:2015-02-28 22:09:28

标签: mysql twitter-bootstrap pdo

我无法使用PDO将信息从bootstrap模式更新到mysql数据库。

我尝试了一种基本的静态方法,但似乎没有用。我单击“编辑”按钮以触发显示表格信息的模态,但是当我更改course_title时,即使代码看起来正确,它也不会执行任何操作。

这是模态的代码:

<div id="editModel" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title">Update Record: </h4>
        </div>
        <div class="modal-body">
            <p class="text-primary">Please make any changes and click 'update' to save or 'cancel' to return</p>
            <form id="updateForm" action="ManageCourses_UpdateSubmit.php" method="post">
                <div class="form-group">
                    <label for="course_code" class="control-label">Course Code:</label>
                    <input type="text" class="form-control" id="course_code" readonly="">
                </div>
                <div class="form-group">
                    <label for="course_title" class="control-label">Course Title:</label>
                    <input type="text" class="form-control" id="course_title">
                </div>
            </form>
        </div>
        <div class="modal-footer">
            <button id="cancel" type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
            <button id= "update" value="update" type="submit" class="btn btn-primary">Update</button>
        </div>
    </div>
</div>

php代码:

    <?php

    if(!isset( $_POST['course_title'], $_POST['course_code']))
    {
echo = 'invalid data has been sent';
    }

     include "db_conx.php";

     try
    {
$db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);

$db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "UPDATE course_details SET course_title = :course_title,  
WHERE course_code = :course_code";
$stmt = $pdo->prepare($sql);                                  
$stmt->bindParam(':course_title', $_POST['course_title'], PDO::PARAM_STR);       
$stmt->bindParam(':course_code', $_POST['course_code'], PDO::PARAM_INT);   
$stmt->execute(); 

/*** success message ***/
echo = 'record updated';
    }
    catch(Exception $e)
   {
/*** fail message incase something went wrong ***/
        //$message = 'Unable to process your request. Please try again later';
echo = 'Unable to process your request. Please try again later';
    }
    }
     ?>

有关我如何克服这个问题的任何建议吗?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

您的表单元素没有name属性,因此它们可能未提交给服务器:

<input type="text" class="form-control" id="course_title">

应该是:

<input type="text" class="form-control" id="course_title" name="course_title">

(相同的修复应适用于所有表单元素。)