#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. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    carl,

    Some mistakes are their in the script.

    here is the an example for inserting a record to database..

    add.php3:


    <html>
    <head><title>Web Database Sample inserting </title></head>
    <body bgcolor=#ffffff>
    <?
    if (isset($submit))
    //when click on submit button..
    {

    $con=mysql_connect("localhost", "username", "password");
    //database connectivity..


    mysql_select_db("yourdatabasename",$con);
    //select your database name

    $query = "INSERT INTO mytable(name,phone) VALUES ('$name', '$phone')";
    //sql query

    $result = mysqlquery($query,$con) ;
    //issue the insert query..


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

    <form action="add.php3" method="post">
    Name:<input type=text name="name"><br>

    phone: <input type=text name="phone"><br>
    <input type="submit" name="submit" value="submit">
    </form>

    <p><a href="index.php3">Back to index</a>

    </body></html>


    ---
    Make sure that you are creating "mytable with name ane phone" in your database..

    ---


    -----------

    index.php3



    <html><head><title>Web Database Sample Index</title></head>


    <body bgcolor=#ffffff><h1>Data from mytable</h1>

    <?
    $con=mysql_connect("localhost", "username", "password");
    //database connectivity..


    mysql_select_db("yourdatabasename",$con);
    //select your database name

    $query = "SELECT name, phone FROM mytable";
    //sql query

    $result = mysqlquery($query,$con) ;
    //issue the insert query..



    if (mysql_num_rows($result)>0) {

    //some records are their..

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

    <p><a href="add.php3">Add new entry </a>
    </body>
    </html>


    Good Luck!!


    ------------------
    SR -
    webshiju.com

    "The fear of the LORD is the beginning of knowledge..."

    [This message has been edited by Shiju Rajan (edited August 08, 2000).]

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: 0
    Last Post: February 10th, 2004, 11:44 PM
  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