Jump to content


Checking if mysql data exists with strpos()

  • Please log in to reply
2 replies to this topic

#1 dharm

  • Members
  • PipPip
  • Member
  • 27 posts

Posted 15 September 2006 - 10:52 AM

I have written this code but im having trouble making it work. This code is to check weather a part of a needle is present within haystack. I think the problem maybe when thrs more then one mysql result checked. This code is to help prevent inserting data into the database more then once if the refresh button is pressed. Here is the code:

<?php // check discription string within $basevar if is present in database 
$query2 = "Select * from ".$DBprefix."protable where client_id='$cid' and off_date='0'";
$result2 = mysql_query($query2); $pos[] = array();

while($row1 = mysql_fetch_array($result2)){
$discription = $row1["discription"];
$mystring = $basevars;
$pos[] = strpos($mystring, $discription);}

$num = count($pos);
for ($i=0;$i<$num;$i++) {
if ($pos[$i] === false ){} else{
echo $pos[1]; // checkin output
echo"<h1>Data Already complete</h1>;

exit();}} ?>

Any help will be much appreciated!



#2 448191

  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 15 September 2006 - 01:06 PM

Don't you have any indexes on the table other than the id? That would cause failure.

Otherwise a simple way would to be store the action in session variable, and preventing repeating it when the same action is requested twice.

You store your values in the table, upon sucess you write a value, say the uri or something else describing the action just performed. On every page request you simply check if that session variable is set, and skip the storing part if it is. At some point you will of course have to either unset or overwrtie it, unless you want  the action to become unavailable for the duration of the session.

If you want to ensure a database field has a unique value, use a UNIQUE index in it.

Oh I almost forgot, your code has a parse error:

echo"<h1>Data Already complete</h1>";

#3 dharm

  • Members
  • PipPip
  • Member
  • 27 posts

Posted 15 September 2006 - 04:11 PM

thank you for ur reply..

Sry..yes thr is also a unique id called project_id. But i will try putting a value within sessions sounds simpler!!. Thx!

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users