如何将表单中的文本添加到sql表中?

Posted

技术标签:

【中文标题】如何将表单中的文本添加到sql表中?【英文标题】:How to add text from form into sql table? 【发布时间】:2014-04-22 20:42:06 【问题描述】:

在我的 flex 应用程序中,我有一个从 SQL 表中检索数据并将其显示在文本输入中的表单:

<s:Form id="form" includeIn="ShoppingList" x="223" y="353"
        creationComplete="form_creationCompleteHandler(event)" defaultButton="button">
    <s:FormItem label="Name">
        <s:TextInput id="textInput" text="getAllShoppinglistResult.lastResult[0].name"/>
        <s:TextInput id="textInput1" text="getAllShoppinglistResult.lastResult[1].name"/>
        <s:TextInput id="textInput2" text="getAllShoppinglistResult.lastResult[2].name"/>
        <s:TextInput id="textInput3" text="getAllShoppinglistResult.lastResult[3].name"/>
    </s:FormItem>
    <s:Button id="button" label="Submit" click="button_clickHandler(event)"/>
</s:Form>

在这种情况下,SQL 表中只有 2 个项目,其他 2 个文本输入字段是免费的。

我希望能够在 textinput 中输入文本并将其保存到服务器。

 protected function button_clickHandler(event:MouseEvent):void
        
            items.name = textInput.text;
            createShoppinglistResult.token = shoppinglistService1.createShoppinglist(name);
        

        protected function createShoppinglistResult_resultHandler(event:ResultEvent):void
        


        

我不确定 createShoppinglist..* 函数中的内容。

我知道 php 服务是正确的,因为目前它确实将它保存到服务器但它只是保存 NULL,我希望它保存文本输入。我知道如果它是一个数据网格,我可以将 AddItem() 用于数组集合,但我不知道我可以将什么用于表单?

【问题讨论】:

【参考方案1】:

也许这会有所帮助!关于aktell

不幸的是,看起来有点像一团糟!这是一个链接,您可以更好地看到它 - 就在底部!

http://board.flashkit.com/board/showthread.php?828493-php-gt-gt-mysql-not-returning-data-as-XML


<?php

//init.php

// 脚本: // 连接到 MySQL 数据库服务器和数据库。

/* --------------------------------------------- -------------------------------------------------- ------------------- */

    // MySQL DataBase Server Variables.
    $mysql_host = "localhost";
        $mysql_user = "root";
            $mysql_pass = "";

    // DataBase Variable.               
    $db = 'webflash_createcartdb';

    // Connect to MySQL DataBase Server.    
    $con = @ mysql_connect("$mysql_host", "$mysql_user", "$mysql_pass") 
        or exit("Could not connect to MySQL DataBase Server! \n Select DB Error: " . @ mysql_error());

        $con = @ mysql_select_db($db) 
            or exit( 'Can\'t select the Database is unavailable.' . @ mysql_error());

    return $con;

/* --------------------------------------------- -------------------------------------------------- ------------------- */

?>

读写:

// custInfoDetails.php

/* --------------------------------------------- -------------------------------------------------- ------------------- */

    header('Content-Type: text/xml');

/* --------------------------------------------- -------------------------------------------------- ------------------- */

    include("../Conn/init.php");

