Jump to content


Photo

Don't insert into database if variable matches string


Best Answer wright67uk, 03 May 2013 - 05:12 AM

Wo! My code went a bit bonkers when i edited my forum text from my mobile.

Go to the full post


  • Please log in to reply
3 replies to this topic

#1 wright67uk

wright67uk

    Advanced Member

  • Members
  • PipPipPip
  • 438 posts

Posted 02 May 2013 - 04:33 PM

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 { 
	
	?>  

 



#2 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 02 May 2013 - 04:37 PM

Use and, not or.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 wright67uk

wright67uk

    Advanced Member

  • Members
  • PipPipPip
  • 438 posts

Posted 03 May 2013 - 03:32 AM

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, 03 May 2013 - 03:35 AM.


#4 wright67uk

wright67uk

    Advanced Member

  • Members
  • PipPipPip
  • 438 posts

Posted 03 May 2013 - 05:12 AM   Best Answer

Wo! My code went a bit bonkers when i edited my forum text from my mobile.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com