php:变量值未保存在db中

时间:2012-03-22 09:11:38

标签: php

我有一个PHP代码,我正在登录用户名。然后表单来了,然后我需要在db。中保存登录的用户名。

<?PHP
 function renderForm($id, $task, $comments, $assignee, $priority, $status, $dataum1, $dataum2, $error )
 {
require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
    $fgmembersite->RedirectToURL("login.php");
    exit;
}
$user = $fgmembersite->UserFullName();
?>

Html Form来到这里...........

<?php
 }



 // connect to the database
 include('configdb1.php');

 // check if the form has been submitted. If it has, process the form and save it to the database
 if (isset($_POST['submit']))
 { 

 mysql_query("INSERT INTO comments (comment_id, comments, user) VALUES ('$id', '$text', '$user')")
 or die(mysql_error()); 


?>

但是我声明为登录名的$ user没有插入到db中。请帮忙。

这是我的全部代码:

<?PHP
 function renderForm($id, $task, $comments, $assignee, $priority, $status, $dataum1, $dataum2, $error )
 {
require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
    $fgmembersite->RedirectToURL("login.php");
    exit;
}

?>


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<link href="style.css" rel="stylesheet" type="text/css" />
   <script language="javaScript" type="text/javascript" src="calendar.js"></script>
<script src="rollover.js" type="text/javascript"></script>
<script src="datetimepicker.js" type="text/javascript"></script>
</head>
<body id="page1">
<table class="main">
    <tr>
        <td id="header">
            <table class="centre">
                        <td class="column3">
                                                <table>

                                                      <tr>
                                                      <td >
                                                      </br>
                                                      <color = 'white'>
                                                      Welcome back   <?= $fgmembersite->UserFullName(); ?>! , <a href='logout.php'>Logout</a></p>
                                                      </br>
                                                      </td>
                                                    </tr>

                                                </table>

                <tr>
                 <td class="row2"><a href="home.php"><img src="images/m1.gif" alt="" color = "white" width="112" height="33" id="Image9"</a><a href="myactivity.php"><img src="images/m2_1.gif" alt=" " width="112" height="33" id="Image10" /></a><a href="ba_report.php"><img src="images/m3.gif" alt=" " width="112" height="33" id="Image11" /></a><a href="automation_report.php"><img src="images/m4.gif" alt=" " width="155" height="33"  id="Image12" /></a><a href="functional_report.php"><img src="images/m5.gif" alt=" " width="148" height="33" id="Image13" /></a><a href="setting.php"><img src="images/m6.gif" alt=" " width="112" height="33" id="Image14" /></a></td>
                </tr>
            </table>

        </td>
    </tr>
    <tr>
        <td id="content">
            <table>
                <tr>

                    <td class="column1"><img src="images/spacer.gif" alt="" width="23" height="1" /></td>
                    <td class="column2">

                        <table>
 &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp <a href="myactivity.php"><img src="images/t1.gif" alt="" color = "white" width="112" height="33" id="Image9"  /></a> &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp <a href="view.php"><img src="images/t2.gif" alt=" " width="112" height="33" id="Image10" /></a><br /><br /><br /><br /><br /><br />



                            <div class="signup_form">
 <form action="" method="post">
  <input type="hidden" name="id" value="<?php echo $id; ?>"/>
                            <p><strong>ID:</strong> <?php echo $id; ?></p>
                                 <tr><font size="2" face="tahoma" color="black"><b>Enter the Task </b></br></br> </tr><input name="task" readonly value= <?php echo $task; ?> type="text" size = "127" input style="height:30px;font-size:13;" ...../><br /><br /><br />
                                <tr><font size="2" face="tahoma" color="black"><b>Enter Task Description </b></br></br> </tr><textarea readonlyname="comments" ROWS="10" Cols = "91"> <?php echo $comments; ?></textarea> <br/><br /><br /><br />
   <?php
include('configdb.php');
    $query = "SELECT username FROM users";

    $result = mysqli_query($mysqli,$query) or die(mysqli_error($mysqli));
    $assignee = "<select name='user'> width = '200'";
    while($row = mysqli_fetch_assoc($result)) {
    $assignee .= "\r\n<option width = '200'  value='{$row['username']}'>{$row['username']}</option>";
    }
    $assignee .= "\r\n</select> ";

    ?>                              
<b>Assignee: &nbsp &nbsp &nbsp &nbsp </b><select name = "assignee" value = <?php echo $assignee ?></select> &nbsp &nbsp  &nbsp &nbsp  &nbsp &nbsp 

<b>Priority: &nbsp &nbsp &nbsp &nbsp </b><select name = "type" id="type">
    <option <?php if ($type == 'MAC OS') { echo 'selected="selected"'; } ?>>MAC OS</option>
    <option <?php if ($type == 'WINDOWS OS') { echo 'selected="selected"'; } ?>>WINDOWS OS</option>
    <option <?php if ($type == 'GENERAL TASK') { echo 'selected="selected"'; } ?>>GENERAL TASK</option>
</select> &nbsp &nbsp  &nbsp &nbsp  &nbsp &nbsp 



<b>Priority: &nbsp &nbsp &nbsp &nbsp </b><select name = "priority" id="priority">
    <option <?php if ($priority == 'Low') { echo 'selected="selected"'; } ?>>Low</option>
    <option <?php if ($priority == 'Medium') { echo 'selected="selected"'; } ?>>Medium </option>
    <option <?php if ($priority == 'High') { echo 'selected="selected"'; } ?>>High</option>
</select> &nbsp &nbsp  &nbsp &nbsp  &nbsp &nbsp 


<b>Status: &nbsp &nbsp &nbsp &nbsp </b>    <select name = "status" id="status">
    <option <?php if ($status == 'Assigned') { echo 'selected="selected"'; } ?>>Assigned</option>
    <option <?php if ($status == 'Yet to Start') { echo 'selected="selected"'; } ?>>Yet to Start </option>
    <option <?php if ($status == 'In Progress') { echo 'selected="selected"'; } ?>>In Progress</option>
    <option <?php if ($status == 'Completed') { echo 'selected="selected"'; } ?>>Completed</option>
     <option <?php if ($status == 'Blocked') { echo 'selected="selected"'; } ?>>Blocked</option>
</select><br/><br /><br /><br />
<b>Start Date :&nbsp &nbsp </b><input type="text" size = "25" input style="height:25px;font-size:13;" name="datum1" id="datum1" value="<?php echo $dataum1; ?>"><a href="javascript:NewCal('datum1','ddmmmyyyy',true,24)"><img src="cal.gif"></a>&nbsp &nbsp &nbsp &nbsp  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp  &nbsp &nbsp &nbsp
<b>End Date :&nbsp &nbsp </b><input type="text"  size = "25" input style="height:25px;font-size:13;" name="datum2" id="datum2" value="<?php echo $dataum2; ?>"><a href="javascript:NewCal('datum2','ddmmmyyyy',true,24)"><img src="cal.gif"></a><br/><br /><br /><br />
                            <input name="submit" type="submit" value="submit"/>
                            </form>

                            </div>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>    
        <td id="footer">
        </td>

</table>

</body>
</html>

 <?php
 }



 // connect to the database
 include('configdb1.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'];
 $task = $_POST['task'];
 $comments = $_POST['comments'];