/* --------------------------------------------- -------------------------------------------------- ------------------- / // 主表。 / --------------------------------------------------------- -------------------------------------------------- ------------------- */

    if (isset($_POST['lastName']))  
        $title = $_POST['title'];
        $firstName = $_POST['firstName'];
        $middleName = $_POST['middleName'];
        $lastName = $_POST['lastName'];
        $fullName = $_POST['fullName'];
        $no = $_POST['no'];
        $street1 = $_POST['street1'];
        $street2 = $_POST['street2'];
        $zip = $_POST['zip'];
        $suburb = $_POST['suburb'];
        $city = $_POST['city'];
        $state = $_POST['state'];
        $country = $_POST['country'];
        $email = $_POST['email'];
        $userName = $_POST['userName'];
        $password = $_POST['password'];

    $userNameCopy = mysql_real_escape_string($_POST["userNameCopy"]);

     $sql = 'INSERT INTO ' . $userNameCopy . ' 
        (title, firstName, middleName, lastName, fullName, no, street1, street2, zip, suburb, city, 
        state, country, email, userName, password, now) 
            VALUES 
        ("' . $title . '", "' . $firstName . '", "' . $middleName . '", "' . $lastName . '", "' . $fullName . '", 
        "' . $no . '", "' . $street1 . '", "' . $street2 . '", "' . $zip . '", "' . $suburb . '", 
        "' . $city . '", "' . $state . '", "' . $country . '", "' . $email . '", 
        "' . $userName . '", "' . $password . '", NOW() )';

    $result = @ mysql_query($sql);
        if (!$result)  exit('Error performing the MAIN INSERT query.' . @ mysql_error());
        
    

/* --------------------------------------------- -------------------------------------------------- ------------------- */

    $sql = "SELECT custcartinfoID, title, firstName, middleName, lastName, fullName, no, street1, street2, 
            zip, suburb, city, state, country, email, userName, password, now FROM ' . $userNameCopy . ' ";

    $result = @ mysql_query($sql);

    if (!$result) 
        $message  = 'Invalid query: ' . @ mysql_errno() . " : " . @ mysql_error() . "\n";
        $message .= 'Whole query: ' . $sql;
        exit($message);
    

/* --------------------------------------------- -------------------------------------------------- ------------------- / // XML 读出。 / --------------------------------------------------------- -------------------------------------------------- ------------------- */

        $xml = new DomDocument('1.0', 'UTF-8');

        $root = $xml->createElement('allMessages');

        $root = $xml->appendChild($root);           

        $result = @ mysql_query($sql);

        if ($result) 

            if ( @ mysql_num_rows($result) > 0) 

                while ($row = @ mysql_fetch_array($result)) 

                    $custcartinfoID = $row['custcartinfoID'];
                    $title = $row['title'];
                    $firstName = $row['firstName'];
                    $middleName = $row['middleName'];
                    $lastName = $row['lastName'];
                    $fullName = $row['fullName'];
                    $no = $row['no'];
                    $street1 = $row['street1'];
                    $street2 = $row['street2'];
                    $zip = $row['zip'];
                    $suburb = $row['suburb'];
                    $city = $row['city'];
                    $state = $row['state'];
                    $country = $row['country'];
                    $email = $row['email'];
                    $userName = $row['userName'];
                    $password = $row['password'];
                    $now = $row['now'];

                    // Message Element OPEN Node.               
                    $itemElement = $xml->createElement('message');
                    $itemElement = $root->appendChild($itemElement);

                    // First Field.         
                    $idElement = $xml->createElement('custcartinfoID', $custcartinfoID);
                    $idElement = $itemElement->appendChild($idElement);

                    $titleElement = $xml->createElement('title', $title);
                    $titleElement = $itemElement->appendChild($titleElement);

                    $firstNameElement = $xml->createElement('firstName', $firstName);
                    $firstNameElement = $itemElement->appendChild($firstNameElement);

                    $middleNameElement = $xml->createElement('middleName', $middleName);
                    $middleNameElement = $itemElement->appendChild($middleNameElement);

                    $lastNameElement = $xml->createElement('lastName', $lastName);
                    $lastNameElement = $itemElement->appendChild($lastNameElement);

                    $fullNameElement = $xml->createElement('fullName', $fullName);
                    $fullNameElement = $itemElement->appendChild($fullNameElement);

                    $noElement = $xml->createElement('no', $no);
                    $noElement = $itemElement->appendChild($noElement);

                    $street1Element = $xml->createElement('street1', $street1);
                    $street1Element = $itemElement->appendChild($street1Element);

                    $street2Element = $xml->createElement('street2', $street2);
                    $street2Element = $itemElement->appendChild($street2Element);

                    $zipElement = $xml->createElement('zip', $zip);
                    $zipElement = $itemElement->appendChild($zipElement);

                    $suburbElement = $xml->createElement('suburb', $suburb);
                    $suburbElement = $itemElement->appendChild($suburbElement);

                    $cityElement = $xml->createElement('city', $city);
                    $cityElement = $itemElement->appendChild($cityElement);

                    $stateElement = $xml->createElement('state', $state);
                    $stateElement = $itemElement->appendChild($stateElement);

                    $countryElement = $xml->createElement('country', $country);
                    $countryElement = $itemElement->appendChild($countryElement);

                    $emailElement = $xml->createElement('email', $email);
                    $emailElement = $itemElement->appendChild($emailElement);   

                    $userNameElement = $xml->createElement('userName', $userName);
                    $userNameElement = $itemElement->appendChild($userNameElement);

                    $passwordElement = $xml->createElement('password', $password);
                    $passwordElement = $itemElement->appendChild($passwordElement);

                    // Last Field.  
                    $nowElement = $xml->createElement('now', $now);
                    $nowElement = $itemElement->appendChild($nowElement);               
                
            
            // Message Element CLOSING Node.
            else 
                $messageElement = $xml->createElement('message','There are no posts.');
                $messageElement = $root->appendChild($messageElement);
            
        
        else 
            $messageElement = $xml->createElement('message', @mysql_error());
            $messageElement = $root->appendChild($messageElement);
        


    // Return the XML Document.
    echo $xml->saveXML();

