2014-04-14 2 views
-3

일부 편집 기능을 만들었습니다. 제출 단추를 누르면 양식이 새로 고쳐지고 양식에 입력 한 새로운 정보로 데이터베이스가 업데이트되지 않습니다.PHP - 양식을 제출할 때 데이터베이스 업데이트

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 
} 
+0

너무 섞여서 정리해야합니다. –

답변

0

이 원 -

mysql_query("UPDATE options SET option_name='".$option_name."', option_value='".$option_value."' WHERE option_id='".$id."'"); 
와 쿼리 구문을 정정

및 sele에 동일 ct query-

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

희망이 있으면 도움이 될 것입니다.

+0

나는 그에 맞게 업데이트했는데, 그 코드를 자세히 살펴볼 수는 없습니다. 나는 모든 IFs 등 basiclly에서 echo'ing 시도했지만 아무것도 작동하지 않는 것. – hedqvist

+0

해결 : 이름이 누락되었습니다. 내 버튼에 "제출"=. – hedqvist

관련 문제