problem with counting records

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
hannahd
Forum Newbie
Posts: 16
Joined: Wed Feb 18, 2004 9:40 am

problem with counting records

Post 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");
}
}
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
hannahd
Forum Newbie
Posts: 16
Joined: Wed Feb 18, 2004 9:40 am

Post by hannahd »

It's still causing me problems on the same line.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

i take it you have connected to the DB first!?

Mark
hannahd
Forum Newbie
Posts: 16
Joined: Wed Feb 18, 2004 9:40 am

Post 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>
Post Reply