Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prevent multiple client inserts 1

Status
Not open for further replies.

rogerzebra

Technical User
May 19, 2004
216
0
0
SE
Hi friends,
I'm trying to validate a form and prevent client submissions being registered twice in the database. But unfortunately something is wrong and I can't see were the problem is. I know it's alot going on here, I was hoping for someone to check my code and perhaps see what's wrong with it. As usual I'm very thankful for your help.

here is the code I'm struggling with, it's the code in bold I have problem with.
My sql code seems to work fine when I execute it separate.

Code:
[b]$result = mysql_query("SELECT count(*) FROM submissions WHERE NameInsured = '" . $NameInsured . "'");

if (mysql_result($result) == 0) { [/b]

$query= "INSERT INTO submissions (SubmissionID, NameInsured, Fein, EffectiveDate, DateSubmitted, Producer, Contacts1, ClassCode)
VALUES (NULL, '$NameInsured', '$Fein', '$EffectiveDate', NOW(), '$Producer', '$Contacts1', '$ClassCode')";

$result = mysql_query($query);

$SubmissionID = mysql_insert_id();

$query2= "INSERT INTO test (EvaluationID, DateR) VALUES ('$SubmissionID', '$DateR')";

$result2 = mysql_query($query2);


header ('Location: enter_submissions.php');

[b]}else {
echo "Sorry, your insert data already exist in our database<br /> Please, try again.";
[/b]
}
when I execute the code it's just chewing it over and over and nothings happend.
thanks in advance /rz
 
like a cow eating grass...hehe

hi sleipnir,

..something did happend though. it's been looping and make hundreds of inserts...hmm great. If I put a limit clause in the insert query would at least stop the loop right?
All I need this to do is to check whether the clients name already exist in the database or not.
If the query result equals 0 then do the insert, if not go back and insert something else.
that's it.

btw ...may I ask to satisfy my curiosity...what is tanstaafl!! mean??
 
Maybe change this:

