database connection

sariwangi

New Member
Messages
2
Reaction score
0
Points
0
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!!");
}

?>
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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 [URL="http://php.net/PDO"]PDO[/URL] (for a usage example, see "[URL="http://forums.x10hosting.com/programming-help/105919-deleting-data-base-using-value-form.html#post604576"]Re: Deleting from data base using value from form.[/URL]").
 
Last edited:

sariwangi

New Member
Messages
2
Reaction score
0
Points
0
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>
");
}
?>
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
That's not a minimal test case, that's a code dump.

You didn't post the error message:
[...] are missing a description of [...] the behavior you get, which includes any error messages.

You also failed to tag the code appropriately:

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

Fortunately, you can edit your post and make corrections.

A quick glance at your code and it appears to use a [url=http://davespicks.com/essays/notables.html]table based layout[/url]. Don't do this; instead, use style sheets. You're also using [URL="http://www.phpfreaks.com/blog/or-die-must-die"]or die(...)[/URL] 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.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
PHP:
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("


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:
require("koneksi.php");

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

$title = $_POST['title'] ;
}

# etc
 
Last edited:
Top