+ Reply to Thread
Results 1 to 5 of 5

Thread: database connection

  1. #1
    sariwangi is offline x10Hosting Member sariwangi is an unknown quantity at this point
    Join Date
    Dec 2009
    Posts
    2

    database connection

    how to make connection between web page using PHP with MySQL database? I already tried to input some values using webpage but database doesn't respond it properly. The database just add 1 row, but it is empty, doesn't have any values at all.
    This is the code I use :

    <?php
    mysql_connect("localhost","username","password");
    mysql_select_db("database_name") or die("Doesn't have Database");
    $perintah = "INSERT INTO customer_table (title, writers, content, date) VALUES ('$title','$writers','$content','$date')";
    $isi_data=mysql_query($perintah);
    if (isset($isi_data))
    {
    echo("Congratulation, successfully add 1 row into database<BR><BR>");
    echo("Name: <B>$title</B><BR>");
    echo("E-mail: <B>$writers</B><BR>");
    echo("Message: <B>$content</B>");
    echo("<FORM METHOD=POST ACTION=forum.php>");
    echo("<INPUT TYPE=submit NAME=submit VALUE='Halaman Forum'></FORM>");
    echo("<FORM METHOD=POST ACTION=index.php>");
    echo("<INPUT TYPE=submit NAME=submit VALUE='Halaman Index'></FORM>");
    }
    else
    {
    echo("Failed to input the values!!");
    }

    ?>

  2. #2
    misson is offline Community Advocate misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,391

    Re: database connection

    What, exactly, isn't working? You posted a good minimal test case, but are missing a description of the behavior you expect and the behavior you get, which includes any error messages.

    When posting code, surround it with [code], [php] or [html] tags (as appropriate) to format it.

    Don't use the old MySQL driver, which is out of date; use PDO (for a usage example, see "Re: Deleting from data base using value from form.").
    Last edited by misson; 12-21-2009 at 02:31 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    sariwangi is offline x10Hosting Member sariwangi is an unknown quantity at this point
    Join Date
    Dec 2009
    Posts
    2

    Re: database connection

    I'm expecting the value that I inserted at web page stored in the databaase. The error shown before tell me the fields are empty still empty even I already put something to all of them.

    These are the coding that I use before

    KONEKSI.PHP

    <?php
    $dbhost = "localhost";
    $dbuser = "";
    $dbpass = "";
    $dbname = "";
    mysql_connect($dbhost, $dbuser, $dbpass) or die
    ("connection to MySQL failed!");
    mysql_select_db($dbname);
    $tanggal = date("Y-m-d H:i:s");
    ?>

    ================================================== ========

    FORUM.PHP (html file)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>..:: Welcome to Babilonia Website ::..</title>
    <LINK href="./files/font.css" type=text/css rel=stylesheet>
    <LINK href="./files/corpweb.css" type=text/css rel=stylesheet>
    <LINK href="./files/corpweb2.css" type=text/css rel=stylesheet>
    </head>
    <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" bgcolor="#0066FF">

    <table width="800" border="1" cellspacing="1" cellpadding="1" align="center" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
    <tr>
    <td valign="top">
    <?
    include ("header.php");
    ?>
    </td>
    </tr>

    <tr>
    <td>
    <table width="800" border="0" cellspacing="1" cellpadding="1">
    <tr>
    <td width="250" valign="top" bgcolor="#FFFFFF">
    <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
    <tr>
    <td bgcolor="#CCCCCC" align="center">
    <a class=mainmenulink onMouseOver="this.className='mainmenulinkover'" onMouseOut="this.className='mainmenulink'" href="forum.php"><font size="4">Forum</font></a></font></td>
    </tr>
    <td bgcolor="#CCCCCC" align="center"><hr></td>
    <tr>
    <td bgcolor="#CCCCCC" align="center">
    <a class=mainmenulink onMouseOver="this.className='mainmenulinkover'" onMouseOut="this.className='mainmenulink'" href="mailto:buitenzorgcity@hotmail.com"><font size="4">Contact Us</font></a>
    </td>
    </tr>
    </table>
    </td>

    <td width="550" valign="top" bgcolor="#FFFFFF">
    <table width="100%" border="0" cellspacing="1" cellpadding="1">
    <tr>
    <td bgcolor="#93CDF5">
    <?
    require("koneksi.php");
    $data2=mysql_query("select * from forum order by id");
    $jumlah=mysql_num_rows($data2);
    $limit = 10;
    if (empty($offset))
    {
    $offset = 0;
    }
    $query = "select * from forum ORDER BY id DESC LIMIT $offset, $limit";
    $result = mysql_query($query);
    // Pembagian halaman dimulai
    $halaman = intval($jumlah/$limit);
    if ($jumlah%$limit)
    {
    $halaman++;
    }
    for ($i=1; $i<=$halaman; $i++)
    {
    $newoffset=$limit*($i-1);
    if ($offset!=$newoffset)
    {
    echo "[<a href=\"forum.php?offset=$newoffset\">$i</a>]";
    }
    else
    {
    echo "[$i]";
    }
    }
    // Pembagian halaman selesai
    ?>
    </td>
    </tr>
    <tr>
    <td>
    <?php
    while ($data = mysql_fetch_array($result)) {
    echo ("
    <br>
    <b><u>$data[penulis]</b></u>, <b>$data[title]</b>, <br> $data[tanggal] <br>
    $data[content]<br>
    <hr>
    ");
    }
    ?>
    </td>
    </tr>
    <tr>
    <td>

    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <form method="post" action="gbinput.php">
    <tr>
    <td align="center" colspan="3" bgcolor="#93CDF5">
    <span class="teks10_normal"><span class="style1">Give Your Post or Comment</span></span></td>
    </tr>
    <tr>
    <td align="center" colspan="3">&nbsp;</td>
    </tr>
    <tr>
    <td><span class="teks10_normal"><span class="style1">Title</span></span></td>
    <td><div align="center"><span class="teks10_normal"><span class="style1">:</span></span></div></td>
    <td><input type="text" name="title" size="48"></td>
    </tr>
    <tr>
    <td><span class="teks10_normal"><span class="style1">Your Name</span></span></td>
    <td><div align="center"><span class="teks10_normal"><span class="style1">:</span></span></div></td>
    <td><input type="text" name="penulis" size="48"></td>
    </tr>
    <tr>
    <td><span class="teks10_normal"><span class="style1">Content</span></span></td>
    <td><div align="center"><span class="teks10_normal"><span class="style1">:</span></span></div></td>
    <td rowspan="4"><textarea rows="4" name="content" cols="36"></textarea>
    </td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td colspan="3" align="center"><br>
    <input type="submit" value="Submit" name="submit">
    <input type="reset" value="Clear" name="Clear">
    </td>
    </tr>
    </form>
    </table>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>

    <?
    include ("footer.php");
    ?>

    </body>
    </html>

    ================================================== =====

    GBINPUT.php

    <?php
    require("koneksi.php");

    if (empty($title) || empty($penulis) || empty($content))
    { $kesalahan = TRUE; }
    if (!$kesalahan)
    {
    $perintah = "INSERT INTO forum (title, penulis, content, tanggal)
    VALUES ('$title','$penulis','$content','$tanggal')";
    $hasil = mysql_query($perintah);
    if($hasil)
    {
    header("Location:forum.php");
    }
    }
    else
    {
    echo("
    <html>
    <head>
    <title>Error</title>
    <LINK href=\"./files/font.css\" type=text/css rel=stylesheet>
    <LINK href=\"./files/corpweb.css\" type=text/css rel=stylesheet>
    <LINK href=\"./files/corpweb2.css\" type=text/css rel=stylesheet>
    </head>
    <body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">
    <table width=\"800\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\" align=\"center\">
    <tr>
    <td>

    ");
    include ("header.php");
    echo("
    </td>
    </tr>
    <tr>
    <td>
    <table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\">
    <tr>
    <td colspan=\"3\">
    There are an error has occured during executing your submission!!!!
    ");
    if(empty($title)) { echo"<li>The <b>Title</b> still blank !!!!</li>"; }
    if(empty($penulis)) { echo"<li><b>Your Name</b> still blank !!!!</li>"; }
    if(empty($content)) { echo"<li><b>Content</b> still blank !!!!</li>"; }
    echo("

    </td>
    </tr>
    </table>
    <tr>
    <td>

    <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
    <form method=\"post\" action=\"gbinput.php\">
    <tr>
    <td align=\"center\" colspan=\"3\" bgcolor=\"#93CDF5\">
    <span class=\"teks10_normal\"><span class=\"style1\">Give Your Post or Comment</span></span></td>
    </tr>
    <tr>
    <td align=\"center\" colspan=\"3\">&nbsp;</td>
    </tr>
    <tr>
    <td><span class=\"teks10_normal\"><span class=\"style1\">Title</span></span></td>
    <td><div align=\"center\"><span class=\"teks10_normal\"><span class=\"style1\">:</span></span></div></td>
    <td><input type=\"text\" name=\"title\" size=\"48\"></td>
    </tr>
    <tr>
    <td><span class=\"teks10_normal\"><span class=\"style1\">Your Name</span></span></td>
    <td><div align=\"center\"><span class=\"teks10_normal\"><span class=\"style1\">:</span></span></div></td>
    <td><input type=\"text\" name=\"penulis\" size=\"48\"></td>
    </tr>
    <tr>
    <td><span class=\"teks10_normal\"><span class=\"style1\">Content</span></span></td>
    <td><div align=\"center\"><span class=\"teks10_normal\"><span class=\"style1\">:</span></span></div></td>
    <td rowspan=\"4\"><textarea rows=\"4\" name=\"content\" cols=\"36\"></textarea>
    </td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td colspan=\"3\" align=\"center\"><br>
    <input type=\"submit\" value=\"Submit\" name=\"submit\">
    <input type=\"reset\" value=\"Clear\" name=\"Clear\">
    </td>
    </tr>
    </form>
    </table>
    </td>
    </tr>

    </td>
    </tr>
    </table>

    <br>
    <br>
    </body>
    </html>
    ");
    }
    ?>

  4. #4
    misson is offline Community Advocate misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,391

    Re: database connection

    That's not a minimal test case, that's a code dump.

    You didn't post the error message:
    Quote Originally Posted by misson View Post
    [...] are missing a description of [...] the behavior you get, which includes any error messages.
    You also failed to tag the code appropriately:

    Quote Originally Posted by misson View Post
    When posting code, surround it with [code], [php] or [html] tags (as appropriate) to format it.
    Fortunately, you can edit your post and make corrections.

    A quick glance at your code and it appears to use a table based layout. Don't do this; instead, use style sheets. You're also using or die(...) for error reporting. Don't (the linked article explains why, and what you should do instead).

    As it says in the sig, make sure you read the pages I linked to in both of my posts.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,267

    Re: database connection

    PHP Code:
     

    There are an error has occured during executing your submission
    !!!!
    ");
    if(empty(
    $title)) { echo"<li>The <b>Title</bstill blank !!!!</li>"; }
    if(empty(
    $penulis)) { echo"<li><b>Your Name</bstill blank !!!!</li>"; }
    if(empty(
    $content)) { echo"<li><b>Content</bstill blank !!!!</li>"; }
    echo(" 

    If you are saying that the last three error messages show up, the reason is because you never put values into the variables $title, $penulis, and $content. register_globals is turned off. The content of form field "title" is not automatically inserted into $title.


    PHP Code:
    require("koneksi.php");

    $title '' ;
    if( isset(  
    $_POST['title'] )){

    $title $_POST['title'] ;
    }

    # etc 
    Last edited by descalzo; 12-21-2009 at 02:58 PM.

+ Reply to Thread

Similar Threads

  1. Database connection error
    By binaliws in forum Free Hosting
    Replies: 1
    Last Post: 10-08-2009, 07:44 AM
  2. database connection error
    By julie524 in forum Free Hosting
    Replies: 0
    Last Post: 01-24-2009, 02:08 PM
  3. Error establishing a database connection
    By digitaljit in forum Free Hosting
    Replies: 3
    Last Post: 09-06-2008, 05:35 AM
  4. WordPress - Error Establishing a database connection
    By daftster in forum Free Hosting
    Replies: 4
    Last Post: 12-19-2007, 09:27 AM
  5. WP mysql error : Error establishing a database connection
    By orangpelupa in forum Free Hosting
    Replies: 2
    Last Post: 12-19-2007, 06:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers