Jump to content

Checking if mysql data exists with strpos()


dharm

Recommended Posts

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:

[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>;

include("../../includes/footer.php");
exit();}} ?> [/code]

Any help will be much appreciated!

Thxs

dharm
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>[i][b]"[/b][/i];

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.