$assignee = $_POST['assignee'];
 $priority = $_POST['priority'];
 $status = $_POST['status'];
 $dataum1 = $_POST['datum1'];
 $dataum2 = $_POST['datum2'];
    // check that firstname/lastname fields are both filled in
if ($task == '' || $comments == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 //error, display form
 renderForm($id, $task, $comments, $assignee, $priority, $status, $dataum1, $dataum2, $error );
 }
 else
 {
 // save the data to the database
 mysql_query("UPDATE work SET task='$task', comments='$comments', assignee='$assignee', priority='$priority', status='$status', dataum1='$dataum1', dataum2='$dataum2' WHERE id='$id' ")
 or die(mysql_error()); 

 // once saved, redirect back to the view page
 header("Location: view.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['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
 {
 // query db
 $id = $_GET['id'];
 $result = mysql_query("SELECT * FROM work WHERE 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
 // get data from db
 $task = $row['task'];
 $comments = $row['comments'];
  $assignee = $row['assignee'];
   $priority = $row['priority'];
    $status = $row['status'];
     $dataum1 = $row['dataum1'];
      $dataum2 = $row['dataum2'];

 // show form
 renderForm($id, $task, $comments, $assignee, $priority, $status, $dataum1, $dataum2,'');
 }
 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!';
 }
 }
?>

2 个答案:

答案 0 :(得分:0)

尝试此操作(添加"),因为评论可能有特殊的字符:

mysql_query("INSERT INTO comments (comment_id, comments, user) VALUES ('".$id."', '".$text."', '".$user."')") or die(mysql_error()); 

答案 1 :(得分:0)

你有一个名为“提交”的字段吗? 我的猜测是检查表单是否发布是不正确的。 这就是你永远不会执行查询的原因。

尝试类似:

if (count($_POST) > 0)
// QUERY