Hello friends,

I have a very minor error, but i am not able to figure out which one is that. I have a simple form page and it goes to the another page and insert the values in database. But the thing is it inserts multiple rows in the database. here is the code

<script type="text/javascript"> 
function validate1(form) 
{ 
if (form.last.value == "") 
            { 
                alert("You must enter Last Name."); 
                form.last.focus(); 
                return (false); 
            } 

if (form.first.value == "") 
            { 
                alert("You must enter First Name."); 
                form.first.focus(); 
                return (false); 
            } 
if (form.comments.value == "") 
            { 
                alert("You must enter Comments/Feedback."); 
                form.comments.focus(); 
                return (false); 
            } 

            return (true); 
} 
</script> 

<form method="post" action="submit1.php"enctype="multipart/form-data" onsubmit="return validate1(this)" name="K12"> 
<table > 
<tr> 
<td align="left" valign="top">Last Name:</td> 
<td> <input type="text" size="20" name="last"></td> 
</tr> 
<tr> 
<td align="left" valign="top">First Name:</td> 
<td> <input type="text" size="20" name="first"></td> 
</tr> 
<tr> 
<td width="110" align="left" valign="top">Comments/Feedback</td> 
<td><textarea cols="60" rows="7" name="comments"></textarea></td> 
</tr> 
<tr> 
<td width="110" align="left" valign="top"></td> 
<td><br><input type="Submit" value="Submit" name="Submit"> 
</td> 
</tr> 
</table> 
</form>
<?php 

$first=$_POST['first']; 
$last=$_POST['last']; 
$comments=$_POST['comments']; 
$date = date("Y-m-d"); 

$sql1 = "INSERT INTO feed (first_name,last_name,date,comments)VALUES('$first','$last','$date','$comments')"; 
$rs = mysql_query($sql1); 
echo "Your feedback is submitted."; 
?>

Recommended Answers

All 6 Replies

It may happen if second page is refreshed continuously. After inserting you redirect browser to another or first page. Also use cookies.

Hello friends,

I have a very minor error, but i am not able to figure out which one is that. I have a simple form page and it goes to the another page and insert the values in database. But the thing is it inserts multiple rows in the database. here is the code

<script type="text/javascript"> 
function validate1(form) 
{ 
if (form.last.value == "") 
            { 
                alert("You must enter Last Name."); 
                form.last.focus(); 
                return (false); 
            } 

if (form.first.value == "") 
            { 
                alert("You must enter First Name."); 
                form.first.focus(); 
                return (false); 
            } 
if (form.comments.value == "") 
            { 
                alert("You must enter Comments/Feedback."); 
                form.comments.focus(); 
                return (false); 
            } 

            return (true); 
} 
</script> 

<form method="post" action="submit1.php"enctype="multipart/form-data" onsubmit="return validate1(this)" name="K12"> 
<table > 
<tr> 
<td align="left" valign="top">Last Name:</td> 
<td> <input type="text" size="20" name="last"></td> 
</tr> 
<tr> 
<td align="left" valign="top">First Name:</td> 
<td> <input type="text" size="20" name="first"></td> 
</tr> 
<tr> 
<td width="110" align="left" valign="top">Comments/Feedback</td> 
<td><textarea cols="60" rows="7" name="comments"></textarea></td> 
</tr> 
<tr> 
<td width="110" align="left" valign="top"></td> 
<td><br><input type="Submit" value="Submit" name="Submit"> 
</td> 
</tr> 
</table> 
</form>
<?php 

$first=$_POST['first']; 
$last=$_POST['last']; 
$comments=$_POST['comments']; 
$date = date("Y-m-d"); 

$sql1 = "INSERT INTO feed (first_name,last_name,date,comments)VALUES('$first','$last','$date','$comments')"; 
$rs = mysql_query($sql1); 
echo "Your feedback is submitted."; 
?>

can you try to remove this in your form? "enctype="multipart/form-data".

Also you should insert some security as users could SQL inject into your database.

I deleted "enctype="multipart/form-data" from form

Now its inserting 2 times in the databse

the 1st insert works properly. i mean lastname, firstname and comments are going properly.

But the 2nd insert is inserting blank values

I deleted "enctype="multipart/form-data" from form

Now its inserting 2 times in the databse

the 1st insert works properly. i mean lastname, firstname and comments are going properly.

But the 2nd insert is inserting blank values

did you use something like this on your second page?

//it is in your form snippet
// <input type="Submit" value="Submit" name="Submit">


if(isset($_POST)) {
put your code here...
}

Please do this:

<script type="text/javascript"> 
function validate1(form) 
{ 
if (form.last.value == "") 
            { 
                alert("You must enter Last Name."); 
                form.last.focus(); 
                return (false); 
            } 

if (form.first.value == "") 
            { 
                alert("You must enter First Name."); 
                form.first.focus(); 
                return (false); 
            } 
if (form.comments.value == "") 
            { 
                alert("You must enter Comments/Feedback."); 
                form.comments.focus(); 
                return (false); 
            } 

            return (true); 
} 
</script> 

<form method="post" action="submit1.php" onsubmit="return validate1(this)" name="K12"> 
<table > 
<tr> 
<td align="left" valign="top">Last Name:</td> 
<td> <input type="text" size="20" name="last"></td> 
</tr> 
<tr> 
<td align="left" valign="top">First Name:</td> 
<td> <input type="text" size="20" name="first"></td> 
</tr> 
<tr> 
<td width="110" align="left" valign="top">Comments/Feedback</td> 
<td><textarea cols="60" rows="7" name="comments"></textarea></td> 
</tr> 
<tr> 
<td width="110" align="left" valign="top"></td> 
<td><br><input type="Submit" value="Submit" name="Submit"> 
<input type="hidden" name="action" value="add" /></td> 
</tr> 
</table> 
</form>

I add the hidden field name above, named "action", contains "add" data.

<?php 

$action=$_POST['action']; 
$first=$_POST['first']; 
$last=$_POST['last']; 
$comments=$_POST['comments']; 
$date = date("Y-m-d"); 
if($action=="add")
{
$sql1 = "INSERT INTO feed (first_name,last_name,date,comments)VALUES('$first','$last','$date','$comments')"; 
$rs = mysql_query($sql1); 
echo "Your feedback is submitted."; 
}
?>

This will work, i have tested.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.