dongpu9481 2016-04-23 12:38
浏览 430
已采纳

如何使用php在提交按钮上保存数据从文本框到数据库

i need to transfer the data from textboxes designed in HTML to Mysql database using php. I tried a lot but didnt work...so finally if anyone has a working code already for it then please let me know. i just have two textboxes named "username" and "password" and a submit button. I have already created a db [dbname: userdemo, tablename: users] but no results. Please use OOPs concept not procedural. Any kind of help will be appreciated!!!

  • 写回答

1条回答 默认 最新

  • dongpo4197 2016-04-23 12:50
    关注

    The html code below is a snippet of how your form should look, though you have mentioned you already have this part done:

    page1.html

    <form method="POST" action="page2.php">
      <input type="text" name="usernameForm">
      <input type="password" name="passwordForm">
      <input type="submit" value="Submit">
    </form>
    

    The php code below then obtains the variables from page1.html after a user Submits their information from the form, and then inserts a line into a table in the database. When using this code, don't forget to replace all the table names, columns, etc with the ones you have on your form, as well as the 4 "********" variables that give your code access to your database:

    page2.php

    //uses MySQL (PDO)
    <?php
    $usernameForm = $_POST['usernameForm'];
    $passwordForm = $_POST['passwordForm'];
    
    $servername = "********";
    $username = "********";
    $password = "********";
    $myDB = "********";
    
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$myDB", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully"; 
    
        $sql = "INSERT INTO myTable (username, password) VALUES (:usernameForm, :passwordForm)";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':usernameForm', $usernameForm);
        $stmt->bindParam(':passwordForm', $passwordForm);
        $stmt->execute();
    
        }
    catch(PDOException $e)
        {
        echo "Connection failed: " . $e->getMessage();
        }
    
    $conn = NULL;
    ?>
    

    Edited: After having read your comment on wanting to have all this code on one page, a simple way of doing it is the following:

    Replace this line:

    <form method="POST" action="page2.php">
    

    with this line:

    <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    

    What is happening here is the following: PHP code is always run on the server. When you have your website fully loaded, your connection with the server terminated. Whatever information you are sending to the server via the <form>...</form> tags need to establish a connection with the server, which is why in general people user two pages (one for the input, and another one that receives the data), HOWEVER there is a simple and common way of keeping all this code in one page: have the <form>...</form> send to itself!

    To do this you have 2 options:

    1. if your page was called page1.php, then you could just replace action="page2.php" with action="page1.php". However this in general is not good programming because if for some reason the name of your page changes, then your code breaks, which leads us to the second (and better) option.
    2. replacing action="page2.php" with action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>". What this does is almost the same as option 1, HOWEVER the php code inside action is asking the $_SERVER superglobal for what the name of the page is, hence preventing your page from "breaking" in cases like when you rename your file.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号