0

嗨,这让我感到困惑,因为复选框也是带有文本框的形式。我将以下内容作为新记录输入数据库:

<form action="processmem.php" method="post">
    <table width="800" align="center">
        <tr>
            <td width="99" height="2">&nbsp;</td>
            <td width="100" height="2">&nbsp;</td>
            <td width="100" height="2">&nbsp;</td>
            <td width="100" height="2">&nbsp;</td>
            <td width="100" height="2">&nbsp;</td>
            <td width="100" height="2">&nbsp;</td>
            <td width="100" height="2">&nbsp;</td>
            <td width="100" height="2">&nbsp;</td>
        </tr>
        <tr>
            <td colspan="3" align="right" height="30">
                Name&nbsp;&nbsp;
            </td>
            <td colspan="2" align="left">
                <input type="text" name="textfield[childname]" />
            </td>
            <td colspan="3"><p>&nbsp;</p><p>&nbsp;</p></td>
        </tr>
        <tr>
            <td colspan="3" align="right" height="30">
                Age&nbsp;&nbsp;
            </td>
            <td colspan="2" align="left">
                <input type="text" name="textfield[age]" />
            </td>
            <td colspan="3"><p>&nbsp;</p><p>&nbsp;</p></td>
        </tr>
        <tr>
            <td colspan="3" align="right" height="30">
                Parent Name&nbsp;&nbsp;
            </td>
            <td colspan="2" align="left">
                <input type="text" name="textfield[parentname]" />
            </td>
            <td colspan="3"><p>&nbsp;</p><p>&nbsp;</p></td>
        </tr>
        <tr>
            <td colspan="3" align="right" height="30">
                Email Address&nbsp;&nbsp;
            </td>
            <td colspan="2" align="left">
                <input type="text" name="textfield[email]" />
            </td>
            <td colspan="3"><p>&nbsp;</p><p>&nbsp;</p></td>
        </tr>
        <tr>
            <td colspan="3" align="right" height="30">
                Contact Number&nbsp;&nbsp;
            </td>
            <td colspan="2" align="left">
                <input type="text" name="textfield[contactno]" />
            </td>
            <td colspan="3">&nbsp;</td>
        </tr>
        <tr>
            <td colspan="8" height="30">&nbsp;</td>
        </tr>
        <tr>
            <td height="15" colspan="2">&nbsp;</td>
            <td colspan="3" align="right">&nbsp;&nbsp;</strong>
                Check box if this member is a  Ninja!
            </td>
            <td align="center">
                <input type="checkbox" name="checkbox[ninja]" value="checkbox" />
            </td>
            <td colspan="2">&nbsp;</td>
        </tr>
        <tr>
            <td colspan="8" height="30">&nbsp;</td>
        </tr>
        <tr>
            <td height="15" colspan="2">&nbsp;</td>
            <td colspan="4" align="center">
                Which subjects has this member attended?
            </td>
            <td colspan="2"></td>
        </tr>
        <tr>
            <td height="5" colspan="8">&nbsp;</td>
        </tr>
        <tr>
            <td height="15">&nbsp;</td>
            <td colspan="2" align="center">Scratch</td>
            <td colspan="2" align="center">App Inventor</td>
            <td colspan="2" align="center">HTML</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td height="15">&nbsp;</td>
            <td colspan="2" align="center">
                <input type="checkbox" name="checkbox[scratch]" value="checkbox" />
            </td>
            <td  colspan="2" align="center">
                <input type="checkbox" name="checkbox[appInventor]" value="checkbox" />
            </td>
            <td colspan="2" align="center">
                <input type="checkbox"  name="checkbox[html]" value="checkbox" />
            </td>
            <td>&nbsp;</td>
        </tr>
    </table>
</form>

它由这个脚本执行:

<?php

include("connect.php");
require_once("logincheck.php");

if(isset($_POST)){

$childname=mysql_real_escape_string($_POST['textfield']['childname']);
$age=mysql_real_escape_string($_POST['textfield']['age']);
$parentname=mysql_real_escape_string($_POST['textfield']['parentname']);
$email=mysql_real_escape_string($_POST['textfield']['email']);
$contactno=mysql_real_escape_string($_POST['textfield']['contactno']);
$ninja=isset($_POST['checkbox']['ninja'])?'yes':'no';
$scratch=isset($_POST['checkbox']['scratch'])?'yes':'no';
$appinventor=isset($_POST['checkbox']['appinventor'])?'yes':'no';
$html=isset($_POST['checkbox']['html'])?'yes':'no';

$sql="INSERT INTO tblmembers (childname, age, parentname, email, contactno, ninja, scratch, 
appinventor, html) VALUES ('$childname','$age','$parentname','$email','$contactno',
'$ninja','$scratch','$appinventor','$html')";mysql_query($sql) or die (mysql_error());

}

