douyi6960 2016-06-24 23:37
浏览 34
已采纳

使用php无法将值添加到mysql数据库? [关闭]

This is the index.php file:

<?php
    //Attempt MySQL server connection. Assuming you are running MySQL
    //server with default setting (user 'root' with no password)
    $link = mysqli_connect("localhost", "root", "SecretPassword", "ClientDataDB");

    // Check connection
    if($link === false){
        die("Connection failed: " . mysqli_connect_error());
    }

    // Attempt select query execution
    $sql = "SELECT * FROM ClientLogin";
    if($result = mysqli_query($link, $sql)){
        if(mysqli_num_rows($result) > 0){
            echo "<table>";
                echo "<tr>";
                    echo "<th>ID</th>";
                    echo "<th>First Name</th>";
                    echo "<th>Last Name</th>";
                    echo "<th>Email</th>";
                    echo "<th>Username</th>";
                    echo "<th>Password</th>";
                echo "</tr>";
            while($row = mysqli_fetch_array($result)){
                echo "<tr>";
                    echo "<td>" . $row['ID'] . "</td>";
                    echo "<td>" . $row['FirstName'] . "</td>";
                    echo "<td>" . $row['LastName'] . "</td>";
                    echo "<td>" . $row['Email'] . "</td>";
                    echo "<td>" . $row['Username'] . "</td>";
                    echo "<td>" . $row['Password'] . "</td>";
                echo "</tr>";
            }
            echo "</table>";
            // Close result set
            mysqli_free_result($result);
        } else{
            echo "No records found!";
        }
    } else{
        echo "Error could not execute query: " . mysqli_error($link);
    }

    // Close connection
    mysqli_close($link);
    ?>

<head>
<meta charset="UTF-8">
<title>Create New User</title>
</head>
<body>
<form action="insert.php" method="post">
    <p>
        <label>First Name:</label>
        <input type="text" name="FirstName"/>
    </p>
    <p>
        <label>Last Name:</label>
        <input type="text" name="LastName"/>
    </p>
    <p>
        <label>Email Address:</label>
        <input type="text" name="Email"/>
    </p>
    <p>
        <label>Username:</label>
        <input type="text" name="Username"/>
</p>
    <p>
        <label>Password:</label>
        <input type="text" name="Password"/>
    </p>
    <input type="submit" value="Submit">
</form>
</body>
</html>

And this is the insert.php file:

<?php
//Attempt MySQL server connection. Assuming you are running MySQL
//server with default setting (user 'root' with no password)
$link = mysqli_connect("localhost", "root", "MiggKuter5049", "ClientDataDB");

//Check connection
if($link === false)
{
    die("Connection failed: " . mysqli_connect_error());
}

//Get user inputs
$firstname = $_POST['FirstName']);
$lastname = $_POST['LastName']);
$email = $_POST['Email']);
$username = $_POST['Username']);
$password = $_POST['Password']);

// attempt insert query execution
$sql = "INSERT INTO ClientLogin (FirstName, LastName, Email, Username, Password) VALUES ('$firstname', '$lastname', '$email', '$username', '$password')";
if(mysqli_query($link, $sql)){
    echo "User added successfully!";
} else{
    echo "Error, could not execute query: " . mysqli_error($link);
}

// close connection
mysqli_close($link);
?>

I cannot for the life of me find my mistake. When you click the submit button it creates another entry in the table but all blank, none of the inputted values are used. Please help :)

  • 写回答

1条回答 默认 最新

  • douzhang7603 2016-06-24 23:50
    关注

    It looks like you have a unnecessary paren around around your POST statement when retrieving the user input in your insert.php file.

    Should look like this

    $firstname = $_POST['FirstName'];
    $lastname = $_POST['LastName'];
    $email = $_POST['Email'];
    $username = $_POST['Username'];
    $password = $_POST['Password'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题