0

how to throw error if the name has been inserted more than 5 times in sql in php??

$sql = mysql_query("INSERT INTO `transfer`(`t_id`, `agent_id`, `agent_name`, `date`, `name`, `phone`, `email`, `tname`, `tphone`, `temail`, `status`) VALUES (NULL,'$agent_id','$agent_name','$date','$name','$phone','$email','$tname','$tphone','$temail','$cmmnt','$status')");
pavel
  • 26,538
  • 10
  • 45
  • 61
  • yes.....it should allow insertion bt if the same name has been inserted more than 5 times than it should not insert the value...!! it should throw error....!!!!! – Bhaskar Gariibidi Mar 07 '15 at 09:22

3 Answers3

3

Try this

$sql = mysql_query("SELECT * FROM transfer HAVING COUNT(name) > 5");

if (mysql_num_rows($sql) != 0) {

    // There are more than 5 - print an error.
    echo 'Error - More than 5';

}else{

    // No Error - Insert
    $insert_query = "INSERT INTO `transfer`(`t_id`, `agent_id`, `agent_name`, `date`, `name`, `phone`, `email`, `tname`, `tphone`, `temail`, `status`) VALUES (NULL,'$agent_id','$agent_name','$date','$name','$phone','$email','$tname','$tphone','$temail','$cmmnt','$status')";
    $insert_result= $mysqli -> query($insert_query);

}
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
0

Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which.

See the above note - I'd suggest you use the mysqli_ function as the mysql_ functions have been deprecated.

$mysqli = new mysqli('localhost', 'username', 'password', 'db');

$query  = "SELECT * FROM transfer HAVING COUNT(name) > 5";
$result = $mysqli -> query($query);
$num    = $result -> num_rows();

if($num != 0){

    // There are more than 5 - print an error.
    echo 'Error - More than 5';

}else{

    // No Error - Insert
    $insert_query = "INSERT INTO `transfer`(`t_id`, `agent_id`, `agent_name`, `date`, `name`, `phone`, `email`, `tname`, `tphone`, `temail`, `status`) VALUES (NULL,'$agent_id','$agent_name','$date','$name','$phone','$email','$tname','$tphone','$temail','$cmmnt','$status')";
    $insert_result= $mysqli -> query($insert_query);

}
Community
  • 1
  • 1
ajtrichards
  • 29,723
  • 13
  • 94
  • 101
0

Comments re the importance of using mysqli* instead of mysql* API are quite important and should not be ignored, but since you've started with mysql* I will continue in that same route...

I believe the previous answers (using HAVING ...) were not answering your actual question. Try the code below.

$sql = mysql_query("SELECT * FROM transfer WHERE name = '$name'");

if (mysql_num_rows($sql) >= 5) {
    echo "Error - Too many occurrences of $name";
} else {
    $insert_query = "INSERT INTO `transfer`(`t_id`, `agent_id`, `agent_name`, `date`, `name`, `phone`, `email`, `tname`, `tphone`, `temail`, `status`) VALUES (NULL,'$agent_id','$agent_name','$date','$name','$phone','$email','$tname','$tphone','$temail','$cmmnt','$status')";
    $insert_result= $mysqli -> query($insert_query);
}
Peter Bowers
  • 3,063
  • 1
  • 10
  • 18