这一切都很好,但如果我稍后查询记录,我看不到复选框的值,如下所示:

$id=$_GET["id"];

$sql="SELECT * from tblmembers WHERE id=".$id;

$result=mysql_query($sql);

$rows=mysql_num_rows($result);

while($row =mysql_fetch_array($result))
{

$childname=$row["childname"];
$id=$row["id"];
$age=$row["age"];
$parentname=$row["parentname"];
$email=$row["email"];
$contactno=$row["contactno"];
$ninja=$row["checkbox"]["ninja"];
$scratch=$row["checkbox"]["scratch"];
$appinventor=$row["checkbox"]["appInventor"];
$html=$row["checkbox"]["html"];

print "<p align=\"center\"><strong><i>Information for $childname</i><strong></p><br>";
print "<table width=\"800\" align=\"center\">";         
print "<tr>";
print "<td width=\"99\" height=\"2\">&nbsp;</td>";
print "<td width=\"100\" height=\"2\">&nbsp;</td>";
print "<td width=\"100\" height=\"2\">&nbsp;</td>";
print "<td width=\"100\" height=\"2\">&nbsp;</td>";
print "<td width=\"100\" height=\"2\">&nbsp;</td>";
print "<td width=\"100\" height=\"2\">&nbsp;</td>";
print "<td width=\"100\" height=\"2\">&nbsp;</td>";
print "<td width=\"100\" height=\"2\">&nbsp;</td>";
print "</tr>";
print "<tr>";
print "<td colspan=\"3\" align=\"right\" height=\"30\">Name&nbsp;&nbsp;</td>";
print "<td colspan=\"2\" align=\"left\"><input type=\"text\" name=\"textfield[childname]\" 
value='".$childname."'/></td>";
print "<td colspan=\"3\"><p>&nbsp;</p><p>&nbsp;</p></td>";
print "</tr>";
print "<tr>";
print "<td colspan=\"3\" align=\"right\" height=\"30\">Age&nbsp;&nbsp;</td>";
print "<td colspan=\"2\" align=\"left\"><input type=\"text\" name=\"textfield 
[age] "value='".$age."'/></td>";
print "<td colspan=\"3\"><p>&nbsp;</p><p>&nbsp;</p></td>";
print "</tr>";
print "<tr>";
print "<td colspan=\"3\" align=\"right\" height=\"30\">Parent Name&nbsp;&nbsp;</td>";
print "<td colspan=\"2\" align=\"left\"><input type=\"text\" name=\"textfield[parentname]\"  
value='".$parentname."'/></td>";
print "<td colspan=\"3\"><p>&nbsp;</p><p>&nbsp;</p></td>";
print "</tr>";
print "<tr>";
print "<td colspan=\"3\" align=\"right\" height=\"30\">Email Address&nbsp;&nbsp;</td>";
print "<td colspan=\"2\" align=\"left\"><input type=\"text\" name=\"textfield[email]\"  
value='".$email."'/></td>";
print "<td colspan=\"3\"><p>&nbsp;</p><p>&nbsp;</p></td>";
print "</tr>";
print "<tr>";
print "<td colspan=\"3\" align=\"right\" height=\"30\">Contact Number&nbsp;&nbsp;</td>";
print "<td colspan=\"2\" align=\"left\"><input type=\"text\" name=\"textfield[contactno]\" 
value='".$contactno."'/></td>";
print "<td colspan=\"3\">&nbsp;</td>";
print "</tr>";
print "<tr>";
print "<td colspan=\"8\" height=\"30\">&nbsp;</td>";
print "</tr>";
print "<tr>";
print "<td height=\"15\" colspan=\"2\">&nbsp;</td>";
print "<td colspan=\"3\" align=\"right\">&nbsp;&nbsp;</strong>Check box if  this member is a 
Ninja!</td>";
print "<td align=\"center\"><input type=\"checkbox\" name=\"checkbox[ninja]\" value=$row  
[\"ninja\"]/></td>";
print "<td colspan=\"2\">&nbsp;</td>";
print "</tr>";
print "<tr>";
print "<td colspan=\"8\" height=\"30\">&nbsp;</td>";
print "</tr>";
print "<tr>";
print "<td height=\"15\" colspan=\"2\">&nbsp;</td>";
print "<td colspan=\"4\" align=\"center\">Which subjects has this member attended?</td>";
print "<td colspan=\"2\"></td>";
print "</tr>";
print "<tr>";
print "<td height=\"5\" colspan=\"8\">&nbsp;</td>";
print "</tr>";
print "<tr>";
print "<td height=\"15\">&nbsp;</td>";
print "<td colspan=\"2\" align=\"center\">Scratch</td>";
print "<td colspan=\"2\" align=\"center\">App Inventor</td>";
print "<td colspan=\"2\" align=\"center\">HTML</td>";
print "<td>&nbsp;</td>";
print "</tr>";
print "<tr>";
print "<td height=\"15\">&nbsp;</td>";
print "<td colspan=\"2\" align=\"center\"><input type=\"checkbox\"  name=\"checkbox[scratch]\" 
value='".$scratch."' /></td>";
print "<td  colspan=\"2\" align=\"center\"><input type=\"checkbox\"  name=\"checkbox[appInventor]
\" value='".$appinventor."' /></td>";
print "<td colspan=\"2\" align=\"center\"><input type=\"checkbox\"  name=\"checkbox[html]\" 
value='".$html."' /></td>";
print "<td>&nbsp;</td>";
print "</tr>";
print "</table>";
}

?>

最后——显然,如果我在进入数据库时​​看不到复选框被选中,我就无法编辑它或更新记录。这就是我正在使用的:

$id=$_GET["id"];

include("connect.php");
require_once("logincheck.php");

if($_GET["function"]=='update') 
{

$childname=mysql_real_escape_string($_POST['textfield']['childname']);
$age=mysql_real_escape_string($_POST['textfield']['age']);
$parentname=mysql_real_escape_string($_POST['textfield']['parentname']);
$email=mysql_real_escape_string($_POST['textfield']['email']);
$contactno=mysql_real_escape_string($_POST['textfield']['contactno']);
$ninja=isset($_POST['checkbox']['ninja'])?'yes':'no';
$scratch=isset($_POST['checkbox']['scratch'])?'yes':'no';
$appinventor=isset($_POST['checkbox']['appinventor'])?'yes':'no';
$html=isset($_POST['checkbox']['html'])?'yes':'no';

$sql2="UPDATE tblmembers SET 
childname='".$childname."',age='".$age."',parentname='".$parentname."',
email='".$email."',contactno='".$contactno."',ninja='".$ninja."',scratch='".$scratch."',
appinventor='".$appinventor."',html='".$html."' WHERE id=".$id;

mysql_query($sql2) or die (mysql_error());

print "<strong>The member details have been saved. Thank you.</strong><br><br><br>";
}
else
{

$sql="SELECT * from tblmembers WHERE id=".$id;

$result = mysql_query ($sql) or die(mysql_error());

$rows=mysql_num_rows($result);

while($row =mysql_fetch_array($result))
{

$childname=$row["childname"];
$id=$row["id"];
$age=$row["age"];
$parentname=$row["parentname"];
$email=$row["email"];
$contactno=$row["contactno"];
$ninja=$row["ninja"];
$scratch=$row["scratch"];
$appinventor=$row["appInventor"];
$html=$row["html"];

print "<form method=post action='editmember.php?function=update'>";
print "<table width=\"800\" align=\"center\">";         
print "<tr>";
print "<td width=\"99\" height=\"2\">&nbsp;</td>";
print "<td width=\"100\" height=\"2\">&nbsp;</td>";
print "<td width=\"100\" height=\"2\">&nbsp;</td>";
print "<td width=\"100\" height=\"2\">&nbsp;</td>";
print "<td width=\"100\" height=\"2\">&nbsp;</td>";
print "<td width=\"100\" height=\"2\">&nbsp;</td>";
print "<td width=\"100\" height=\"2\">&nbsp;</td>";
print "<td width=\"100\" height=\"2\">&nbsp;</td>";
print "</tr>";
print "<tr>";
print "<td colspan=\"3\" align=\"right\" height=\"30\">Name&nbsp;&nbsp;</td>";
print "<td colspan=\"2\" align=\"left\"><input type=\"text\" name=\"textfield[childname]\" 
value='".$childname."'/></td>";
print "<td colspan=\"3\"><p>&nbsp;</p><p>&nbsp;</p></td>";
print "</tr>";
print "<tr>";
print "<td colspan=\"3\" align=\"right\" height=\"30\">Age&nbsp;&nbsp;</td>";
print "<td colspan=\"2\" align=\"left\"><input type=\"text\" name=\"textfield[age]\" 
value='".$age."'/></td>";
print "<td colspan=\"3\"><p>&nbsp;</p><p>&nbsp;</p></td>";
print "</tr>";
print "<tr>";
print "<td colspan=\"3\" align=\"right\" height=\"30\">Parent Name&nbsp;&nbsp;</td>";
print "<td colspan=\"2\" align=\"left\"><input type=\"text\" name=\"textfield[parentname]\" 
value='".$parentname."'/></td>";
print "<td colspan=\"3\"><p>&nbsp;</p><p>&nbsp;</p></td>";
print "</tr>";
print "<tr>";
print "<td colspan=\"3\" align=\"right\" height=\"30\">Email Address&nbsp;&nbsp;</td>";
print "<td colspan=\"2\" align=\"left\"><input type=\"text\" name=\"textfield[email]\" 
value='".$email."'/></td>";
print "<td colspan=\"3\"><p>&nbsp;</p><p>&nbsp;</p></td>";
print "</tr>";
print "<tr>";
print "<td colspan=\"3\" align=\"right\" height=\"30\">Contact Number&nbsp;&nbsp;</td>";
print "<td colspan=\"2\" align=\"left\"><input type=\"text\" name=\"textfield[contactno]\" 
value='".$contactno."'/></td>";
print "<td colspan=\"3\">&nbsp;</td>";
print "</tr>";
print "<tr>";
print "<td colspan=\"8\" height=\"30\">&nbsp;</td>";
print "</tr>";
print "<tr>";
print "<td height=\"15\" colspan=\"2\">&nbsp;</td>";
print "<td colspan=\"3\" align=\"right\">&nbsp;&nbsp;</strong>Check box if  this member is a  
Ninja!</td>";
print "<td align=\"center\"><input type=\"checkbox\" name=\"checkbox[ninja]\" 
value='".$ninja."' /></td>";
print "<td colspan=\"2\">&nbsp;</td>";
print "</tr>";
print "<tr>";
print "<td colspan=\"8\" height=\"30\">&nbsp;</td>"; 
print "</tr>";
print "<tr>";
print "<td height=\"15\" colspan=\"2\">&nbsp;</td>";
print "<td colspan=\"4\" align=\"center\">Which subjects has this member attended?</td>";
print "<td colspan=\"2\"></td>";
print "</tr>";
print "<tr>";
print "<td height=\"5\" colspan=\"8\">&nbsp;</td>";
print "</tr>";
print "<tr>";
print "<td height=\"15\">&nbsp;</td>";
print "<td colspan=\"2\" align=\"center\">Scratch</td>";
print "<td colspan=\"2\" align=\"center\">App Inventor</td>";
print "<td colspan=\"2\" align=\"center\">HTML</td>";
print "<td>&nbsp;</td>";
print "</tr>";
print "<tr>";
print "<td height=\"15\">&nbsp;</td>";
print "<td colspan=\"2\" align=\"center\"><input type=\"checkbox\"  name=\"checkbox[scratch]\" 
value='".$scratch."' /></td>";
print "<td  colspan=\"2\" align=\"center\"><input type=\"checkbox\"  name=\"checkbox[appInventor]
\" value='".$appinventor."' /></td>";
print "<td colspan=\"2\" align=\"center\"><input type=\"checkbox\"  name=\"checkbox[html]\"   
value='".$html."' /></td>";
print "<td>&nbsp;</td>";
print "</tr>";
print "<tr>";
print "<td><input type=hidden name=id value=".$id."><br></td>";
print "<td colspan=\"4\" align=\"center\"><input type=submit 
value='&nbsp;Save&nbsp;'></td></form>";
print "<td td colspan=\"4\" align=\"center\"><form method='post' name='Cancel'  
action='memberinfo.php?id=".$id."'><input type='submit' value='&nbsp;Cancel&nbsp;'></form></td>";
print "</tr>";
print "</table>";

}

}

?>

我希望有人能帮帮忙!我以前在这里得到了很大的帮助,而且我是 PHP 的初学者。请原谅错别字或错误。

我需要能够显示输入的记录,然后进入,编辑并将更新的记录保存到数据库。提前致谢...

4

0 回答 0