Reputation: 485
I have some code im trying to get working. What im looking for is to have it so that i query a site, then display results and upload to database. I want it to ignore duplicates which i have working by checking the date. issue im having is when a user searches for another stock ticker it does not insert into DB because the date is the same. Anyone have any suggestions?
<?php
$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bbpinpal_stocks", $con);
mysql_query("INSERT INTO stocks (stock_name, stock_value, stock_perc, date)
VALUES ('$name', '$last','$perc2', '$date')");
$result = mysql_query("SELECT * FROM stocks");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Value</th>
<th>Percent Change</th>
<th>Date</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['stock_name'] . "</td>";
echo "<td>" . $row['stock_value'] . "</td>";
echo "<td>" . $row['stock_perc'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
I have set date to unique but i know that is prob my issue. How can i have this so that it checks both name and date and if match to ignore
Upvotes: 0
Views: 73
Reputation: 1309
INSERT IGNORE prevents you adding data with a primary key that is already in the database. You can have a primary key that consists of 2 fields.
Upvotes: 0
Reputation: 5335
I have set date to unique but i know that is prob my issue
This is most probably the issue. Why don't you use another column to uniquely identify a row?
http://en.wikipedia.org/wiki/Unique_key
"..Each unique key is composed from one or more data attributes of that data entity.."
Upvotes: 0
Reputation: 125865
Replace your UNIQUE
index on date
with one on (stock_name, date)
:
ALTER TABLE stocks
DROP KEY date,
ADD UNIQUE KEY (stock_name, date)
Upvotes: 1