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
Link to comment
Share on other sites

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];
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.