表单将空行插入数据表

时间:2014-03-07 11:49:47

标签: php mysql forms

我对此很陌生,这个问题已经暂时搁置了几天。 我已经搜索了我的问题的答案,但找不到任何满足我需要的东西。 我试图通过表单将一些数据发送到数据库,然后将相应的数据表示到显示特定行的表中。 我的问题是,其中一行(Actions行)显示为空白数据,尽管用户用某些东西填充表单......

这是我的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>";
?>

提前谢谢!

2 个答案:

答案 0 :(得分:0)

当字段名称为$_POST['acti'];时,您正在调用name='action'

你已经把:

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

答案 1 :(得分:0)

你在mysql和mysqli之间混合

将所有mysql代码切换到mysqli示例:

 $name=mysql_real_escape_string($_POST['name']);

应该是

$name=mysqli_real_escape_string($_POST['name']);

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

应该是

$result=mysqli_query($query)
        or die (mysqli_error()); 

 if(isset($_POST['acti'])

应该是

if(isset($_POST['action'])
顺便说一句:你用两种方法连接? mysql和mysqli ??

删除它:

 mysql_connect("localhost","admin") or die (mysqli_error());//this connection is mysql and is also wrong without password.
 $db=mysql_select_db("test") or die (mysql_error());

并仅通过mysqli连接。