if (mysql_result($result) == 0) {

to:

if (mysql_num_rows($result) == 0) {
 
hi guys, thanks for your time.
Woody... I have tried mysql_num_rows aswell when I try that I'm not able to register anything.

Ken, my scriptname is enter_submissions so the header location should be submission_form.php. I changed it and now it doesn't register more than one submission at the time...thank you I appreciate it.
My problem still though, is that it doesn't validate the code and keep register despite that the client name exists in the database.

I have alot of code on this page, so I might show you the rest of it and perhaps you guys are able to figure out of what is going on here.
Code:
$result = mysql_query("SELECT count(*) FROM submissions WHERE NameInsured = '" . $NameInsured . "'");
 if (mysql_result($result) == 0) {

$query= "INSERT INTO submissions (SubmissionID, NameInsured, Fein, EffectiveDate, DateSubmitted, Producer, Contacts1, ClassCode)
		VALUES (NULL, '$NameInsured', '$Fein', '$EffectiveDate', NOW(), '$Producer', '$Contacts1', '$ClassCode')";
		$result = mysql_query($query);

$SubmissionID = mysql_insert_id();
		
$query2= "INSERT INTO evaluation (EvaluationID, DateR) VALUES ('$SubbmissionID', '$DateR')";

		$result2 = mysql_query($query2);

header ('Location: submission_form.php');	
	}
	else 
	{
    echo "Sorry, your insert data already exist in our database<br /> Please, try again.";
	
  	}
	
	$db = mysql_connect("localhost", "root", "matrix");
	mysql_select_db("intranet",$db) or die ('Unable to connect to database');

$q="SELECT SubmissionID, NameInsured, Fein, EffectiveDate, DateSubmitted, DateR, Producer, Contacts1, ClassCode 
	FROM submissions, evaluation 
	WHERE submissions.SubmissionID = evaluation.EvaluationId 
	AND NameInsured ='$NameInsured'"; 

	$result = mysql_query( $q, $db )
	or die(" - Failed More Information:<br><pre>$q</pre><br>Error: " . mysql_error());

		$num_rows = mysql_num_rows($result);
	if ($myrow = mysql_fetch_array($result))
	{
		bla,bla
	do 
	{
		bla,bla

	} 
	while ($myrow = mysql_fetch_array($result));
	
		echo "</table>\n";
		echo "<a href=\"submission_form.php\">Click here to return to the form page.</a>";
	} 
	else 
	{
		echo "Sorry, no records were found"; 
	}
what do you guys think??
Thanks in advance /rz
 
Try changing your mysql_result line to read:
Code:
if (mysql_result($result, 0) == 0) {
Looking at the php manual, mysql_result must take two parameters, not just one.
 
$query = mysql_query("SELECT count(*) FROM submissions WHERE NameInsured = '" . $NameInsured . "')"; # bcareful of the quotes
try this
$result=mysql_query($query,$db);

if (mysql_num_rows($result) == 0) {

$query= "INSERT INTO submissions (SubmissionID, NameInsured, Fein, EffectiveDate, DateSubmitted, Producer, Contacts1, ClassCode)
VALUES (NULL, '$NameInsured', '$Fein', '$EffectiveDate', NOW(), '$Producer', '$Contacts1', '$ClassCode')";
$result = mysql_query($query);

$SubmissionID = mysql_insert_id();

$query2= "INSERT INTO evaluation (EvaluationID, DateR) VALUES ('$SubbmissionID', '$DateR')";

$result2 = mysql_query($query2);

header ('Location: submission_form.php');
}
else
{
echo "Sorry, your insert data already exist in our database<br /> Please, try again.";

}

$db = mysql_connect("localhost", "root", "matrix");
mysql_select_db("intranet",$db) or die ('Unable to connect to database');

$q="SELECT SubmissionID, NameInsured, Fein, EffectiveDate, DateSubmitted, DateR, Producer, Contacts1, ClassCode
FROM submissions, evaluation
WHERE submissions.SubmissionID = evaluation.EvaluationId
AND NameInsured ='$NameInsured'";

$result = mysql_query( $q, $db )
or die(" - Failed More Information:<br><pre>$q</pre><br>Error: " . mysql_error());

$num_rows = mysql_num_rows($result);
if ($myrow = mysql_fetch_array($result))
{
bla,bla
do
{
bla,bla

}
while ($myrow = mysql_fetch_array($result));

echo "</table>\n";
echo "<a href=\"submission_form.php\">Click here to return to the form page.</a>";
}
else
{
echo "Sorry, no records were found";
}
 
i've forgot this one:

change

SELECT count(*) FROM submissions...

to

SELECT * FROM submissions...

hope this help...


yenping
 
yenping, your post is not different with the combination of his original POST and my suggestion.

It's also wrong.

How could this run?

$query = mysql_query("SELECT count(*) FROM submissions WHERE NameInsured = '" . $NameInsured . "')";
$result=mysql_query($query,$db);

I think you probably mean this:
$query = "SELECT count(*) FROM submissions WHERE NameInsured = '" . $NameInsured . "'";
$result=mysql_query($query,$db);
 
Hi guys,
thanks for trying it's much appreciated.
So, this is were I'm at now. I tried Vragabond's code
and that partly work. As you can see in the code that when code is executed, it supposed to register in both table submission and using mysql_insert_id()is for insert of the ID in table evaluation. All that worked before I added the validation part.

This is what happend: when I execute the code now it's register the table submission as it should but in table evaluation it's just register a 0 for the ID and 0000-00-00 00:00:00 for the DateR (datetime)column. When the code is executed it also supposed to print the result on another page but, I'm still on the same insert page. I guess the header function should go somewhere else? What do think of this?
 
**********SOLVED***********************

okay I got this to work. Without the header it seems to be okay.
Code:
if (mysql_result($result, 0) == 0) {
..it is
thank you Vragabond, you deserve a star...and thanks all of you, you are a amazing bunch of nice people.
/rz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top