Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 10-18-2008, 08:23 AM form related query
Experienced Talker

Posts: 31
Name: Ignatius Vinoth
Trades: 0
Dear Friends,

As Im new to PHP I have writen a simple script with all u pplz help. The problem now Im facing is:

1. Everytime I click refresh(F5) button it adds one blank data in my sql table.
2. Im not able to avoid blank entries in the form.
3. If click on submit it again gives one blank entry.
4.If I click on echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page it again gives one blank entry.

Hope u the ppl can support me.

Thing which I need is:
1. blank submit should give error message.
2.No other source should add blank or any count in my table content.

Note: I have enter not null in all my table fields.




<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="guestbook"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="INSERT INTO $tbl_name(nick, email, comments)VALUES('$nick', '$email', '$comments')";
$result=mysql_query($sql);
//check if query successful
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page
}
else {
echo "ERROR";
}
mysql_close();
?>


<?php
$host="localhost"; // Host name
$username="****"; // Mysql username
$password="***"; // Mysql password
$db_name="***"; // Database name
$tbl_name="***"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><? echo $rows['nick']; ?></td>
<td>Email</td>
<td>:</td>
<td><? echo $rows['email']; ?></td>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><? echo $rows['comments']; ?></td>
<?
}
mysql_close(); //close database
?>
iggywiggy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-18-2008, 09:04 AM Re: form related query
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
You could not insert if there are no values in the variables $nick, $email, and $comments by wrapping the mysql_query in an if() statement, like this:

PHP Code:
if(!empty($nick) && !empty($email) && !empty($comments)) {
$sql="INSERT INTO $tbl_name(nick, email, comments)VALUES('$nick', '$email', '$comments')";
$result=mysql_query($sql);

The empty() function returns true if a variable is empty, which includes the value 0 or an empty string "". !empty() would be the opposite, so if there are values in all of the strings it will do the mysql_query().

***EDIT***
actually, you will want to wrap the if() all the way to the block reading:
PHP Code:
else {
echo 
"ERROR";

so that you're not echoing "ERROR" every time there is no insertion.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.

Last edited by wayfarer07; 10-18-2008 at 09:06 AM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 10-18-2008, 09:18 AM Re: form related query
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
check for the form values being blank before the insert and redirect back to the same page after a successful insert.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-18-2008, 09:19 AM Re: form related query
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
moral is

"Don't talk to the other half while writing a post"
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-18-2008, 12:51 PM Re: form related query
Experienced Talker

Posts: 31
Name: Ignatius Vinoth
Trades: 0
Dear Friends,

Sorry to disturb u. Now I have written the blow codings..The problem persist is,

1. No data added in table.
2. Nor it shows in my page.
3. As well as im not getting any error message.

<html>
<body>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>Test Sign Guestbook </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="viewguestbook.php">
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><input name="nick" type="text" id="nick" size="40" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email" size="40" /></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td> </---100--->
<td><textarea name="comments" cols="40" rows="3" id="comments"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
$host="localhost"; // Host name
$username="***"; // Mysql username
$password="****"; // Mysql password
$db_name="worldzyo_customer"; // Database name
$tbl_name="guestbook"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$errmsg = ''; // error message
$nick = ''; // sender's name
$email = ''; // sender's email addres
$comments = ''; // the message itself
if(isset($_POST['submit']))
{
$nick = $_POST['nick'];
$email = $_POST['email'];
$comments = $_POST['comments'];

if(trim($nick) == '')
{
$errmsg = 'Please enter your nick';
}
else if(trim($email) == '')
{
$errmsg = 'Please enter your email address';
}
else if(!isEmail($email))
{
$errmsg = 'Your email address is not valid';
}
else if(trim($comments) == '')
{
$errmsg = 'Please enter comments';
}

$sql="INSERT INTO $tbl_name(nick, email, comments)VALUES('$nick', '$email', '$comments')";
$result=mysql_query($sql);
//check if query successful
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page
}
else {
echo "ERROR";
}
mysql_close();
?>
<?php
$host="localhost"; // Host name
$username="****"; // Mysql username
$password="*****"; // Mysql password
$db_name="worldzyo_customer"; // Database name
$tbl_name="guestbook"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><? echo $rows['nick']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><? echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><? echo $rows['comments']; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?
}
mysql_close(); //close database
}
?>
</body>
</html>

As well as when ever I click on refresh button I use to get below attachment error,
Attached Images
File Type: jpg error.JPG (21.2 KB, 3 views)
iggywiggy is offline
Reply With Quote
View Public Profile
 
Old 10-18-2008, 01:10 PM Re: form related query
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
You didn't do what I said. All that error message means is that there is $_POST data still on the page, if you refresh it will resend it.

***EDIT***
BTW, please read this before posting any more code: http://www.webmaster-talk.com/php-fo...st-my-php.html
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.

Last edited by wayfarer07; 10-18-2008 at 01:14 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 10-19-2008, 11:02 AM Re: form related query
Experienced Talker

Posts: 31
Name: Ignatius Vinoth
Trades: 0
Dear Wayfarer07/chrishirst,


Hope I have competed my task of guestbook up to 90%. Remaining I can complete without any dilemma. I would like to put my special thanx for your kind support.

Iggywiggy
iggywiggy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to form related query
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.37811 seconds with 13 queries