/* --------------------------------------------- -------------------------------------------------- ------------------- */

    // CLOSE DATABSE.

    // Close DataBase Server Connection!
    @ mysql_close($con);

/* --------------------------------------------- -------------------------------------------------- ------------------- */

?>

只读:

// 选择.php

/* --------------------------------------------- -------------------------------------------------- ------------------- */

    // First, this script uses the header() function to tell the web server that the return is going to be XML.
    header('Content-Type: text/xml');

/* --------------------------------------------- -------------------------------------------------- ------------------- */

    // Connecting with MySQL DataBase Server & a DataBase.
    include("init.php");

/* --------------------------------------------- -------------------------------------------------- ------------------- / // 主表。 / --------------------------------------------------------- -------------------------------------------------- ------------------- */

    // SELECT more than one Table ! 
    /* $query="SELECT movie.movie_name, movietype.movietype_label FROM movie, movietype     
                WHERE movie.movie_type = movietype.movietype_id
                AND movie.movie_year>1990
                ORDER BY movie_type"; */

    $sql = 'SELECT customerID, title, firstname, middlename, lastname, no, street1, street2, zip, suburb, city, 
        state, country, email, emailpriv, emailbus, url1, url2, note, now FROM customerdetails';

    $result = @ mysql_query($sql);

    if (!$result) 
        $message  = 'Invalid query: ' . @ mysql_errno() . " : " . @ mysql_error() . "\n";
        $message .= 'Whole query: ' . $sql;
        exit($message);
    

