How to Create an HTML Form That Stores Data in a MySQL Database Using PHP Part 2 of 4




Watch the updated version of this tutorial here: https://www.youtube.com/watch?v=BmHLvUdkCA0&list=PL530D33D6E548481F&index=1 For more video …

Original source


44 responses to “How to Create an HTML Form That Stores Data in a MySQL Database Using PHP Part 2 of 4”

  1. Demo-form.php

    <html>
    <body>

    <form action="demo.php" method="post">
    <p>
    </br>
    <label> Name: <input type="text" name="name">
    </br></br>
    <label> Submit This Query Please! <input type="submit" value="Go For It!">
    </p>
    </form>
    </body>
    <html>

    Demo.php

    <?php

    define('DB_NAME', 'form1');
    define('DB_USER', 'root');
    define('DB_PASSWORD', '');
    define('DB_HOST', 'localhost');

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

    if(!$link)
    {
    die ('Could not Connect: ' . mysql_error());
    }
    $db_selected = mysql_select_db(DB_NAME, $link);

    if(!$db_selected)
    {
    die('can't use ' . DB_NAME . ': ' .mysql_error()) ;
    }
    echo " Dang";

    $value = $_POST['name'];

    $sql = "INSERT INTO demo (name) Values ('$value')";

    if (!mysql_query($sql))
    {
    die('Error: ' . mysql_error());
    }

    mysql_close();
    ?>

    I don't know what's wrong in this code when i am trying to compile my code it says error in this line . i have no idea 

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

  2. Thank you so much for this series of videos! I'm definitely going to join. I like you teaching style. I'm having a problem and I'm hoping your still around to help out. I have my files live on my website. however i keep getting the error:
    Could not connect: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

    What does that mean? What do i have to do to fix it?

  3. <?php

    define('DB_NAME', 'forms1');
    define('DB_USER', 'root');
    define('DB_PASSWORD', '');
    define('DB_HOST', 'localhost');

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }

    $db_selected = mysql_select_db(DB_NAME, $link);

    if (!$db_selected) {
        die('Cant use ' . DB_NAME . ': ' . mysql_error());
    }     

    echo 'Connected Successfully';

    mysql_close();
    ?>
     NEED HELP

  4. how should i know if my code is correct.
    here is my code:
    demo.php

    <?php

    define('DB_NAME', 'forms1');
    define('DB_USER', 'root');
    define('DB_PASSWORD', '');
    define('DB_HOST', 'localhost');

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

    if (!$link) {
    die('Could not connect: ' . mysql_error());
    }

    $db_selected = mysql_select_db(DB_NAME, $link);

    if (!$db_selected) {
    die('Can't use ' . DB_NAME . ': ' . mysql_error());
    }
    ?>

    demo-form.php

    <form action="demo.php" method="post" />
    <p>Input 1: <input type="text" name="input" /></p>
    <input type="submit" value="Submit" />
    </form>

    forms1

    — phpMyAdmin SQL Dump
    — version 3.3.9
    http://www.phpmyadmin.net

    — Host: localhost
    — Generation Time: Aug 19, 2014 at 07:05 AM
    — Server version: 5.5.8
    — PHP Version: 5.3.5

    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

    /!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT/;
    /!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS/;
    /!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION/;
    /*!40101 SET NAMES utf8 */;


    — Database: `forms1`

    CREATE DATABASE `forms1` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
    USE `forms1`;

    — ——————————————————–


    — Table structure for table `demo`

    CREATE TABLE IF NOT EXISTS `demo` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `input1` varchar(20) DEFAULT NULL,
      PRIMARY KEY (`ID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


    — Dumping data for table `demo`

    i really need to know if its right or if it is wrong…

  5. When I downloaded the demo from your actual site it didn't give me file with "demo-form.php" only a "demo.php" that is the form. It looks like you have six files open but I only have 5 that got downloaded. Help!!

    Also do you happen to have a tutorial on how to create those country, state, city selectors? That's really what I'm looking for but I'm hoping to make it myself with the knowledge you have provided.

    Let me know. 🙂

    Thanks,
    Naya

  6. Whenever I go to make the database I do the ID and input1. When I go to get the final product, I get "MySQL returned an empty result set (i.e. zero rows). (Query took 0.0000 sec)" and I don't know what to do.

  7. just a bullshit first of all i just came here to know how the informations stores into the database and you've described a lot about css and html (very disturbing) after that when i saw the main thing that i'm here for but its also not fully cleared you have done many things without showing how to

  8. Yes here is my script.
    <?php
    define ('DB_NAME','newebay');
    define ('DB_USER','root');
    define ('DB_PASSWORD','bh05238');
    define ('DB_HOST','localhost');

    $link = mysql_connect('DB_HOST','DB_USER','DB_PASSWORD');

    IF (!$link) {
     die('Could not Connect :
        ' . mysql_error());
     
    }

    $db_selected = mysql_select_db (DB_NAME, $link);

    if (!$db_selected) {
    die('Can't use ' . DB_NAME . ': ' . mysql_error());

    }
    echo 'Connected successfully';

    ?>

    Thanks for the Fast response!

  9. Hi John,  Love your videos so far.  Having a issue trying to get php to connect to mysql.  I keep getting this message. "php_network_getaddresses: getaddrinfo failed: Name or service not known".  I think the issue is the 'DB_HOST'.  I verified using mysql workbench that its name is local host.  I even tried putting the port number into the variable 'DB_HOST' = 'localhost:3306' and still didn't work.  What do you suggest?

  10. @Justin Emerson
    If you delete the function he wrote in there that is called $value=$_POST['input1'];
    and then where he has the next line of code stating $sql = "INSERT INTO demo (input1) VALUES ('$value'); and change it to $sql = "INSERT INTO Pledge (ID, Names) VALUES ('','$_POST[input1]')";
    then it should work. Just remember that you need to add security to something like this and if you don't it is very easy to retrieve the information in your box by simply putting a retrieve code.

  11. hi John, I have a question Where can I find the PHP codes where you are showing define ('DB_name', demo') on my form I indicated ID, Login and Password (3 columns) but I do not know where can I find these code that you are showing PHP

Leave a Reply