0

所以我在 mysql 中有这个表,在数据库 addressbook1 中称为 colegue。它具有以下字段:colegue_id、名字、姓氏、电话和电子邮件。我想要的是获取 firstname 列中的数据并将其显示在使用复选框的表单中。例如:
checkbox jerry
checkbox mary
checkbox cindy
等等。然后,检查的数据应该被发送到一个脚本,该脚本回显所选数据。这是将数据加载到页面上的代码:

 <?php
 //connect to mysql
mysql_connect("localhost", "root", "");
//select database
mysql_select_db("adressbook1");
//select data from table
$query=("SELECT * FROM colegue");
//perform query
$result=mysql_query($query);
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>testing</title>
</head>
<body>
<h3>testing a checkbox form</h3>

<?php
//start the form
echo "<form method=\"post\" action=\"process.php\">";
while($row=mysql_fetch_array($result)) 
{
//assign value from associative array to variable
$data=$row['firstName'];
/*echo data and assign the value in the input the current value of $data while loading     onto array*/
echo "<input type=\"checkbox\" name=\"firstname[]\" value=\"<?php $data;?>\">".$data."    <br>";

}
echo "<br>";
//send data to process.php when clicked
echo "<input type=\"submit\" name=\"sent\" value=\"add\">";
echo "</form>";
?>
</body>
</html>

现在对于处理数据的脚本:

<?php
//get the data from the form in an array
$entry=$_POST['firstname'];
//check if the array is empty or not
if (empty($entry))
{
echo("execution failed");
}
//if not empty, continue
else
{
//get the array size
$N=count($entry);
//display the size of the array
echo "$N"."<br>";
//this loop should display all the data in the array
for($i=0; $i<$N; $i++)
{

echo "$entry[$i]"."<br>";

 }
//end script by showing successful result
echo ("request successful!");

}
?>

当我检查要发送的名称并将它们提交给脚本时,代码运行良好。该脚本确实向我显示了数组的大小(例如,如果我单击四个名称,它会显示数字四)但它没有显示其他任何内容,它没有显示我单击的实际名称页。谁能帮我解决这个问题?我在想也许我没有在数组中正确分配值firstname[]

请注意,我必须使用此方法(复选框)来提交从数据库中获取的数据,以便将其显示在脚本页面中。我对编程很陌生,所以我请求帮助,谢谢。

4

1 回答 1

0

您需要将 $data 与字符串连接,只需更改

value=\"<?php $data;?>\"

value=\"".$data."\"
于 2012-09-27T18:28:04.757 回答