我对此很陌生,这个问题已经困扰我几天了。 我一直在寻找问题的答案,但找不到任何满足我需要的东西。 我试图通过表单将一些数据发送到数据库中,然后将相应的数据表示到显示特定行的表中。 我的问题是,其中一行(操作行)显示为存储为空白数据,即使用户在表单中填写了某些内容...任何内容。
这是我的 Add.php 文件:
<?php
$name="";$first="";$last="";$email="";$pass="";$acti="";
mysql_connect("localhost","admin") or die (mysqli_error());
$db=mysql_select_db("test") or die (mysql_error());
$con=mysqli_connect("localhost","admin","","test")or die (mysqli_error());
if (isset($_POST['save']))
{
$name=mysql_real_escape_string($_POST['name']);
$first=mysql_real_escape_string($_POST['first']);
$last=mysql_real_escape_string($_POST['last']);
$email=mysql_real_escape_string($_POST['email']);
$pass=mysql_real_escape_string($_POST['pass']);
if(isset($_POST['acti']){$actions=mysql_real_escape_string($_POST['acti']);}
echo "A new record has been added!";
$query="INSERT INTO users (Name,FirstName,LastName,Email,Password,Actions)
VALUES ('$name','$first','$last','$email','$pass','$acti')";
$result=mysql_query($query)
or die (mysql_error());
}
mysqli_close($con);
?>
//the form is included in the file
<html>
<head>
<body>
<br/><br/><h2 align=center ><b> Registration page </b></h2><br/><br/><br/>
<form enctype="multipart/form-data" method="POST" action="" align=center >
Name: <input type="text" name="name" value="<?php echo $name;?>"> <br/>
First Name:<input name="first" input type="text" value="<?php echo $first;?>"> <br/>
Last Name:<input name="last" input type="text" value="<?php echo $last;?>"> <br/>
Email: <input type="text" name="email" value="<?php echo $email;?>"> <br/>
Password:<input type="password" input name="pass" value="<?php echo $pass;?>"> <br/>
Actions:<input name="action" input type="text" value="<?php echo $acti;?>"><br/<br/>
<input type="submit" name="save" value="Save">
</form>
<p align=center>Click <a href="List.php"> here</a> for a list!</p>
</body>
</head>
</html>
这是我的 List.php 文件:
<?php
mysql_connect("localhost","admin") or die (mysql_error());
mysql_select_db("test") or die (mysql_error());
/*mysql_query("CREATE TABLE users(id INT NOT NULL AUTO_INCREMENT,PRIMARY KEY (id),Name VARCHAR(255),FirstName VARCHAR(255),LastName VARCHAR(255),Email VARCHAR(255),Password VARCHAR(255),Actions VARCHAR(255))") or die (mysql_error());*/
echo"<table border='2' align=center>";
echo"<tr> <th>Name</th> <th>Email</th> <th>Actions</th> </tr>";
$result=mysql_query("SELECT * FROM users")or die(mysql_error());
while($row=mysql_fetch_array($result))
{
echo "<tr> <td>";
echo $row['Name'];
echo"</td> <td>";
echo $row['Email'];
echo"</td> <td>";
echo $row['Actions'];
echo"</td> </tr>";
}
echo"</table>";
?>
提前谢谢您!
最佳答案
当字段名称为 name='action'
时,您正在调用 $_POST['acti'];
。
您已输入:
if(isset($_POST['acti']){$actions=mysql_real_escape_string($_POST['acti']);}
^ acti ^acti
您的 HTML:
Actions:<input name="action" input type="text" value="<?php echo $acti;?>"><br/<br/>
^action, not 'acti'
在表单中使用 name='acti'
,或在 PHP 中使用 $_POST['action']
。
请查看this link关于如何正确使用mysqli
。
关于php - 表单向数据表中插入空白行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22249315/