2012-03-22 135 views
0

我有一个php代码,我正在登录的用户名。然后表单来了,然后我需要保存在db中登录的用户名。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表单来这里...........

<?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()); 


?> 

但我声明为登录名是没有得到插入分贝$用户。请帮忙。

这是我的全部代码:

<?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!'; 
} 
} 
?> 
+0

使用了XDebug,并告诉我们,你有错误消息。 – 2012-03-22 09:14:01

+0

此外,这些字母甚至无法执行,不要称之为“代码”。先看书。 – 2012-03-22 09:15:23

+0

其他字段是否被插入?如果你在查询之前使用var_dump($ user),输出是什么? $ user的变量类型是否与数据库中的变量类型兼容? – Andy 2012-03-22 09:17:01

回答

0

试试这个(添加"),因为评论可能有特殊字符:

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

你有一场名为 “提交”? 我的猜测是,检查表单是否发布不正确。 这就是为什么你查询永远不会执行。

尝试类似:

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