首页 > 解决方案 > 为图书馆学校制作一个 fwrite 联系人 PHP

问题描述

我试图用 PHP 制作联系人簿,这个网站是我学校的,我是初中老师,但我不能制作 PHP 代码/对不起

索引.php

    <?php
    if(!empty($errorMessage)) 
    {
        echo("<p>There was an error with your form:</p>\n");
        echo("<ul>" . $errorMessage . "</ul>\n");
    } 
?>
<form action="file.php" method="post">
    <p>
        Name<br>
        <input type="text" name="aa" maxlength="50"  />
    </p>
    <p>
        Contact <br>
        <input type="text" name="ab" maxlength="50"  />
    </p>
    <p>
        Books<br>
        <input type="text" name="ac" maxlength="50"  />
    </p>            
    <input type="submit" name="formSubmit" value="Submit" />
</form>

这里.html

<head>
	<title>Thank you!</title>
</head>

<body>
Data Telah di Input.<br>
CEk Disini <a href="data.html" >Data</a>


</body>

文件.php

<?php
if($_POST['formSubmit'] == "Submit")
{
	$errorMessage = "";
	
	if(empty($_POST['aa']))
	{
		$errorMessage .= "<li>You forgot to enter a name</li>";
	}
	if(empty($_POST['ab']))
	{
		$errorMessage .= "<li>You forgot to enter a contact</li>";
	}
	if(empty($_POST['ac']))
	{
		$errorMessage .= "<li>You forgot to enter a book</li>";
	}
	
	$varaa = $_POST['aa'];
	$varab = $_POST['ab'];
	$varac = $_POST['ac'];

	
	if(empty($errorMessage)) 
	{
		$fs = fopen("data.html","ab");
		echo "<table > <tr><th>No</th> <th>Name</th> <th>Contact</th> <th>Books</th></tr>";
		fwrite($fs,"
		<tr>
		     <td> ".$varaa. "</td>
			<td>" .$varab. "</td>
			<td>" .$varac. "</td>
		</tr>");
		echo "</table >";
		fclose($fs);
		
		header("Location: here.html");
		exit;
	}
}
?>

那么,为什么我得到像这样的输出

		<tr>
		     <td> Andreas</td>
			<td>64898722</td>
			<td>Test Book1</td>
		</tr>
		<tr>
		     <td> Yuyun</td>
			<td>64898444</td>
			<td>Book4</td>
		</tr>

我试图做出这样的输出,但为什么我得到 alawys 失败我不知道如何制作其他 php,抱歉。

我需要输出表,并在第一个表中添加数字。

<table>
	<tr><th>No</th> <th>Name</th> <th>Contact</th> <th>Books</th></tr>
		<tr>
			<td>1</td>
		     <td> Andreas</td>
			<td>64898722</td>
			<td>Test Book1</td>
		</tr>
		<tr>
			<td>2</td>
		     <td> Yuyun</td>
			<td>64898444</td>
			<td>Book4</td>
		</tr>
</table>

标签: phphtmlfwrite

解决方案


删除echotable使用fwrite

if(empty($errorMessage)) 
{
    $fs = fopen("data.html","w");
    fwrite($fs, "<table > <tr><th>No</th> <th>Name</th> <th>Contact</th> <th>Books</th></tr>");
    fwrite($fs,"
    <tr>
         <td> ".$varaa. "</td>
        <td>" .$varab. "</td>
        <td>" .$varac. "</td>
    </tr>");
    fwrite($fs, "</table >");
    fclose($fs);

    header("Location: here.html");
    exit;
}

推荐阅读