CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2006
    Posts
    50

    [RESOLVED] insert data from form to mySQL database

    I have run createTable.php and everything is created.
    Now I want to run insertData.php. I want the user to enter stuff in the form & everything must be fill in before they click the submit button. It will call submitData() & all the information was enter will insert into the database.

    How can I do that.

    config.php
    PHP Code:
    <?php
    $errCon 
    "<br> Contact your webmaster. <br>";
    $server "localhost";
    $user "root";
    $password "pass";
    $tableDaily="issue";
    $dbnameDaily="daily_issue";

    ?>

     <?php
        mysql_connect
    ($server$user$password)
        or die(
    $errCon mysql_error());

        echo 
    "SUCCESS";
    ?>
    createTable.php
    PHP Code:
    <?php include("config.php"); ?>

    <?php


    mysql_query
    ("CREATE DATABASE $dbnameDaily") or die($errCon mysql_error());
    echo 
    "success in database creation. $dbnameDaily";

    ?>


     <?php

        mysql_select_db
    ($dbnameDaily) or die($errCon mysql_error());
    // Create a MySQL table in the selected database
    mysql_query("CREATE TABLE $tableDaily(
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(id),
    owner VARCHAR(30),
    problem VARCHAR(999),
    status VARCHAR(1),
    dateCreate DATETIME,   //YYYY-MM-DD HH:MM:SS
    dateModify TIMESTAMP)"
    )  //YYYY-MM-DD HH:MM:SS
    or die($errCon mysql_error());

    echo 
    "Table Created!";
    ?>
    insertData.php
    PHP Code:
    <?php include("config.php"); ?>

    <?php

    function submitData() {

    mysql_select_db($dbnameDaily) or die($errCon mysql_error());

    $DATECREATE=$DATEMODIFY=date("Y-m-d H:i:s");


    // Insert a row of information into the table

    mysql_query("INSERT INTO $tableDaily
    (owner, problem, status, dateCreate, dateModidy)

    VALUES('
    $OWNER', '$PROBLEM','$STATUS', '$DATECREATE', '$DATEMODIFY' ) ")

    or die(
    $errCon mysql_error());

    //owner VARCHAR(30),
    //problem VARCHAR(999),
    //status VARCHAR(1),
    //dateCreate DATETIME,   //YYYY-MM-DD HH:MM:SS
    //dateModify TIMESTAMP)")  //YYYY-MM-DD HH:MM:SS

    echo "Data Inserted!";

    }
    ?>

    <?php

    echo('<form name="create_form" method="post">
    <input type="hidden" name="require" value="OWNER,PROBLEM">
    <table>
    <tr>
        <td align="right">Name:</td>
        <td><input name="OWNER" size="25"></td>
    </tr>

    <tr>
        <td align="right">Status:</td>
        <td><select name="STATUS">
        <option value="W">Work
        <option value="I">Idle
        </select>
        </td>
    </tr>

    <tr>
        <td align="right">Problem:</td>
        <td><textarea name="PROBLEM" rows="10" cols="40"></textarea>
        </td>
    </tr>

    <tr>
        <td colspan="2" align="center"><input type="submit" value="Submit" name="create_form">
        <input type="reset" value="Reset" name="reset"></td>
    </tr>

    </table>
    </form>'
    );
    ?>

  2. #2
    Join Date
    Nov 2004
    Location
    Slough, UK
    Posts
    184

    Re: insert data from form to mySQL database

    Your code makes me want to cry. PHP is a HTML embedded scripting language, meaning that you need not and should not use echo to output huge chunks of HMTL. Secondly, your submit data function is never caleld, therefore the data will never be submitted.

    I have corrected your code and showed how you may want to call the function:
    PHP Code:
    <?php include("config.php");

    if (isset(
    $_POST['create_form'])) { // this means the form was submitted
        
    submitData(); // niow Call the function
    }

    function 
    submitData() {
        
    /* the form variables come from  the $_POST array, they must be sanitized
           before use in an SQL string */
        
    $OWNER addslashes($_POST['OWNER']);
        
    $STATUS addslashes($_POST['STATUS']);
        
    $PROBLEM addslashes($_POST['problem']);

        
    /* don't forget to indent code */
        
    mysql_select_db($dbnameDaily) or die($errCon mysql_error());

        
    $DATECREATE=$DATEMODIFY=date("Y-m-d H:i:s");


        
    // Insert a row of information into the table

        
    mysql_query("INSERT INTO $tableDaily
        (owner, problem, status, dateCreate, dateModidy)

        VALUES('
    $OWNER', '$PROBLEM','$STATUS', '$DATECREATE', '$DATEMODIFY' ) ")

        or die(
    $errCon mysql_error());

        
    //owner VARCHAR(30),
        //problem VARCHAR(999),
        //status VARCHAR(1),
        //dateCreate DATETIME,   //YYYY-MM-DD HH:MM:SS
        //dateModify TIMESTAMP)")  //YYYY-MM-DD HH:MM:SS

        
    echo "Data Inserted!";

    }

    / * 
    close the PHP tags and you can dump as much HTML as you like make sure you 
        always 
    include opening and closign HTML and BODY tags too
         
    */
    ?>
    <html>
      <body>
    <form name="create_form" method="post">
    <input type="hidden" name="require" value="OWNER,PROBLEM">
    <table>
    <tr>
        <td align="right">Name:</td>
        <td><input name="OWNER" size="25"></td>
    </tr>

    <tr>
        <td align="right">Status:</td>
        <td><select name="STATUS">
        <option value="W">Work
        <option value="I">Idle
        </select>
        </td>
    </tr>

    <tr>
        <td align="right">Problem:</td>
        <td><textarea name="PROBLEM" rows="10" cols="40"></textarea>
        </td>
    </tr>

    <tr>
        <td colspan="2" align="center"><input type="submit" value="Submit" name="create_form">
        <input type="reset" value="Reset" name="reset"></td>
    </tr>
    </table>
    </form>

    </body>
    </html>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || ClickOnline ||

    Did I ever say I was an expert?

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Please mark threads resolved by going to the thread tools menu and clicking the Mark Thread Resolved button.

    Has a post really helped you? Please Rate it.

  3. #3
    Join Date
    Feb 2006
    Posts
    50

    Re: insert data from form to mySQL database

    that one gives me the form,

    it print out SUCCESS when load.. then gives

    Lost connection to MySQL server during query

  4. #4
    Join Date
    Nov 2004
    Location
    Slough, UK
    Posts
    184

    Re: insert data from form to mySQL database

    Which line gives you this error?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || ClickOnline ||

    Did I ever say I was an expert?

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Please mark threads resolved by going to the thread tools menu and clicking the Mark Thread Resolved button.

    Has a post really helped you? Please Rate it.

  5. #5
    Join Date
    Feb 2006
    Posts
    50

    Re: insert data from form to mySQL database

    It works with the below code.

    PHP Code:
    <?php include("config.php"); ?>

    <?php

    if (isset($_POST['create_form'])) {
    // this means the form was submitted
        
    submitData(); // now Call the function
    }

    function 
    submitData() {
       global 
    $dbnameDaily;
       global 
    $tableDaily;

    mysql_select_db($dbnameDaily) or die($errCon mysql_error());

    $DATECREATE=$DATEMODIFY=date("Y-m-d H:i:s");

       
    $OWNER $_POST["OWNER"];
       
    $PROBLEM $_POST["PROBLEM"];
       
    $STATUS $_POST["STATUS"];


    // Insert a row of information into the table

    mysql_query("INSERT INTO $tableDaily
    (owner, problem, status, dateCreate, dateModify)

    VALUES('
    $OWNER', '$PROBLEM','$STATUS', '$DATECREATE', '$DATEMODIFY' ) ")

    or die(
    $errCon mysql_error());

    echo 
    "Data Inserted!";

    }
    ?>


    <html>
      <body>

    <form name="create_form" method="POST" action="<?php echo $PHP_SELF ?>">
    <input type="hidden" name="require" value="OWNER,PROBLEM">
    <table>
    <tr>
        <td align="right">Name:</td>
        <td><input name="OWNER" size="25"></td>
    </tr>

    <tr>
        <td align="right">Status:</td>
        <td><select name="STATUS">
        <option value="W">Work
        <option value="I">Idle
        </select>
        </td>
    </tr>

    <tr>
        <td align="right">Problem:</td>
        <td><textarea name="PROBLEM" rows="10" cols="40"></textarea>
        </td>
    </tr>

    <tr>
        <td colspan="2" align="center"><input type="submit" value="Submit" name="create_form">
        <input type="reset" value="Reset" name="reset"></td>
    </tr>

    </table>
    </form>
    </body>
    </html>
    This just check when it's submit & call the submitData(). But I want to make sure all entries are fill in such as OWNER & PROBLEM.

    I try this and it doesn't work. Regardless, it's always call submitData() without enter info in.
    PHP Code:
    if (isset($_POST['create_form']) && isset($_POST['OWNER']) && isset($_POST['PROBLEM']) ) {
    // this means the form was submitted
        
    submitData(); // now Call the function


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured