我有一个名为casino
的数据库,我有一个名为cards
的表,其中有card_numbers
和points
。这是数据库条目的一个示例。
+------------------------+
| cards |
+------------------------+
| card_number | points |
+------------------------+
| 55555 | 200 |
+------------------------+
我解析.csv文件并更新每个卡号的点数。如果我在一个.csv中有相同卡号的更多条目,我保留第一个并跳过有关该卡号的所有其他内容。当我点击数据库中已存在的卡号时,我只是用前一个值和新值更新points
值。这是我的实现,但有些东西不起作用。
你可以试着帮我看看有什么不对吗?
// function to check if the user already exists in the database
function exists_in_database($db_handle, $card_number)
{
$query = mysqli_query($db_handle, "SELECT * FROM `cards` WHERE card_number='".$card_number."'");
if(mysqli_num_rows($query) > 0)
return true;
else
{
return false;
if (!mysqli_query($db_handle,$query))
{
return false;
}
}
}
// function to update points of a specific card number
function update_points_in_database($db_handle, $card_number, $points)
{
$query = mysqli_query($db_handle, "UPDATE `cards` SET `points` = `points` + ".$points." WHERE `card_number` = ".$card_number."");
}
/* DATABASE PART */
$servername = "localhost"; // database host
$username = "root"; // database user
$password = "root"; // database password
$db_name = "casino"; // database name
// create connection
$db_handle = new mysqli($servername, $username, $password, $db_name);
// check connection
if($db_handle->connect_error)
die("Connection <b>failed</b>: " . $db_handle->connect_error . "<br />");
/* PARSING PART */
for($key = 0; $key < count($_FILES['files']['tmp_name']); $key++)
{
$tmp_file_path = $_FILES['files']['tmp_name'][$key];
if($tmp_file_path)
{
$new_file_path = 'uploaded-' . $_FILES['files']['name'][$key];
if(move_uploaded_file($tmp_file_path, $new_file_path))
{
// opening
if(!($handle = fopen($new_file_path, "r")))
die("fopen failed. reason: " . $error_get_last());
// helper count var
$count = 0;
// array of card numbers we already read
$cards_passed = [];
while(!feof($handle)) // until we're done with one .csv
{
// add to helper var
$count++;
// parse the file
$contents = fgetcsv($handle, NULL, ";");
// skip the header (umisteni, body...)
if($count < 2)
continue;
// if there is nothing there
else if($contents[0] == NULL)
continue;
// if there is anything else than a number in the card number
else if(!is_numeric($contents[0]))
continue;
$card_number = $contents[0]; // card number
$points = $contents[5]; // points
if($contents[6]) // if we have extra points
$points += $contents[6]; // add them to the total
if(exists_in_database($card_number)) // check if we already have this card in database
{
if(in_array($card_number, $cards_passed)) // more occurences in one files, skip that
continue;
// first occurence
update_points_in_database($db_handle, $card_number, $points); // update the points in db
array_push($cards_passed, $card_number); // so we know we already passed this card number
echo "The card number " . $card_number . " has been updated with " . $points . " points.<br />";
continue;
}
// otherwise we simply insert the card number
$sql = "INSERT INTO cards (card_number, points)
VALUES ('$card_number', $points)";
array_push($cards_passed, $card_number);
// if everything went smoothly
if($db_handle->query($sql) === TRUE)
if($contents[6]) // extra points
echo "The card number " . $card_number . " has been added with " . $points . " (including extra " . $contents[6] . " points).<br />";
else // no extra points
echo "The card number " . $card_number . " has been added with " . $points . " points.<br />";
// if we got an error
else
echo "Error: " . $sql . "<br />" . $db_handle->error; // let us know
}
echo "<br /><br />"; // new file
// closing
if(!(fclose($handle)))
// if we have an error while closing
echo "fclose failed. reason: " . $error_get_last();
}
// error possibility
else
// let us know
die("Error while handling the files!");
}
}
$db_handle->close();
当系统在不同文件中遇到相同的卡号时,会引发以下错误:
错误:INSERT INTO卡(card_number,points)VALUES('12165',443) 密钥'card_number'Error重复输入'12165':INSERT INTO卡(card_number,points)VALUES('12180',133)
但我不知道为什么会这样做,因为我认为我用条件语句对待它。
编辑:
我正在使用
$sql = "INSERT INTO cards (card_number, points) VALUES ('$card_number', $points) ON DUPLICATE KEY UPDATE `cards` SET `points` = `points` + ".$points."";
但这给了我一个语法错误。有什么想法可以吗?
答案 0 :(得分:0)
我自己想出来,这就是我所需要的:
$sql = "INSERT INTO cards (card_number, points) VALUES ('$card_number', $points) ON DUPLICATE KEY UPDATE `points` = `points` + $points";