Jump to content

Don't insert into database if variable matches string


Go to solution Solved by wright67uk,

Recommended Posts

Hi, I'm trying to prevent strings that read "Favourite" or" 2nd Favourite" from being inserted into my database.

I've looked at the manual and I think that i'm using == correctly.  Perhaps i'm going wrong elsewhere?

 

 

 <?php  if ($mysqli->connect_error) {die('Connect Error: ' . $mysqli->connect_error);}
					
if ($stmt = $mysqli->query("SELECT * FROM races WHERE name = '$name' and race = '$race' ")) {
                        $row_cnt = $stmt->num_rows;	
                        $stmt->close();}
                    
                    if ($row_cnt > 0) {	
                   $stmt = $mysqli->prepare("UPDATE races SET odds = ?, race = ? WHERE name = ?"); 
                   $stmt->bind_param('sss', $odds, $race, $name);
                   $stmt->execute();
                   $stmt->close();  }

                     else if (($name !== "Favourite") OR ($name !== "2nd Favourite")) {
                    $stmt = $mysqli->prepare("INSERT INTO races VALUES (?, ?, ?)");
                    $stmt->bind_param('sss', $name, $odds, $race);
                    $stmt->execute();
                    $stmt->close();  } 
					} 
                   } 
                  } 
	             } else { 
	
	?>  

 

Ah thanks Jessica.

 

I think somthing else is wrong aswell.

It let's in 2nd Favourite and adds it many many times.

 

I'm trying to put the feed into mysql.

If the feed is unavailable, then I want the script to try again, hence the refresh.

 

I wasn't expecting 2nd Favourite to be added at all, let alone over and over again.

 

<html><body> <?php ob_start();                  $mysqli = new mysqli('', '', '' ); 				                 $url = "http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=2&marketSort=--&filterBIR=N";                $root = @simplexml_load_file($url);                if ($root) {                    $data = get_object_vars($root);                    $response = $data['response'];                    $class = $response->williamhill->class;                    $attClass = $class->attributes();                    $types = $class->type;                foreach ($types as $_type) {                     $_attributeType = $_type->attributes();                     $markets = $_type->market;                     $html = '';					 	        foreach ($markets as $_market) {$_attributeMarket = $_market->attributes();                                       $participants = $_market->participant;                                                                                                                foreach ($participants as $_participants) {$_attributeParticipant = $_participants->attributes();                     $name = $_attributeParticipant["name"];                        $odds = $_attributeParticipant["odds"];                     $race = $_attributeMarket["name"];                     echo $name . " - " . $odds . " - " . $race ?><br/>                                         <?php  if ($mysqli->connect_error) {die('Connect Error: ' . $mysqli->connect_error);}										if ($stmt = $mysqli->query("SELECT * FROM races WHERE name = '$name' and race = '$race' ")) {                        $row_cnt = $stmt->num_rows;	                        $stmt->close();}                                        if ($row_cnt > 0) {	                   $stmt = $mysqli->prepare("UPDATE races SET odds = ?, race = ? WHERE name = ?");                    $stmt->bind_param('sss', $odds, $race, $name);                   $stmt->execute();                   $stmt->close();  }                     else if ($name !=="2nd Favourite") {                     $stmt = $mysqli->prepare("INSERT INTO races VALUES (?, ?, ?)");                    $stmt->bind_param('sss', $name, $odds, $race);                    $stmt->execute();                    $stmt->close();  } 					}                    }                   } 	             } else { 		?>        <meta http-equiv="refresh" content="3 ;url=<?php echo $_SERVER['PHP_SELF']; ?>"><?php } ob_flush(); ?></body></html>
Edited by wright67uk
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.