wright67uk Posted May 3, 2013 Share Posted May 3, 2013 For some reason my code is creating duplicate entries within my database. Am I using row count in the wrong way? Line 32 <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") AND ($name !="Favourite")) { $stmt = $mysqli->prepare("INSERT INTO races VALUES (?, ?, ?)"); $stmt->bind_param('sss', $name, $odds, $race); $stmt->execute(); $stmt->close(); } } } } } ?> <?php ob_flush(); ?> </body></html> Link to comment https://forums.phpfreaks.com/topic/277575-is-row-counting-causing-duplicate-entries/ Share on other sites More sharing options...
Barand Posted May 3, 2013 Share Posted May 3, 2013 How is your table "races" defined? What constitutes a duplicate, same race and name in more than one record? Link to comment https://forums.phpfreaks.com/topic/277575-is-row-counting-causing-duplicate-entries/#findComment-1427926 Share on other sites More sharing options...
wright67uk Posted May 3, 2013 Author Share Posted May 3, 2013 I've 3 varchar columns - name, race, and odds. with a primary key on name. I want only one record for each name. If the name exists, then I want the odds and race to be updated. Link to comment https://forums.phpfreaks.com/topic/277575-is-row-counting-causing-duplicate-entries/#findComment-1427934 Share on other sites More sharing options...
Barand Posted May 3, 2013 Share Posted May 3, 2013 you can use a single query INSERT INTO races (odds, race name) VALUES ('$odds', '$race', '$name') ON DUPLICATE KEY UPDATE odds = '$odds', race = '$race' Link to comment https://forums.phpfreaks.com/topic/277575-is-row-counting-causing-duplicate-entries/#findComment-1427938 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.