wright67uk Posted May 2, 2013 Share Posted May 2, 2013 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 { ?> Link to comment https://forums.phpfreaks.com/topic/277558-dont-insert-into-database-if-variable-matches-string/ Share on other sites More sharing options...
Jessica Posted May 2, 2013 Share Posted May 2, 2013 Use and, not or. Link to comment https://forums.phpfreaks.com/topic/277558-dont-insert-into-database-if-variable-matches-string/#findComment-1427835 Share on other sites More sharing options...
wright67uk Posted May 3, 2013 Author Share Posted May 3, 2013 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> Link to comment https://forums.phpfreaks.com/topic/277558-dont-insert-into-database-if-variable-matches-string/#findComment-1427907 Share on other sites More sharing options...
wright67uk Posted May 3, 2013 Author Share Posted May 3, 2013 Wo! My code went a bit bonkers when i edited my forum text from my mobile. Link to comment https://forums.phpfreaks.com/topic/277558-dont-insert-into-database-if-variable-matches-string/#findComment-1427918 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.