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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址