【问题标题】:MySQL multi SELECT query into form and then UPDATEMySQL 多 SELECT 查询到表单然后更新
【发布时间】:2015-09-02 03:31:34
【问题描述】:

我想要完成的是

  1. 将数据从 MySQL 数据库获取到一个表中,该表仅包含用户忘记登录或注销的行(完成)
  2. 更改表中的数据(完成)
  3. 点击提交,将表中所有数据更新到MySQL数据库中

我已经让每个输入/值都有自己的名称/ID,例如。 (name0, name1, name2)

我只是不太确定如何将所有数据从表中获取到 MySQL 数据库

这是我当前的代码:

$query = "SELECT ID, name, date, start, end, location, leavetype, comments FROM test WHERE `start`='' AND `date`<>'$today_date' OR `end`='' AND `date`<>'$today_date'";
$result=mysql_query($query);
$num=mysql_numrows($result);

?>
<section>
<form>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th hidden>
<font face="Arial, Helvetica, sans-serif">Datename</font>
</th>
<th>
<font face="Arial, Helvetica, sans-serif">ID</font>
</th>
<th>
<font face="Arial, Helvetica, sans-serif">Name</font>
</th>
<th width="80px">
<font face="Arial, Helvetica, sans-serif">Date</font>
</th>
<th>
<font face="Arial, Helvetica, sans-serif">Start</font>
</th>
<th>
<font face="Arial, Helvetica, sans-serif">End</font>
</th>
<th style="width:120px;">
<font face="Arial, Helvetica, sans-serif">Location</font>
</th>
<th style="width:80px;">
<font face="Arial, Helvetica, sans-serif">Leave-Type</font>
</th>
<th>
<font face="Arial, Helvetica, sans-serif">Comments</font>
</th>
</tr>
<?php 
$i=0;while ($i < $num) {$f1=mysql_result($result,$i,"ID");
$f2=mysql_result($result,$i,"name");
$f3=mysql_result($result,$i,"date");
$f4=mysql_result($result,$i,"start");
$f5=mysql_result($result,$i,"end");
$f6=mysql_result($result,$i,"location");
$f7=mysql_result($result,$i,"leavetype");
$f8=mysql_result($result,$i,"comments"); 
echo '
<tr>
<td hidden>
<font face="Arial, Helvetica, sans-serif">';echo "$f3$f2";echo '</font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"> <input type="text" name="ID'; echo $i; echo '" style="width:35px;" Value="'; echo $f1;echo '" style="width:80px" disabled></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"> <input type="text" name="name'; echo $i; echo '" style="width:120px;" Value="'; echo $f2;echo '" style="width:80px" disabled></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"> <input type="text" name="date'; echo $i; echo '" pattern="[0-9]{2}/[0-9]{2}/[0-9]{4}$" style="width:65px;" Value="'; echo $f3;echo '" style="width:70px" disabled>    </font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"> <input type="text" name="start-time'; echo $i; echo '" pattern="[0-9]{2}:[00,15,30,45]{2}$" style="width:35px;" Value="'; echo $f4;echo '" style="width:35px"></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"> <input type="text" name="end-time'; echo $i; echo '" pattern="[0-9]{2}:[00,15,30,45]{2}$" style="width:35px;" Value="'; echo $f5;echo '" style="width:35px"></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"> <select name="location'; echo $i; echo '"><option value="'; echo $f6;echo '">'; echo $f6;echo '</option></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"> <select name="leavetype'; echo $i; echo '"><option value="'; echo $f7;echo '">'; echo $f7;echo '</option></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"> <input type="text" name="rcomment'; echo $i; echo '" style="width:120px;" Value="'; echo $f8;echo '" style="width:70px"></font>
</td>
</tr>    '; 
++$i;}

if ($i=="0"){echo '<tr><td colspan="9"><p style="color: Green; font-size: 35px; font-weight:bold; text-align: center">All signed in-out correctly!</p></td></tr>';}
else if ($i > "0"){ echo '<tr><td colspan="9"><input name="Submit" type="Submit" Value="Update">';}
?>

