Jump to content

[SOLVED] Count rows


web_master

Recommended Posts

Hi,

 

I put into database:

<?php 
$Query = mysql_query('INSERT INTO `request` (
	`request_year`,
	`request_month`,
	`request_day`,
	`request_date`,
	`request_ip`,
	`request_tel`,
	`request_txt`,
	`request_artist`,
	`request_title`,
	`request_datetime`,
	`request_ipp`

) VALUES (
	"'.$_POST['request_year'].'",
	"'.$_POST['request_month'].'",
	"'.$_POST['request_day'].'",
	"'.$_POST['request_year'].$_POST['request_month'].$_POST['request_day'].'",
	"'.$_POST['request_ip'].'",
	"'.$_POST['request_tel'].'",
	"'.$NoPregText.'",
	"'.$_POST['request_artist'].'",
	"'.$_POST['request_title'].'",
	"'.date('Y-m-d H:i:s').'",
	"'.getenv('REMOTE_ADDR').'"
)');

?>

 

But! Before put the data into database I want to check is there in database same phonenumber 3 times (`request_tel`)?

 

How can I check this?

 

(if in database same phonenumber more than 3 times the data can't be put into dbase)

 

thanx

Link to comment
Share on other sites

Probably a better way to do this, but just do:

 

$count = mysql_query("SELECT `request_tel` FROM `request` WHERE `request_tel`='".$_POST['request_tel']."' ");
if (mysql_num_rows($count) <= 3)
{
$Query = mysql_query('INSERT INTO `request` (
      `request_year`,
      `request_month`,
      `request_day`,
      `request_date`,
      `request_ip`,
      `request_tel`,
      `request_txt`,
      `request_artist`,
      `request_title`,
      `request_datetime`,
      `request_ipp`
      
   ) VALUES (
      "'.$_POST['request_year'].'",
      "'.$_POST['request_month'].'",
      "'.$_POST['request_day'].'",
      "'.$_POST['request_year'].$_POST['request_month'].$_POST['request_day'].'",
      "'.$_POST['request_ip'].'",
      "'.$_POST['request_tel'].'",
      "'.$NoPregText.'",
      "'.$_POST['request_artist'].'",
      "'.$_POST['request_title'].'",
      "'.date('Y-m-d H:i:s').'",
      "'.getenv('REMOTE_ADDR').'"
   )');

  if ($Query)
    {
     //inserted correctly
    }
  else
    {
     //issue inserting
    }

}
else
{
//3 or more with the same tel in db
}

Link to comment
Share on other sites

Hi,

 

I put into database:

<?php 
$Query = mysql_query('INSERT INTO `request` (
	`request_year`,
	`request_month`,
	`request_day`,
	`request_date`,
	`request_ip`,
	`request_tel`,
	`request_txt`,
	`request_artist`,
	`request_title`,
	`request_datetime`,
	`request_ipp`

) VALUES (
	"'.$_POST['request_year'].'",
	"'.$_POST['request_month'].'",
	"'.$_POST['request_day'].'",
	"'.$_POST['request_year'].$_POST['request_month'].$_POST['request_day'].'",
	"'.$_POST['request_ip'].'",
	"'.$_POST['request_tel'].'",
	"'.$NoPregText.'",
	"'.$_POST['request_artist'].'",
	"'.$_POST['request_title'].'",
	"'.date('Y-m-d H:i:s').'",
	"'.getenv('REMOTE_ADDR').'"
)');

?>

 

But! Before put the data into database I want to check is there in database same phonenumber 3 times (`request_tel`)?

 

How can I check this?

 

(if in database same phonenumber more than 3 times the data can't be put into dbase)

 

thanx

 

Try this:

<?php
$sql = "SELECT COUNT(*) AS `phone_count` FROM `request` WHERE `request_tel` = '" . $_POST['request_tel'] . "'";
$rs = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($rs);
mysql_free_result($rs);
if ( $row['phone_count'] >= 3 ) {
   echo 'Phone number <strong>' . $_POST['request_tel'] . '</strong> entered more then 3 times.';
}
?>

 

Put this code above the INSERT SQL statement and do the required checks in it.

 

Hope this will help.

 

Link to comment
Share on other sites

For starters I would suggest removing $_POST vars and $_GET vars from your SQL entry, thats just askin for issues later in a number of ways.. take all your POST and GET vars and change them into normal vars..

 

$tele = $_POST['request_tel'];

for example. The one reason I say this is cause you can start filtering everything that's being posted through your form and make sure its not malicious coding that's attempting to be passed through, make sure its not something that's going to break your script like a double quote and so on.

 

Mostly the malicious code i would worry about more then anything. As anyone with a little knolledge whos bored enough or doesnt like you can just wipe out your database as if it were never there.. google "mysql injection" youll see what I mean..

 

as for your original question how do you check to see if its in there already..

 

Check your database.. do a row count looking specificly for that value if it comes up one or more times its already in there. if it comes up null or 0 then insert.. Ill be happy to help you a little more should you need it.

Link to comment
Share on other sites

Try this:

<?php
$sql = "SELECT COUNT(*) AS `phone_count` FROM `request` WHERE `request_tel` = '" . $_POST['request_tel'] . "'";
$rs = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($rs);
mysql_free_result($rs);
if ( $row['phone_count'] >= 3 ) {
   echo 'Phone number <strong>' . $_POST['request_tel'] . '</strong> entered more then 3 times.';
}
?>

 

Put this code above the INSERT SQL statement and do the required checks in it.

 

Hope this will help.

 

 

ITS WORK, THANKX PEOPLE!!! GREAT

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.