Problem Add Data Into Database (Mysql)

Discussion in 'PHP' started by Sasikumar, Mar 17, 2008.

  1. #1
    I try to add data into database.. but i unable do that
    can any want help me to solve this problem?

    Add Data :

    <form action="add_process.php" method="post">
    <p>Name:
    <input type="text" name="name">
    <br>
    Address:
    <input type="text" name = "address" />
    <br
    Country:
    <input type="text" name = "country">
    <br />
    City:
    <input name = "text" type="text" id="city" />
    <br />
    Suburb:
    <input name = "text" type="text" id="suburb" />
    <br />
    Telephone No:
    <input name = "text" type="text" id="telephone" />
    </p>
    Cuisine:
    <input name = "text" type="text" id="cuisine" />
    <br />
    Food:
    <input name = "text" type="text" id="food" />
    <p><br>
    <input type="submit" value="Submit">
    </p>
    </form>



    Connection Coding :

    <?
    $fname=$_POST['name'];
    $faddress=$_POST['address'];
    $fcountry=$_POST['country'];
    $fcity=$_POST['city'];
    $fsuburb=$_POST['suburb'];
    $ftelephone=$_POST['telephone'];
    $fcuisine=$_POST['cuisine'];
    $ffood=$_POST['food'];

    mysql_connect("localhost", "webzwork_red", "58281044") or die(mysql_error());
    mysql_select_db("webzwork_red") or die(mysql_error());
    mysql_query("INSERT INTO `users` VALUES ('$fname', '$faddress', '$fcountry', '$fcity', '$suburb', '$ftelephone', '$cuisine', '$ffood')");
    Print "Your information has been successfully added to the database.";
    ?>
     
    Sasikumar, Mar 17, 2008 IP
  2. nebhotep

    nebhotep Active Member

    Messages:
    244
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #2
    first off all, your form is designed wrong. You have multiple item named 'text'
    try using
    
    <form action="" method="post">
    <p>Name:
    <input type="text" name="name">
    <br>
    Address:
    <input type="text" name = "address" />
    <br />
    Country:
    <input type="text" name = "country" />
    <br />
    City:
    <input  type="text" id="city" name="city" />
    <br />
    Suburb:
    <input  type="text" id="suburb" name="suburb" />
    <br />
    Telephone No:
    <input  type="text" id="telephone" name="telephone" />
    </p>
    Cuisine:
    <input  type="text" id="cuisine" name="cuisine" />
    <br />
    Food:
    <input type="text" id="food" name="food" />
    <p><br>
    <input type="submit" value="Submit">
    </p>
    </form>
    Code (markup):
    this should work
     
    nebhotep, Mar 17, 2008 IP
  3. Sasikumar

    Sasikumar Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    still can not save the data into database
     
    Sasikumar, Mar 17, 2008 IP
  4. nebhotep

    nebhotep Active Member

    Messages:
    244
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #4
    are you getting any error?
     
    nebhotep, Mar 17, 2008 IP
  5. nebhotep

    nebhotep Active Member

    Messages:
    244
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #5
    try putting your variables between {} in queries: like I show bellow

    mysql_query("INSERT INTO `users` VALUES ('{$fname}', '{$faddress}', '{$fcountry}', '{$fcity}', '{$suburb}', '{$ftelephone}', '{$cuisine}', '{$ffood}')");
    Code (markup):
     
    nebhotep, Mar 17, 2008 IP
  6. Sasikumar

    Sasikumar Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    still same.. check this site

    webzworks.com/web_project_2008/red/login/add.php

    webzworks.com/web_project_2008/red/login/add_process.php
     
    Sasikumar, Mar 17, 2008 IP
  7. olddocks

    olddocks Notable Member

    Messages:
    3,275
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    215
    #7
    You have a wrong SQL query. You missed the fields before values. An example is below.

     
    olddocks, Mar 17, 2008 IP
  8. nebhotep

    nebhotep Active Member

    Messages:
    244
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #8
    in the form page put
    <form action="add_process.php" method="post">
    Code (markup):
    I have removed add_process.php, when i tested on my site.

    If still does not work, post here the code of add_process.php, or PM-it to me
     
    nebhotep, Mar 17, 2008 IP
  9. Sasikumar

    Sasikumar Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    add_porcess.php

    <?
    $fname=$_POST['name'];
    $faddress=$_POST['address'];
    $fcountry=$_POST['country'];
    $fcity=$_POST['city'];
    $fsuburb=$_POST['suburb'];
    $ftelephone=$_POST['telephone'];
    $fcuisine=$_POST['cuisine'];
    $ffood=$_POST['food'];

    mysql_connect("localhost", "webzwork_red", "58281044") or die(mysql_error());
    mysql_select_db("webzwork_red") or die(mysql_error());
    mysql_query("INSERT INTO `users` ('name','address','country','city','suburb','telephone','cuisine','food')VALUES ('{$fname}', '{$faddress}', '{$fcountry}', '{$fcity}', '{$suburb}', '{$ftelephone}', '{$cuisine}', '{$ffood}')");
    Print "Your information has been successfully added to the database.";
    ?>
     
    Sasikumar, Mar 17, 2008 IP
  10. nebhotep

    nebhotep Active Member

    Messages:
    244
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #10
    $con = mysql_connect("localhost", "webzwork_red", "58281044") or die(mysql_error());
    mysql_select_db("webzwork_red",$con ) or die(mysql_error());
    mysql_query("INSERT INTO `users` ('name','address','country','city','suburb','telephone','cuisine','food')VALUES ('{$fname}', '{$faddress}', '{$fcountry}', '{$fcity}', '{$suburb}', '{$ftelephone}', '{$cuisine}', '{$ffood}')",$con );
    Code (markup):
     
    nebhotep, Mar 17, 2008 IP
  11. Sasikumar

    Sasikumar Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    this is mysql file. i not sure whether is correct or not pls advice


    !note : i can't add new records


    -- phpMyAdmin SQL Dump
    -- version 2.11.4
    -- http://www.phpmyadmin.net
    --
    -- Host: localhost
    -- Generation Time: Mar 17, 2008 at 05:51 PM
    -- Server version: 4.1.22
    -- PHP Version: 5.2.3

    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

    --
    -- Database: `webzwork_red`
    --

    -- --------------------------------------------------------

    --
    -- Table structure for table `users`
    --

    CREATE TABLE `users` (
    `no` int(16) NOT NULL auto_increment,
    `fname` varchar(30) NOT NULL default '',
    `faddress` varchar(255) NOT NULL default '',
    `fcountry` varchar(255) NOT NULL default '',
    `fcity` varchar(255) NOT NULL default '',
    `fsuburb` varchar(255) NOT NULL default '',
    `ftelephone` varchar(255) NOT NULL default '',
    `fcuisine` varchar(255) NOT NULL default '',
    `ffood` varchar(255) NOT NULL default '',
    PRIMARY KEY (`no`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

    --
    -- Dumping data for table `users`
    --

    INSERT INTO `users` VALUES(1, 'Al Mirjan Café', 'Lot 415, Level 4, Suria KLCC, 50088 Kuala Lumpur', 'Malaysia', 'Kuala Lumpur', 'City Centre', '603 2168 8557', 'Middle East', 'Standard');
    INSERT INTO `users` VALUES(2, 'Chef Choi', '159, Jalan Ampang, 50450 Kuala Lumpur', 'Malaysia', 'Kuala Lumpur', 'City Centre', '603 2163 5866', 'Chinese', 'Fine Dining');
    INSERT INTO `users` VALUES(3, 'Khaana Peena', 'E-01-04/05 Plaza Mont'' Kiara, 2 Jalan Kiara, Kuala Lumpur', 'Malaysia', 'Kuala Lumpur', 'Mont Kiara', '603 6201 3355', 'Indian', 'Fine Dining');
    INSERT INTO `users` VALUES(4, 'Seafood Paradise', '8, Jalan Conlay, 50450 Kuala Lumpur', 'Malaysia', 'Kuala Lumpur', 'City Centre', '603 2143 1101', 'International', 'Seafood');
    INSERT INTO `users` VALUES(5, 'Tamarind Hill', '1, Jalan Kerja Air Lama, 68000 Ampang, Selangor', 'Malaysia', 'Kuala Lumpur', 'City Centre', '603 4256 9100', 'Indo-chinese', 'Fine Dining');
    INSERT INTO `users` VALUES(6, 'The Rib Shop', '120-122, Jalan Kasah, Damansara Heights, 50490 Kuala Lumpur', 'Malaysia', 'Kuala Lumpur', 'Damansara Heights', '603 2095 9729', 'Western', 'Steaks');
    INSERT INTO `users` VALUES(7, 'Alexis', '29, JalanTelawi 3, Bangsar Baru, 59100 Kuala Lumpur', 'Malaysia', 'Kuala Lumpur', 'Bangsar', '603 2284 2880', 'Fusion', 'Standard');
    INSERT INTO `users` VALUES(8, 'Annalakshmi', 'Lot G(E) 017,Ground Floor, Mid Valley Megamall, Mid Valley City, Lingkaran Syed Putra, 59200 Kuala Lumpur', 'Malaysia', 'Kuala Lumpur', 'Mid Valley', '603 7725 4164', 'Indian', 'Fine Dining');
    INSERT INTO `users` VALUES(9, 'Antonio''s Calabria', '32, Jalan Wan Kadir 4, Off Jalan Damansara, Taman Tun Dr Ismail, 60000 Kuala Lumpur', 'Malaysia', 'Kuala Lumpur', 'Taman Tun Dr Ismail', '603 7725 4164', 'Italian', 'Standard');
    INSERT INTO `users` VALUES(10, 'Aoyama The Green Room', 'No.10 (Ground Floor), Jalan 27/70A, Desa Sri Hartamas, 50480 Kuala Lumpur', 'Malaysia', 'Kuala Lumpur', 'Sri Hartamas', '603 2300 0683', 'Japanese', 'Standard');
     
    Sasikumar, Mar 17, 2008 IP
  12. nebhotep

    nebhotep Active Member

    Messages:
    244
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #12
    your insert statement is wrong check the table names

    you write mysql_query("INSERT INTO `users` ('name','address','country',....
    but your column names are starting with f
    like
    mysql_query("INSERT INTO `users` ('fname','faddress','fcountry',....
     
    nebhotep, Mar 17, 2008 IP
  13. Sasikumar

    Sasikumar Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    can u help me to solve this code?
    currently i can't able to add new records in my database
     
    Sasikumar, Mar 17, 2008 IP
  14. deepak3018

    deepak3018 Peon

    Messages:
    353
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #14
    In the input form you are using "post" in small letter .Change it into capital and then try.
     
    deepak3018, Mar 17, 2008 IP
  15. Sasikumar

    Sasikumar Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    i try change the "post" as u told. but i still facing the same problem
    is my primary key correct in my database?
     
    Sasikumar, Mar 17, 2008 IP
  16. deepak3018

    deepak3018 Peon

    Messages:
    353
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Primary key is working correctly in my system according to your query.I am trying for your problem..
     
    deepak3018, Mar 17, 2008 IP
  17. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #17
    use this query

     
    salmanshafiq, Mar 17, 2008 IP
  18. nebhotep

    nebhotep Active Member

    Messages:
    244
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #18
    this is the correct version

    mysql_query("INSERT INTO `users` (no,'fname','faddress','fcountry','fcity','fsuburb','ftelephone','fcuisine','ffood')VALUES ('','{$fname}', '{$faddress}', '{$fcountry}', '{$fcity}', '{$suburb}', '{$ftelephone}', '{$cuisine}', '{$ffood}')",$con );
    Code (markup):
    watch the table column names
     
    nebhotep, Mar 17, 2008 IP
  19. deepak3018

    deepak3018 Peon

    Messages:
    353
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #19
    This is correct for your problem..

    <?
    $fname=$_POST['name'];
    $faddress=$_POST['address'];
    $fcountry=$_POST['country'];
    $fcity=$_POST['city'];
    $fsuburb=$_POST['suburb'];
    $ftelephone=$_POST['telephone'];
    $fcuisine=$_POST['cuisine'];
    $ffood=$_POST['food'];
    echo $fname;
    echo '<br>';
    echo $faddress;
    echo '<br>';
    echo $fcountry;
    echo '<br>';
    echo $fcity;
    echo '<br>';
    echo $fsuburb;
    $conns=mysql_connect("localhost", "root", "root") or die(mysql_error());
    mysql_select_db("webzwork_red") or die(mysql_error());
    $query="INSERT INTO users VALUES ('','$fname', '$faddress', '$fcountry', '$fcity', '$suburb', '$ftelephone', '$cuisine', '$ffood')";
    $result=mysql_query($query,$conns) or die(mysql_error());
    Print "Your information has been successfully added to the database.";
    ?>



    And it is for your input form ..


    <form action="add_process.php" method="POST">
    <p>Name:
    <input type="text" name="name">
    <br>
    Address:
    <input type="text" name = "address" />
    <br>
    Country:
    <input type="text" name = "country">
    <br />
    City:
    <input name = "city" type="text" id="city" />
    <br />
    Suburb:
    <input name = "suburb" type="text" id="suburb" />
    <br />
    Telephone No:
    <input name = "telephone" type="text" id="telephone" />
    </p>
    Cuisine:
    <input name = "cuisine" type="text" id="cuisine" />
    <br />
    Food:
    <input name = "food" type="text" id="food" />
    <p><br>
    <input type="submit" value="Submit">
    </p>
    </form>
     
    deepak3018, Mar 17, 2008 IP
  20. deepak3018

    deepak3018 Peon

    Messages:
    353
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #20
    set your sql connection information.
     
    deepak3018, Mar 17, 2008 IP