INSERT INTO mysql Using Php
Results 1 to 6 of 6

Thread: INSERT INTO mysql Using Php

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Location
    South Africa
    Posts
    3

    INSERT INTO mysql Using Php

    I have a html form and would like to insert its input to a mysql database using a php file. i am new to php , the version i have is Php version = 5.2.17
    PhpMyAdmin version = 3.4.6 and Mysql version 5.5.16
    I have been trying for 2 long weeks to figure which php script will allow my data to be added to the mysql table. i found a script and it does submit the data however it does not submit the data entered into the form. See my script:

    FIRST PHP I TRIED:
    ------------------------
    <?php

    /*** mysql hostname ***/
    $hostname = 'localhost';

    /*** mysql username ***/
    $username = 'root';

    /*** mysql password ***/
    $password = 'root';

    /*** mysql database name ***/
    $dbname = 'spierform';

    /*** create a new mysqli object with default database***/
    $mysqli = @new mysqli($hostname, $username, $password, $dbname);

    /* check connection */
    if(!mysqli_connect_errno())
    {
    /*** if we are successful ***/
    echo 'Connected Successfully<br />';

    /*** sql to INSERT a new record ***/
    $sql = "INSERT INTO feedback (FirstName, LastName, Department, Technician, TicketNo, Rating_Tech, Rating_Team, Comments)
    VALUES ('FirstName', 'LastName', 'Department', 'Technician', 'TicketNo', 'Rating_Tech', 'Rating_Team', 'Comments')";



    if($mysqli->query($sql) === TRUE)
    {
    echo 'New record created successfully<br />';
    }
    else
    {
    echo $sql.'<br />' . $mysqli->error;
    }

    /*** close connection ***/
    $mysqli->close();
    }
    else
    {
    /*** if we are unable to connect ***/
    echo 'Unable to connect';
    exit();
    }
    ?>


    THE SECOND PHP SCRIPT I TRIED
    ---------------------------------------
    <?php

    define('DB_NAME', 'spierform');
    define('DB_USER', 'root');
    define('DB_PASSWORD', 'root');
    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());
    }

    $FirstName = $_POST['FirstName'];
    $LastName = $_POST['LastName'];
    $Department = $_POST['Department'];
    $Technician = $_POST['Technician'];
    $TicketNo = $_POST['TicketNo'];
    $Rating_Tech = $_POST['Rating_Tech'];
    $Rating_Team = $_POST['Rating_Team'];
    $Comments = $_POST['Comments'];




    $sql = "INSERT INTO feedback (FirstName, LastName, Department, Technician, TicketNo, Rating_Tech, Rating_Team, Comment) VALUES ('$FirstName', '$LastName', '$Department', '$Technician', '$TicketNo', '$Rating_Tech', '$Rating_Team', '$Comments')";


    {
    die('Error' . mysql_error());
    }

    mysql_close();
    ?>



    I hope someone can help me and guide me in the right direction , i am so frustrated that i still cannot get the data into the db table
    thank you for your assistance and advice in advancce

  2. #2
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,904
    Well, the first one does the job of putting data into a table, so the database code from that is largely correct. But it doesn't do anything with any form information.

    The second one does get information from a POSTed form, but doesn't do anything useful with regard to the database.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Location
    South Africa
    Posts
    3
    Thanks for your reply weedpacked , that is just my dilema that no insert into script will insert data to my table , i have tried and tested the insert into code in mysql and phpmyadmin and none like the insert into code :

    "INSERT INTO feedback (FirstName, LastName, Department, Technician, TicketNo, Rating_Tech, Rating_Team, Comments)
    VALUES ('FirstName', 'LastName', 'Department', 'Technician', 'TicketNo', 'Rating_Tech', 'Rating_Team', 'Comments')";

    Phpmyadmin and mysql does not like the quotation marks , if i take the quotation marks away it executes in mysql and phpmyadmin ,however i don't know which insert into code will work. I evens tried mysqli , but get errors eventhough mysqli is enabled. so i don't know what to do next , will appreciate more help if possible as i have a deadline to get the form data into the db table and im stressing , being a newbie in php is not fun , but i do like php and want to grow in it

    thanks for you help thus far

  4. #4
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,721
    Please be specific about which quotation marks aren't "liked" by your tools.

    Also, presumably there is an error message. Could you obtain the output of mysql_error() for this query?
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  5. #5
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,904
    There won't be an error because he never actually calls mysql_query.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Location
    South Africa
    Posts
    3
    Do you see the quotation marks infront of INSERT INTO and and the end of the closed bracket after comments. that does not work in mysql workbench nor does it work in phpmyadmin , if i remove the quotation marks it does execute in both tools , but here is the catch when i submit my form nothing happens. i really don't no what the correct script should be to allow my html form data to be entered into the mysql db

Thread Information

Users Browsing this Thread

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

Posting Permissions

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