douning5041 2010-08-31 17:47
浏览 161
已采纳

使用INSERT语句将用户数据插入数据库

From a user form: I am trying to insert the following data: 1) First Name 2) Last Name 3) Major 4) Graduation Year

I am able to connect to the database, and select the database I need--but I am unable to insert the data from the form. I am able to create records, but the data is not being saved to the database. Basically, right now I'm creating blank forms.

The variable $uInput holds the user data. I tried passing $uInput into the function doAction(), but I believe that is where the problem is. I'm trying to figure out how to pass the user data into the function doAction().

 <?php  

    //Call function mainline
    mainline();

    // Declare the function mainline
    function mainline() {

        $uInput = getUserInput();

        $connectDb = openConnect(); // Open Database Connection
        selectDb($connectDb); // Select Database
        doAction($uInput);
        //closeConnect();
        //display();

    }

    //Declare function getUserInput ------------------------------------------------------------------------------------
    function getUserInput() {

        echo "In the function getUserInput()" . "<br/>";

        // Variables of User Input
        $idnum = $_POST["idnum"];              // id (NOTE: auto increments in database)
        $fname = $_POST["fname"];             // first name
        $lname = $_POST["lname"];            // last name
        $major = $_POST["major"];           // major
        $year = $_POST["year"];          // year
        $action = $_POST["action"];       // action (select, insert, update, delete)

        $userInput = array($idnum, $fname, $lname, $major, $year, $action);
            //echo "info from getUserInput: " . $action;    
        return $userInput;
    }


    function doAction($pUserInput) {
        // if user selects INSERT from dropdown menu, then call function insert 
       //and pass $uInput
        if ($pUserInput[5] == "ins") {
            insert($uInput);    
        }

    }

    // Create a database connection --------------------------------------------------------
    function openConnect() {
        $connection = mysql_connect("localhost", "root_user", "password");
            echo "Opened Connection!" . "<br/>";

        if(!$connection) {
            die("Database connection failed: " . mysql_error());
        }

        return $connection;
    }

    // Select a database to ---------------------------------------------------------------- 
    function selectDb($pConnectDb) {
        $dbSelect = mysql_select_db("School", $pConnectDb);
        if(!$dbSelect) {
            die("Database selection failed: " . mysql_error());
        } else {
        echo "You are in the School database! <br/>";   
        }

    }


    // function insert ---------------------------------------------------------------------
    function insert($pUInput) {

        $sql="INSERT INTO tblStudents (first_name, last_name, major, year)
              VALUES
             ('$pUInput[1]','$pUInput[2]','$pUInput[3]', '$pUInput[4]')";

            if (!mysql_query($sql))
              {
              die('Error: ' . mysql_error());
              }
            echo "1 record added";
    }

    ?> 
  • 写回答

2条回答 默认 最新

  • donglisi8644 2010-08-31 17:53
    关注

    Your doAction() function is buggy. You are taking the parameter into the function as $pUserInput but sending to the insert() function as $uInput.

    You should do it like this:

    function doAction($pUserInput)
    {
         // if user selects INSERT from dropdown menu, then call function insert 
         //and pass $uInput
         if ($pUserInput[5] == "ins")
         {
                insert($pUserInput); // <-- FIXED: Not using correct parameter.
         }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分