dongnue4923 2015-08-08 20:45
浏览 7
已采纳

表单信息未更新到我的数据库

I have been stuck on this for 3 days now and any help would be very much appreciated. Please note that i have the php and html in one page for now until i get it working then once it has been fixed i will place the php connection script into its usual folder. when I get onto the sign up page i can see that i am connected to my and i can fill out my form and submit it without any errors, however the database will not be updated and when i refresh the database no new users are found. Below is my code so far if anyone could show me what i am doing wrong or where i could improve to fix this error i would be trully thankfull. Thank you in advance!

<?php
    $host="localhost";
    $dbuser="root";
    $pass="********";
    $dbname="amsadler";
    $con = mysqli_connect ($host, $dbuser, $pass, $dbname);
    if (mysqli_connect_errno ())
    {
        die ("Connection Failed!" . mysqli_connect_error());
    }
    else
    {
    echo "Connected to database {$dbname} ";
    }

    if(isset($_POST['submit']))
    {
        $fname = mysqli_real_escape_string($con, $_POST ['fname']);
        $lname= mysqli_real_escape_string($con, $_POST ['lname']);
        $email= mysqli_real_escape_string($con, $_POST ['email']);
        $pswrd= mysqli_real_escape_string($con, $_POST ['pswrd']);

        $sql = $con->query("INSERT INTO users ( fname, lname, email, pswrd,)
                    VALUES ( ' {$fname} ' , ' {$lname} ' , ' {$email} ' , ' {$pswrd} ' )");

    }
?>


<!DOCTYPE html>
<html lang="en-US">
<head>
    <title>Signup</title>
    <meta charset="utf-8">
    <meta name="description" content="description of webpage">
    <meta name="keywords" content="keywords go here">
    <meta name="author" content="amsadler">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/signup.css">
    <link rel="index" href="index.php">
    <link rel="icon" href="img/favicon.png" sizes="16x16" type="image/png">
</head>
<body>

    <div class="header">
        <div id="logo">
                <a href="index.php"><img src="img/logo.png" alt="logo" title="blah blah"/></a>
        </div>
    </div>

     <div id="signupform">
        <form method="post"><br>
            <input type="text" id="fname" name="fname" placeholder="First name">
            <input type="text" id="lname" name="lname" placeholder="Last name"><br><br>
            <input type="text" id="email" name="email" placeholder="Email address">
            <input type="password" id="pswrd" name="pswrd" placeholder="Password"><br><br>
            <input id="button" type="submit" value="Submit">
        </form>
    </div>

<?php
    include ("inc/footer.php");
?>

</body>
</html>
  • 写回答

1条回答 默认 最新

  • douqiao2471 2015-08-08 21:00
    关注

    There are a few problems here.

    One of them being with your conditional statement:

    if(isset($_POST['submit'])){...}
    

    Nothing in there will fire up because of a missing name attribute "submit".

    Name your submit button:

    <input name="submit" id="button" type="submit" value="Submit">
           ^^^^^^^^^^^^^
    

    Then a trailing comma in your query:

    ( fname, lname, email, pswrd,)
                                ↑
    

    remove it

    ( fname, lname, email, pswrd)
    

    Add or die(mysqli_error($con)) to mysqli_query()

    Using error checking on the query will have shown another syntax error soon as you would name the submit button:

    No errors you say? You're most likely not checking for them or your system isn't setup for it and to display them.

    Add error reporting to the top of your file(s) which will help find errors.

    <?php 
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    // rest of your code
    

    Sidenote: Displaying errors should only be done in staging, and never production.

    • It would have signaled an undefined index submit notice.

    In order to make absolutely sure that it was a successful query, use affected_rows().


    I noticed you may be storing passwords in plain text. If this is the case, it is highly discouraged.

    I recommend you use CRYPT_BLOWFISH or PHP 5.5's password_hash() function. For PHP < 5.5 use the password_hash() compatibility pack.

    Make sure that if and when you do use a safe hashing method, that your password column is long enough to hold the hash.

    A safe bet is VARCHAR(255) which PHP.net recommends on using. Details are listed on their website.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解