www.webdeveloper.com
Results 1 to 7 of 7

Thread: Form data does not display

Hybrid View

  1. #1
    Join Date
    Jul 2013
    Posts
    5

    Form data does not display

    I have created a program where it displays the data from the database and it will also display what is enter in the form. There are 3 php files with the program. create.php, index.php and connection.php. A folder called mysqltut is created and within that is the folder includes. Includes folder has connection.php(/var/www/html/mysqltut/includes/connection.php, and create.php and index.php is in /var/www/html/mysqltut Index.php page displays the rows from the database
    but does not add to the list when the user adds name and desc.
    connection.php
    <?php
    $dbhost='localhost';
    $dbuser = 'root';
    $dbpass = '888888';
    $db = 'mysql_tut';
    $conn = mysql_connect($dbhost,$dbuser,$dbpass);
    mysql_select_db($db);
    ?>

    index.php
    <?php
    include 'includes/connection.php';

    $query = "SELECT * FROM people";

    $result = mysql_query($query);

    while($person = mysql_fetch_array($result)) {
    echo "<h3>".$person["Name"] . "</h3>";
    echo "<p>".$person["Description"] ."</p>";
    }
    ?>
    <h1>Create A User</h>
    <form action="create.php" method="post">
    Name<input type="text" name="inputName" value=""/><br />
    Description<input type="text" name="inputDesc" value=""/>
    <br />
    <input type="submit" name="submit" />
    </form>


    create.php
    <?php
    include 'includes/connection.php';

    $name = $_POST['inputName'];
    $desc = $_POST['inputDesc'];

    if (!$_POST['submit']) {
    echo "please fill out the form";
    header ('Location: index.php');
    }else {
    mysql_query = ("INSERT INTO people (`ID`, `Name`, `Description`)
    VALUES (NULL, '$name', '$desc')") or die (mysql_error());
    echo "User has been added!";
    header('Location:index.php');

    }

    ?>

  2. #2
    Join Date
    Nov 2006
    Location
    Springfield, MO
    Posts
    1,541
    Pretty sure you're missing the value on the submit button:

    Code:
    <input type="submit" name="submit" value="submit">
    And I would change your logic a bit:

    PHP Code:
    if (isset($_POST['submit'])) {
    mysql_query = ("INSERT INTO people (`ID`, `Name`, `Description`)
    VALUES ('
    $name', '$desc')") or die (mysql_error());
    echo 
    "User has been added!";
    header('Location:index.php');
    }
    else {
    echo 
    "please fill out the form";
    header ('Location: index.php');

    As long as you're seeding your primary key on your database you don't need to provide a parameter for that. Been awhile for PHP but that should work.
    Ryan Butler

    Ryan Butler.org

  3. #3
    Join Date
    Jul 2013
    Posts
    5
    I have changed the code as you have suggested but after I enter the value in index.php I see a blank screen on create.php

  4. #4
    Join Date
    Nov 2006
    Location
    Springfield, MO
    Posts
    1,541
    Strange...try this:

    PHP Code:
    if (isset($_POST['submit'])) {
    $sql="INSERT INTO people (`ID`, `Name`, `Description`)VALUES ('$name', '$desc')";
    //perform the query
    $result=mysql_query($sql));
    //check if query was successful
    if($result){
    echo 
    "User has been added!";
    header('Location:index.php');
    }
    else {
    //display error message
    echo "Create record was not successful: " mysql_error();

    Ryan Butler

    Ryan Butler.org

  5. #5
    Join Date
    Jul 2013
    Posts
    5
    Hello!

    True this is strange, do not know where I am wrong but I enter the information in http://server/mysqltut/index.php, when I click on submit it see a blank screen on http://chitratest.devnet.imsa.edu/mysqltut/create.php. Same result after I have added your new code. I have created a folder mysqltut, and within that I have php file create.php
    and index.php and within includes file I have connection.php.

  6. #6
    Join Date
    Jul 2013
    Posts
    5
    Hello!

    True this is strange, do not know where I am wrong but I enter the information in http://server/mysqltut/index.php, when I click on submit it see a blank screen on http://chitratest.devnet.imsa.edu/mysqltut/create.php. Same result after I have added your new code. I have created a folder mysqltut, and within that I have php file create.php
    and index.php and within includes file I have connection.php. Includes folder is created within mysqltut.

  7. #7
    Join Date
    Nov 2006
    Location
    Springfield, MO
    Posts
    1,541
    Oh, duh, your post variable values:

    PHP Code:
    $name $_POST['inputName'];
    $desc $_POST['inputDesc']; 
    Don't have scope b/w index and create. When you type in on index and re-direct to create, create has no idea about the previous form values on index. You need to include the PHP code from create inside index.php, then it will work. Told you it'd been awhile since PHP.
    Ryan Butler

    Ryan Butler.org

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles