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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化