显示/编辑复选框值?

时间:2013-08-12 21:09:37

标签: php mysql

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

<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的初学者。请原谅错别字或错误。

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

0 个答案:

没有答案