#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    6
    Rep Power
    0

    Hi everyone
    Iím completely new to this game. I have tried a couple of tutorials in Devshed and I seem to
    have the same problem with inserting into the MySQL database. In this example the script
    index.php3 shows the database in a table as expected. However, the add.php3 does not add
    to the database. I have already tested whether I have installed MySQL properly and it seems ok.
    Could someone please explain how to solve this?

    index.php3:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    <html>
    <head><title>Web Database Sample Index</title>
    </head>

    <body bgcolor=#ffffff>
    <h1>Data from mytable</h1>
    <?
    mysql_connect("localhost","username","password");


    $query = "SELECT name, phone FROM mytable";
    $result = mysql_db_query("test2", $query);

    if ($result) {
    echo "Found these entries in the database:<ul>";
    while ($r = mysql_fetch_array($result)) {
    $name = $r["name"];
    $phone = $r["phone"];
    echo "<li>$name, $phone";
    }
    echo "</ul>" ;
    } else {
    echo "No data.";
    }
    mysql_free_result($result);
    ?>

    <p><a href="add.php3">Add new entry </a>
    </body>
    </html>
    [/code]
    add.php3:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    <html>
    <head><title>Web Database Sample inserting </title>
    </head>

    <body bgcolor=#ffffff>
    <?
    if (isset($name) && isset ($phone)) {
    mysql_connect ("localhost", "username", "password");
    $query = "INSERT INTO mytable VALUES ('$name', '$phone')";
    $result = mysql_db_query("test2", "$query") ;

    if ($result) {
    echo "<p>$name was added to the database</p>" ;
    }
    }
    ?>

    <h1>Add an entry</h1>
    <form>
    Name: <input type=text name='name'><br>
    phone: <input type=text name='phone'><br
    <input type=submit>
    </form>
    <p><a href="index.php3">Back to index</a>
    </body>
    </html>
    [/code]


  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    2
    Rep Power
    0
    You need indicate in the <form> tag the action program must to do, ex:
    <form enctype="multipart/data-form" method="post" action="add.php">
    and then add a button like this:
    <input type=submit size=30 value='Send it'>
    It's work!
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    1
    Rep Power
    0
    It looks like your problem lies with your insert statement.

    INSERT INTO my_table VALUES ('$phone','$name');

    You need to specify the table columns within your insert statement.

    For example:

    If you had a table called PERSON with the columns NAME VARCHAR(32)
    AGE INTEGER

    An insert statement would look like this:

    INSERT INTO person (NAME,AGE) VALUES ('$name','$age');
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    81
    Rep Power
    14
    You don't. :-) So long as you specify values for all of the columns in the table (null or otherwise) in the order in which they were added to the table, you don't have to specify what the columns are. So, this code will work (barring any typos I'm about to make):
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    create table EMP (
    first_name VARCHAR(30),
    surname VARCHAR(30),
    email_id VARCHAR(10),
    PRIMARY KEY( email_id ) );

    insert into EMP values ('Chris', 'Tucker', 'christuc' );
    [/code]

Similar Threads

  1. Need advice designing a usage tracking database (MySQL)
    By Randolpho in forum Database Management
    Replies: 0
    Last Post: February 13th, 2004, 04:11 PM
  2. Check MySQL Version thru PHP
    By wolftrap1 in forum PHP Development
    Replies: 2
    Last Post: February 11th, 2004, 08:51 PM
  3. Replies: 5
    Last Post: February 11th, 2004, 02:18 AM
  4. Replies: 5
    Last Post: February 3rd, 2004, 01:59 PM
  5. Help using a database and php to update a scores page.
    By deathsythe in forum PHP Development
    Replies: 0
    Last Post: February 2nd, 2004, 02:48 PM

IMN logo majestic logo threadwatch logo seochat tools logo