PHP - 提交表单时更新数据库

时间:2014-04-14 12:34:50

标签: php mysql forms post submit

我创建了一些编辑功能问题是,当我按下提交按钮时,它会刷新表单,并且不会使用我在表单中输入的新信息更新数据库。

我似乎无法找到阻碍 edit_options.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['id'])){
        // get form data, making sure it is valid
        $id = $_POST['id'];
        $option_name = mysql_real_escape_string($_POST['option_name']);
        $option_value = mysql_real_escape_string($_POST['option_value']);


        // check that prodname/color fields are both filled in
        if ($option_name == '' || $option_value == '')
        {

        // generate error message
        $error = 'ERROR: Please fill in all required fields!';

        //error, display form
        renderForm($id, $option_name, $option_value, $error);
        }
        else
        {
        // save the data to the database
        mysql_query("UPDATE options SET option_name='$option_name', option_value='$option_value' WHERE option_id='$id'")
        or die(mysql_error()); 

        // once saved, redirect back to the view page
        header("Location: view_products.php"); 
        }
    } else{
     // if the 'id' isn't valid, display an error
     echo 'Error: Ogiltigt ID!';
    }
 }
// if the form hasn't been submitted, get the data from the db and display the form
else{
     // 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['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0){
         // query db
         $id = $_GET['id'];
         $result = mysql_query("SELECT * FROM options WHERE option_id=$id")
         or die(mysql_error()); 
         $row = mysql_fetch_array($result);

         // check that the 'id' matches up with a row in the databse
         if($row){
         // get data from db
         $name = $row['option_name'];
         $option_value = $row['option_value'];       
         // show form
         renderForm($id, $name, $option_value, '');
         }
         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: ID saknas eller är ogiltigt';
     }
 }

包含标题功能的FORM ...

<?php
session_start();
if(!isset($_SESSION['myusername'])){
    header("location:login.php");
}
//kollar om användaren är inloggad och har adminbehörigheter annars skickas personen till inloggningsidan
if(isset($_SESSION['permission']) && $_SESSION['permission'] >= 4){
}
else{
header("location:login.php");
}
// connect to the database
include '/include/config.php';

    // get results from database
    $result = mysql_query("SELECT * FROM options") 
        or die(mysql_error());

function renderForm($id, $option_name, $option_value, $error)
 {
 ?>
<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <html lang="sv">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Ändra inställning - Petrolia Lagersystem</title>

    <!-- Core CSS - Include with every page -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="font-awesome/css/font-awesome.css" rel="stylesheet">
    <link href="css/custom-style.css" rel="stylesheet">

    <!-- Page-Level Plugin CSS - Blank -->

    <!-- SB Admin CSS - Include with every page -->
    <link href="css/sb-admin.css" rel="stylesheet">

</head>

<body>

    <div id="wrapper">

        <?php include "top_menu.php"; ?>
        <?php include "side_menu.php"; ?>

        <div id="page-wrapper">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">Inställningar</h1>
                    <?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="id" value="<?php echo $id; ?>"/>
                         <div>
                         <p><strong>ID:</strong> <?php echo $id; ?></p>
                         <strong>Inställning: *</strong> <input class="form-control" type="text" name="option_name" value="<?php echo $option_name; ?>"/><br/>
                         <strong>Värde: *</strong> <input class="form-control" type="text" name="option_value" value="<?php echo $option_value; ?>"/><br/>
                         <p>* obligatoriskt</p>
                         <button type="submit" class="btn btn-primary"><i class="fa fa-save fa-fw"></i>Spara ändring</button>
                         </div>
                    </form> 
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->

    <!-- Core Scripts - Include with every page -->
    <script src="js/jquery-1.10.2.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/plugins/metisMenu/jquery.metisMenu.js"></script>

    <!-- Page-Level Plugin Scripts - Blank -->

    <!-- SB Admin Scripts - Include with every page -->
    <script src="js/sb-admin.js"></script>

    <!-- Page-Level Demo Scripts - Blank - Use for reference -->

</body>

</html>
 <?php
 }

1 个答案:

答案 0 :(得分:0)

使用此方法更正您的查询语法 -

mysql_query("UPDATE options SET option_name='".$option_name."', option_value='".$option_value."' WHERE option_id='".$id."'");

和选择查询相同 -

$result = mysql_query("SELECT * FROM options WHERE option_id='".$id."'");

希望它会对你有所帮助。