如何设置第 2 行和后续行的值

$users_name = $_POST['name']; //员工姓名

$users_leavetype = $_POST['leavetype']; //休假类型(如:年假)

$users_date= date('d/m/Y', strtotime($_POST['rdate'])); //日期

$users_start = $_POST['开始时间']; //开始时间

$users_finish = $_POST['结束时间']; //结束时间

$users_location = $_POST['location']; //他们本来应该在的位置

$users_comment = $_POST['rcomment']; //管理员评论

我必须添加每个变量吗?或者有没有更简单的方法

注意:第二批代码来自另一个表单,因此不会针对此表单进行更改,只是显示我正在使用的格式,并且不确定如何根据需要进行编辑

感谢所有帮助

【问题讨论】:

    标签: php mysql forms variables post


    【解决方案1】:

    您在mysql_num_row() 中有错误;它的

    $num=mysql_num_rows($result);
    

    不是

    $num=mysql_numrows($result);
    

    阅读手册mysql_num_row()

    【讨论】:

    • 嗯,好的,会改变它,看看它是否仍然有效。因为它当前正在正确接收数据。
    • 在您提到的页面底部“注意:为了向后兼容,可能会使用以下已弃用的别名:mysql_numrows()”
    【解决方案2】:

    成功,

    我自己想通了[= 我必须在表的末尾添加一行,其最终值为 $i

    <tr hidden> 
    <td hidden>
    <input type="text" name="ivalue" style="width:120px;" Value="'; echo $i;echo '" style="width:70px" hidden></font>
    </td>
    </tr>
    

    然后这是在我的

    <form action="senddata.php" file>
    

    由于某种原因我不得不 ++$max 不知道为什么,也许如果我将它设置为“while ($i $max) 它可能没有它也可以工作,但无论如何这就是我所做的

    $i="0";
    $max=$_POST['ivalue'];    //total number of rows
    
    ++$max;
    while ($i < $max)
    {
    $ID0    =   "ID$i";
    $name0  =   "name$i";
    $date0  =   "rdate$i";
    $start0 =   "start-time$i";
    $end0   =   "end-time$i";
    $location0 =    "location$i";
    $leavetype0=    "leavetype$i";
    $comment0  =    "comment$i";
    
    
    $users_ID = $_POST[$ID0];
    $users_name = $_POST[$name0];       
    $users_date= $_POST[$date0];            
    $users_start = $_POST[$start0];
    $users_end = $_POST[$end0];
    $users_location = $_POST[$location0];
    $users_leavetype = $_POST[$leavetype0];
    $users_comment = $_POST[$comment0];
    
    $users_ID = mysql_real_escape_string($users_ID);    
    $users_name = mysql_real_escape_string($users_name);
    $users_date= mysql_real_escape_string($users_date);
    $users_start = mysql_real_escape_string($users_start);
    $users_end = mysql_real_escape_string($users_end);
    $users_location = mysql_real_escape_string($users_location);
    $users_leavetype = mysql_real_escape_string($users_leavetype);
    $users_comment= mysql_real_escape_string($users_comment);
    
    mysql_query("INSERT INTO `mydb_mydb`.`mydb` (`datename` `ID`, `name`, `location`, `start`, `end`, `date`, `Leavetype`, `comments`) VALUES ('$users_leavetype$users_date$users_name', '$users_ID', '$users_name', '$users_location', '$users_start', '$users_end', '$users_date', '$users_leavetype', '$users_comment') ON DUPLICATE KEY UPDATE `start`='$users_start', `end`='$users_end', `Leavetype`='$users_leavetype', `comments`= '$users_comment' ");
    ++$i;}
    
    mysql_close();
    ?>
    

    注意:日期名称是我的唯一列,因此有人不能在一天内登录/注销两次

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-06
      • 2010-11-26
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      相关资源
      最近更新 更多