/* --------------------------------------------- -------------------------------------------------- ------------------- / // XML 读出。 / --------------------------------------------------------- -------------------------------------------------- ------------------- */

        $xml = new DomDocument('1.0', 'UTF-8');

        $root = $xml->createElement('customerDetails');

        $root = $xml->appendChild($root);

        $result = @ mysql_query($sql);

        if ($result) 

            if ( @ mysql_num_rows($result) > 0) 

                while ($row = @ mysql_fetch_array($result)) 

                    $customerID = $row['customerID'];
                    $title = $row['title'];
                    $firstname = $row['firstname'];
                    $middlename = $row['middlename'];
                    $lastname = $row['lastname'];
                    $no = $row['no'];
                    $street1 = $row['street1'];
                    $street2 = $row['street2'];
                    $zip = $row['zip'];
                    $suburb = $row['suburb'];
                    $city = $row['city'];
                    $state = $row['state'];
                    $country = $row['country'];
                    $email = $row['email'];
                    $emailpriv = $row['emailpriv'];
                    $emailbus = $row['emailbus'];
                    $url1 = $row['url1'];
                    $url2 = $row['url2'];
                    $note = $row['note'];
                    $now = $row['now'];

                    // Message Element OPEN Node.               
                    $itemElement = $xml->createElement('information');
                    $itemElement = $root->appendChild($itemElement);

                    // First Field.         
                    $idElement = $xml->createElement('customerID', $customerID);
                    $idElement = $itemElement->appendChild($idElement);

                    $titleElement = $xml->createElement('title', $title);
                    $titleElement = $itemElement->appendChild($titleElement);

                    $firstnameElement = $xml->createElement('firstname', $firstname);
                    $firstnameElement = $itemElement->appendChild($firstnameElement);

                    $middlenameElement = $xml->createElement('middlename', $middlename);
                    $middlenameElement = $itemElement->appendChild($middlenameElement);

                    $lastnameElement = $xml->createElement('lastname', $lastname);
                    $lastnameElement = $itemElement->appendChild($lastnameElement);

                    $noElement = $xml->createElement('no', $no);
                    $noElement = $itemElement->appendChild($noElement);

                    $street1Element = $xml->createElement('street1', $street1);
                    $street1Element = $itemElement->appendChild($street1Element);

                    $street2Element = $xml->createElement('street2', $street2);
                    $street2Element = $itemElement->appendChild($street2Element);

                    $zipElement = $xml->createElement('zip', $zip);
                    $zipElement = $itemElement->appendChild($zipElement);

                    $suburbElement = $xml->createElement('suburb', $suburb);
                    $suburbElement = $itemElement->appendChild($suburbElement);

                    $cityElement = $xml->createElement('city', $city);
                    $cityElement = $itemElement->appendChild($cityElement);

                    $stateElement = $xml->createElement('state', $state);
                    $stateElement = $itemElement->appendChild($stateElement);

                    $countryElement = $xml->createElement('country', $country);
                    $countryElement = $itemElement->appendChild($countryElement);

                    $emailElement = $xml->createElement('email', $email);
                    $emailElement = $itemElement->appendChild($emailElement);

                    $emailprivElement = $xml->createElement('emailpriv', $emailpriv);
                    $emailprivElement = $itemElement->appendChild($emailprivElement);

                    $emailbusElement = $xml->createElement('emailbus', $emailbus);
                    $emailbusElement = $itemElement->appendChild($emailbusElement);

                    $url1Element = $xml->createElement('url1', $url1);
                    $url1Element = $itemElement->appendChild($url1Element);

                    $url2Element = $xml->createElement('url2', $url2);
                    $url2Element = $itemElement->appendChild($url2Element);

                    $noteElement = $xml->createElement('note', $note);
                    $noteElement = $itemElement->appendChild($noteElement); 

                    // Last Field.  
                    $nowElement = $xml->createElement('now', $now);
                    $nowElement = $itemElement->appendChild($nowElement);               
                
            
            // Message Element CLOSING Node.
            else 
                $messageElement = $xml->createElement('information','There are no posts.');
                $messageElement = $root>appendChild($messageElement);
            
        
        else 
            $messageElement = $xml->createElement('information', @mysql_error());
            $messageElement = $root>appendChild($messageElement);
        

    echo $xml->saveXML();

?>

【讨论】:

以上是关于如何将表单中的文本添加到sql表中?的主要内容,如果未能解决你的问题,请参考以下文章

如何将一个表中的随机值添加到另一个 Oracle SQL

如何将“其他”文本输入添加到 HTML 表单中的一组单选按钮?

如何将 HTML 表单中的数据保存到 WordPress 中的数据库表中?

DB2 SQL:如何将“WHERE EXISTS”表中的列添加到选择语句

如何使用 C# 和 SQL Server 连接到数据库

如何将 Update 语句添加到我将要插入到带有 MS SQL 查询的表中的选定行中?