Page 1 of 1

problem with counting records

Posted: Wed Feb 18, 2004 10:52 am
by hannahd
I am trying to count the records in a MySQL table to see if a record already exists.

A problem occurs when there is no duplicate. The line that causes me trouble is highlighted in bold. It causes the die statement to execute.

Please help!

Thanks

Hannah



This is my code:

$sql = "SELECT COUNT(verb_name) FROM verb WHERE verb_name = '$verbname'";

$res = mysql_query($sql) or die(mysql_error());

$numrecs = mysql_result($res,0) or die("problem");


if($numrecs>0) {

$display_block = displayWarning("That record has already been entered into the database.");

}

else {

$sql = "INSERT INTO verb VALUES('', '$verbname', '$verbtype', '$changesstem')";
mysql_query($sql) or die(mysql_error());
$display_block = displayConfirmation("The verb <i>$verbname</i> has been successfully added");
}
}

Posted: Wed Feb 18, 2004 10:56 am
by JayBird
you are using the wrong function

try this

Code: Select all

$sql = "SELECT * FROM verb WHERE verb_name = '$verbname'"; 

$res = mysql_query($sql) or die(mysql_error()); 

//$numrecs = mysql_result($res,0) or die("problem"); 

$numrecs = mysql_num_rows($res);



if($numrecs>0) { 

   $display_block = displayWarning("That record has already been entered into the database."); 

} 

else { 

    $sql = "INSERT INTO verb VALUES('', '$verbname', '$verbtype', '$changesstem')"; 
mysql_query($sql) or die(mysql_error()); 
    $display_block = displayConfirmation("The verb <i>$verbname</i> has been successfully added"); 
} 
}
Mark

Posted: Wed Feb 18, 2004 11:03 am
by hannahd
It's still causing me problems on the same line.

Posted: Wed Feb 18, 2004 11:06 am
by JayBird
i take it you have connected to the DB first!?

Mark

Posted: Wed Feb 18, 2004 11:07 am
by hannahd
Yeah, I've done that :)

Here's my whole code.

I hope you can understand it. :)

<?php

include("functions.php");
if($_POST['op']=="addverb") {


$conn = mysql_connect("localhost", "") or die(mysql_error());

mysql_select_db("spanish_verbs", $conn) or die(mysql_error());

$verbname = $_POST['verb'];
$verbtype = $_POST['verbtype'];
$stemchange = $_POST['stemchanging'];
$tense = $_POST['tense'];

if($stemchange=="On") {
$changesstem = "Yes";
}
else {
$changesstem = "No";
}

$sql = "SELECT * FROM verb WHERE verb_name = '$verbname'";

$res = mysql_query($sql) or die(mysql_error());

$numrecs = mysql_num_rows($res) or die("trouble");


if($numrecs>0) {

$display_block = displayWarning("That record has already been entered into the database.");

}

else {

$sql = "INSERT INTO verb VALUES('', '$verbname', '$verbtype', '$changesstem')";
mysql_query($sql) or die(mysql_error());
$display_block = displayConfirmation("The verb <i>$verbname</i> has been successfully added");
}
}

$display_block.= "<H1>Verb editor</H1>

<FORM NAME='verbeditor' ACTION='$_SERVER[PHP_SELF]' METHOD='POST'>

<FONT FACE='Arial' SIZE=3><B>Enter a verb <INPUT TYPE='text' SIZE=12 NAME='verb'>
<P>
<FONT SIZE=2>What is the verb's type?</FONT>
<INPUT TYPE='radio' NAME='verbtype' VALUE='AR' CHECKED>AR
<INPUT TYPE='radio' NAME='verbtype' VALUE='ER'>ER
<INPUT TYPE='radio' NAME='verbtype' VALUE='IR'>IR
<INPUT TYPE='radio' NAME='verbtype' VALUE='IRR'>IRREGULAR
<P>
<FONT SIZE=2>Is this a stem changing verb?</FONT>
<INPUT TYPE='radio' NAME='stemchanging' VALUE='Yes'>Yes
<INPUT TYPE='radio' NAME='stemchanging' VALUE='No' CHECKED>No
<P>
<INPUT TYPE='hidden' NAME='op' VALUE='addverb'>
<INPUT TYPE='submit' VALUE='Add verb'>
</FORM>";


?>
<HTML>
<HEAD>
<LINK REL=STYLESHEET TYPE="text/css" HREF="styles.css">

<SCRIPT LANGUAGE='JavaScript'>

function clearFields() {
var frm = document.verbeditor
for(i=0; i<frm.elements.length; i++) {
if(frm.elements.type=="text" && frm.elements.name!="verb") {
frm.elements.value = "";
}
}
}
</SCRIPT>
</HEAD>
<BODY bgcolor="#B85404">
<CENTER>
<TABLE WIDTH=90% BGCOLOR="white"><TR><TD>
<?php print $display_block?>
<CENTER>
<?php
include("menu.php");
?>

</TD></TR></TABLE>
</BODY>
</HTML>